Terraform
This walkthrough shows you how to pass CloudTruth parameters as Environment Variables to Terraform.
You can also use CloudTruth to externally reference backend tfstate output as an external parameter.

Prerequisites

Terraform deploy with CloudTruth

For this example we will build on top of the Terraform AWS tutorial to deploy an EC2 instance defining variables with CloudTruth parameters. You can use any target for your deploys.

Set CloudTruth Terraform Variables

Create a CloudTruth Project called Terraform.
1
cloudtruth project set Terraform
Copied!
Now you can create the CloudTruth parameters in the Terraform project that we will pass to Terraform as environment input variables with the TF_VAR_ convention.
1
cloudtruth --project Terraform parameter set TF_VAR_ami -v ami-830c94e3
2
cloudtruth --project Terraform parameter set TF_VAR_instance_type -v t2.micro
3
cloudtruth --project Terraform parameter set TF_VAR_availability_zone_names -v \[\"us-west-2a\",\ \"us-west-2b\"\]
4
cloudtruth --project Terraform parameter set TF_VAR_resource_tags -v \{\"Name\":\"Cloudtruth-Instance\",\"project\":\"CloudTruth\ Run\ Terraform\",\"environment\":\"default\"\}
Copied!

Create Terraform Configuration

From your Terraform working directory update your main.tf with the following configuration.
1
terraform {
2
required_providers {
3
aws = {
4
source = "hashicorp/aws"
5
version = "~> 3.27"
6
}
7
}
8
}
9
10
provider "aws" {
11
profile = "default"
12
region = "us-west-2"
13
}
14
15
resource "aws_instance" "cloudtruth" {
16
ami = var.ami
17
instance_type = var.instance_type
18
availability_zone = var.availability_zone_names[0]
19
20
tags = var.resource_tags
21
}
22
23
output "zone" {
24
value = aws_instance.cloudtruth.availability_zone
25
}
26
27
output "AMI" {
28
value = aws_instance.cloudtruth.ami
29
}
30
31
output "instance_type" {
32
value = aws_instance.cloudtruth.instance_type
33
}
34
35
output "instance_name" {
36
value = aws_instance.cloudtruth.tags.Name
37
}
38
39
#########################
40
####### VARIABLES #######
41
#########################
42
43
variable "ami" {
44
description = "Value of the Amazon Machine Image"
45
type = string
46
}
47
48
variable "instance_type" {
49
description = "Value of the Instance Type"
50
type = string
51
}
52
53
variable "availability_zone_names"{
54
description = "List of available regions"
55
type = list(string)
56
}
57
58
variable "resource_tags" {
59
description = "Tags to set for all resources"
60
type = map(string)
61
}
Copied!
This main.tf will deploy an EC2 instance to us-west-2 and defines four variables that are centrally managed by CloudTruth. It also shows you how to use various Terraform variable types such as a map and list.
Variable
Type
CloudTruth Value
ami
string
ami-830c94e3
instance_type
string
t2.micro
availability_zone_names
list
["us-west-2a", "us-west-2b"]
resource_tags
map
{"Name":"Cloudtruth-Instance","project":"CloudTruth Run Terraform","environment":"default"}

Build Terraform Infrastructure

From your Terraform working directory initialize the infrastructure.
1
terraform init
Copied!
Now when executing a terraform plan you will notice that you are prompted to enter values for each variable defined.
1
terraform plan
2
var.ami
3
Value of the Amazon Machine Image
4
5
Enter a value:
Copied!
CloudTruth Run
You can manage your Terraform configuration centrally with CloudTruth and pass Terraform environment input variables with the cloudtruth run CLI command.
The following command will pass the variables from the Terraform project where we set our input variables directly into terraform apply.
1
cloudtruth --project Terraform run -- terraform apply
Copied!
Terraform will show the outputs and deploy the instance with our CloudTruth values.
1
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
2
3
Outputs:
4
5
AMI = "ami-830c94e3"
6
instance_name = "Cloudtruth-Instance"
7
instance_type = "t2.micro"
8
zone = "us-west-2a"
Copied!
Couple this method with CloudTruth Environments and you can manage and deploy different instance types with various configuration setting to your Prod, Dev and Staging with a single command.
Terraform Input Variables
CloudTruth run injects environment variables into your terraform commands. Terraform searches the environment for variables that contain TF_VAR_ preceding the variable name defined in your configuration script. The variable is case sensitive so if it is lower case in main.tf the variables must have lower case key names in CloudTruth.
Environment input variables have a specific order of precedence and CloudTruth run can be used with any combination that Terraform allows. Values from CloudTruth run be overridden by a file if the same variable exists in a terraform.tfvars or any of the higher precedence methods of passing values.
Terraform loads variables in the following order, with later sources taking precedence over earlier ones:
    Environment variables
    The terraform.tfvars file, if present.
    The terraform.tfvars.json file, if present.
    Any *.auto.tfvars or *.auto.tfvars.json files, processed in lexical order of their filenames.
    Any -var and -var-file options on the command line, in the order they are provided. (This includes variables set by a Terraform Cloud workspace.)
Last modified 24d ago