The inputs.tf File¶
| Filename | Location | Group | Project/Repository |
|---|---|---|---|
inputs.tf | ./inputs.tf | infrastructure | terraform |
Why?¶
We need to be able to provide flexibility to our code via external data sources. In this case we need to take in a few AWS credentials from the environment, allowing us to dynamically change the AWS account we run our Terraform code against.
This is why we need variable {} blocks.
Breakdown¶
We need three pieces of information for working with AWS:
- The region we're operating inside of
- The API access key
- The API secret key
First we define a variable for the region:
1 2 3 4 | |
Then we ask for the access key:
1 2 3 4 | |
And finally we ask for the secret key:
1 2 3 4 | |
Once we've got our variable defined we can define their values using a lot of different methods. We're interested in using environment variables.
Note
Don't add this information to the source files!
If we wanted to configure each variable via the environment, we'd do this:
1 2 3 | |
You'll notice the formatting here: TF_VAR_ follow by the variable name exactly (including case.) When Terraform parses a variable {} block it'll look for a value in multiple places. For environment variables it'll look for TF_VAR_the_variable_name and read it for a value.
The Solution¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
Terraform Documentation¶
| Type | Documentation |
|---|---|
variable | Terraform AWS Provider |
Committing the Code¶
- Set your working directory to the
infrastructure/terraformrepository - Save the file as
inputs.tfand usegit add inputs.tfto add it to the Git staging area - Use
git commit -am 'defining our aws inputs'to commit the file to our repository - Push the code to GitLab.com:
git push