Terraform 3 of 10: Input Variables using AWS Cloud9

Background

This hands-on shows how input variables can be used with Terraform, giving you more flexibility than hardcoded values.

1 of 19. Open Terraform documentation on aws_s3_bucket

Terraform Registry > Providers > AWS > Documentation >S3 (Simple Storage) > aws_s3_bucket

Resource: aws_s3_bucket

2 of 19. Open your AWS Cloud9 environment

Open your terraform.tf, resources.tf, and the terminal

Creating An Integrated Developer Environment (IDE) in the Cloud in Two Minutes!: AWS Cloud9(Step-by-Step)

3 of 19. Edit the terraform.tf, deleting Aure

Remove this part of the file, then save:

azurerm = { source = "hashicorp/azurerm" version = "=3.0.0" }

4 of 19. Update the resources.tf file, You'll now make a few aws s3 buckets using the example documentation as a reference

Note: This is NOT the most efficient way to do this. A more efficient method will be revealed in a future part of this series. Reminder: S3 bucket names should be globally unique, so you'll have to come up with your own names. The resources.tf should look similar to the image, with it only containing lines referring to three S3 buckets.

resource "aws_s3_bucket" "example1" { bucket = "humangov-tf-test-bucket1xlewis" tags = { Application = "HumanGov" ProjectId = "HGV20392" } } resource "aws_s3_bucket" "example2" { bucket = "humangov-tf-test-bucket2xlewis" tags = { Application = "HumanGov" ProjectId = "HGV20392" } } resource "aws_s3_bucket" "example3" { bucket = "humangov-tf-test-bucket3xlewis" tags = { Application = "HumanGov" ProjectId = "HGV20392" } }

5 of 19. Browse to your terraform directory, and run init, and plan with the out option.

Remember: The out option saves the plan to a file. You should be familiar with running 'terraform init' by now. Notice that you do not get the error about not saving the plan.

terraform init terraform plan -out=tcb-plan

6 of 19. Apply the plan.

Notice that because the plan was provided, you did not have to agree to apply the plan.

terraform apply tcb-plan

7 of 19. View the created buckets under S3 console.

If you check the buckets you will see the tags you applied.

8 of 19. Open terraform documentation on Input Variables

Define Input Variables

9 of 19. Create a file: variables.tf.

You decide that you want to use a variable for the application and ProjectID, because they are identical across all the buckets.

variable "application" { default = "HumanGov" } variable "projectid" { default = "HGV20392" }

10 of 19. Update resources.tf to use variables

Application = var.application ProjectId = var.projectid

11 of 19. Attempt to update your tcb-plan file.

Notice that there are no changes, because the variables worked as well as the hard-coding.

terraform plan -out=tcb-plan

12 of 19. Modify variables.tf to not include values for the variable.

Notice that I have commmented the variables in the variables.tf

variable "application" { #default = "HumanGov" } variable "projectid" { #default = "HGV20392" }

13 of 19. Run your plan -out again.

You should be prompted to provide values for the variables. Because the variables I provided did not match what was already provisioned, notice that the plan is tracking changes to the tags on each bucket.

14 of 19. Use environment variables (TF_VAR) in Terraform

Export the environment variable, the run the plan out again. You should be prompted ONLY for the projectID, as the variable you exported will be used for the application tag.

export TF_VAR_application=HumanGov3 terraform plan -out=tcb=plan

15 of 19. Use command line to pass the variable.

Try a couple commands, one will pass one variable, the other passes both variables.

terraform plan -var="application=HumanGov4" -out=tcb-plan terraform plan -var="application=HumanGov005" -var="projectid=HGV000005" -out=tcb-plan

16 of 19. Use the terraform.tfvars file to pass the variables.

Create a file called terraform.tfvars and place variables in the file. Run the plan -out again. Notice that this will also update the file.

application = "HumanGov007" projectid = "HGV007" terraform plan -out=tcb-plan

17 of 19. Lets try another way, with the -var-file command line option

Create a new file, testing.tfvars, and run the -out again. The variables will come from the file passed at the command line.

application = "HumanGovg00898765" projectid = "HGV00898765" terraform plan -var-file="testing.tfvars" -out=tcb-plan

18 of 19. Let's apply that last plan we created

If you check your S3 buckets, they should have updated tags.

terraform apply tcb-plan

19 of 19. Cleanup

Destroy the created resources. Check your S3 console, and validate the buckets are gone.

terraform destroy

Reference

Define Input Variables

Resource: aws_s3_bucket

Creating An Integrated Developer Environment (IDE) in the Cloud in Two Minutes!: AWS Cloud9(Step-by-Step)

TF_VAR_name

Input Variables

Variable Definitions (.tfvars) Files

Comments

Popular posts from this blog

Orphaned No More: Adopting AWS Lambda

Containing the Chaos! | A Three-Part Series Demonstrating the Usefulness of Containerization to HumanGov

Ansible is the Answer! | A Three-Part Series Demonstrating the Usefulness of Ansible to HumanGov