Skip to content

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:

  1. The region we're operating inside of
  2. The API access key
  3. The API secret key

First we define a variable for the region:

1
2
3
4
variable "aws_region" {
  description = "The AWS region to deploy the solution into"
  type        = string
}

Then we ask for the access key:

1
2
3
4
variable "aws_access_key" {
  description = "The access key to authenticate with"
  type        = string
}

And finally we ask for the secret key:

1
2
3
4
variable "aws_secret_key" {
  description = "The secret key to authenticate with"
  type        = string
}

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
export TF_VAR_aws_region=ap-southeast-2
export TF_VAR_aws_access_key=some-value
export TF_VAR_aws_secret_key=secret-value

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
variable "aws_region" {
  description = "The AWS region to deploy the solution into"
  type        = string
}

variable "aws_access_key" {
  description = "The access key to authenticate with"
  type        = string
}

variable "aws_secret_key" {
  description = "The secret key to authenticate with"
  type        = string
}

Terraform Documentation

Type Documentation
variable Terraform AWS Provider

Committing the Code

  1. Set your working directory to the infrastructure/terraform repository
  2. Save the file as inputs.tf and use git add inputs.tf to add it to the Git staging area
  3. Use git commit -am 'defining our aws inputs' to commit the file to our repository
  4. Push the code to GitLab.com: git push

Last update: August 25, 2021