Terraform Remote State Files
Link CloudTruth external parameter values to Terraform Remote State Files within S3.
One practical use of CloudTruth external values is the ability to automatically reference Terraform state outputs. These outputs are normally generated and stored by Terraform in a file named terraform.tfstate.HashiCorp provides the ability for Terraform state files to be stored remotely. AWS S3 is a common choice for backend remote storage, and is often considered a best practice, especially when working with multiple users that may depend upon Terraform state outputs for CI/CD.
Once CloudTruth external parameters referencing state files have been created, any parameter access will fetch the the value directly from the latest Terraform state on-demand! This includes parameter references from within the Template mechanism that enables you to generate configuration files per-environment as needed.

Remote State with Terraform

You may already have remote state stored in S3 which can be used directly with the CloudTruth AWS S3 Integration to assign state output as external values.
This walkthrough shows you how to setup a basic S3 bucket and deploy an EC2 instance to us-west-2 with a Terraform remote state file backend in S3 and reference the output as an external CloudTruth parameter value.

Deploy an S3 bucket with Terraform

You can use an existing S3 bucket for your remote backend or deploy a new S3 bucket with a unique name.
Create an s3 directory, change into the directory and create a file to define the S3 infrastructure.
1
mkdir s3
2
cd s3
3
touch main.tf
Copied!
Open main.tf , paste in the configuration below, save the file, and run terraform init and terraform apply.
1
provider "aws" {
2
profile = "default"
3
region = "us-west-2"
4
}
5
6
resource "random_pet" "petname" {
7
length = 1
8
separator = "-"
9
}
10
11
resource "aws_s3_bucket" "demo" {
12
bucket = "cloudtruth-s3-${random_pet.petname.id}"
13
acl = "private"
14
force_destroy = true
15
}
16
17
output "YOUR_UNIQUE_BUCKET_NAME" {
18
value = aws_s3_bucket.demo.bucket
19
}
Copied!
Note the output name of your newly created S3 bucket to use in the terraform s3 backend configuration.

Deploy configuration to a Terraform S3 backend

Create a remote-state directory, change into the directory and create a file to define the infrastructure.
1
mkdir remote-state
2
cd remote-state
3
touch main.tf
Copied!
The following Terraform configuration specifies S3 as the backend for your state file. Open main.tf and paste in the configuration below. Provide the bucket with YOUR_UNIQUE_BUCKET_NAME, save the file, then run terraform init and terraform apply.
The EC2 instance will be created in us-west-2. Terraform will place the state file in the specified S3 bucket path demo/instance/terraform.tfstate and provide outputs for the instance id and public ip.
1
terraform {
2
backend "s3" {
3
bucket = "YOUR_UNIQUE_BUCKET_NAME"
4
key = "demo/instance/terraform.tfstate"
5
region = "us-west-2"
6
}
7
}
8
9
provider "aws" {
10
profile = "default"
11
region = "us-west-2"
12
}
13
14
resource "aws_instance" "app_server" {
15
ami = "ami-830c94e3"
16
instance_type = "t2.micro"
17
18
tags = {
19
Name = "RemoteStateInstance"
20
}
21
}
22
23
output "instance_id" {
24
description = "ID of the EC2 instance"
25
value = aws_instance.app_server.id
26
}
27
28
output "instance_public_ip" {
29
description = "Public IP address of the EC2 instance"
30
value = aws_instance.app_server.public_ip
31
}
Copied!
Now that you have a remote state file in S3 and have configured the AWS S3 CloudTruth Integration, you can set your terraform.tfstate outputs as CloudTruth External Values.
Set state as an External Value
As an example, while editing the External value of a parameter navigate to the S3 bucket destination path demo/instance/terraform.tfstate in the S3 integration and use the JMESPATH selector outputs.instance_id.value. Hit save to set the value to the instance id output from the remote state file.
Don't forget to cleanup your deploy infrastructure by executing a terraform destroy in the S3 and remote-state folders.

Remote State with Terragrunt

Terragrunt provides a quick start on creating a DRY remote_state configuration for the backend. You can use Terragrunt generated remote state with the CloudTruth AWS S3 Integration to assign state output as external values.
Following the Terragrunt quick start guide as an example we created the following folder structure which will configure an S3 bucket and deploy an instance with remote state using the deployed bucket.
1
# terragrunt-remote-state
2
├── stage
3
│ ├── instance
4
│ │ └── main.tf
5
│ │ └── terragrunt.hcl
6
│ └── terragrunt.hcl
Copied!
The backend configuration is defined once in the root stage terragrunt.hcl file. It will create a dynamodb lock table called my-lock-table and an S3 backend. You can update the bucket config with a unique name to follow along.
1
# stage/terragrunt.hcl
2
remote_state {
3
backend = "s3"
4
generate = {
5
path = "backend.tf"
6
if_exists = "overwrite_terragrunt"
7
}
8
config = {
9
bucket = "YOUR_UNIQUE_BUCKET_NAME"
10
11
key = "${path_relative_to_include()}/terraform.tfstate"
12
region = "us-east-1"
13
encrypt = true
14
dynamodb_table = "my-lock-table"
15
}
16
}
Copied!
The instance folder contains a terragrunt.hcl that contains the Terragrunt helper find_in_parent_folders() which will inherit the remote_state configuration from the root terragrunt.hcl file in the directory tree.
1
# stage/instance/terragrunt.hcl
2
include {
3
path = find_in_parent_folders()
4
}
Copied!
The instance folder also contains a main.tf that will configure an EC2 instance in us-east-1 with outputs for the instance_id and public ip.
1
# stage/instance/main.tf
2
provider "aws" {
3
profile = "default"
4
region = "us-east-1"
5
}
6
7
resource "aws_instance" "app_server" {
8
ami = "ami-0aeeebd8d2ab47354"
9
instance_type = "t2.micro"
10
11
tags = {
12
Name = "RemoteStateInstance"
13
}
14
}
15
16
output "instance_id" {
17
description = "ID of the EC2 instance"
18
value = aws_instance.app_server.id
19
}
20
21
output "instance_public_ip" {
22
description = "Public IP address of the EC2 instance"
23
value = aws_instance.app_server.public_ip
24
}
Copied!
Now you can change directory to the instance folder and run terragrunt apply.
1
cd stage/instance
2
terragrunt apply -auto-approve --terragrunt-non-interactive
Copied!
Terragrunt creates an S3 bucket with your unique name and is a destination for the instance configuration terraform.tfstate file!
Now that you have a remote state file in S3 and have configured the AWS S3 CloudTruth Integration, you can set your terraform.tfstate outputs as CloudTruth External Values.
Set state as an External Value
while editing the external value of a parameter navigate to the Terragrunt created S3 bucket destination path instance/terraform.tfstate in the S3 integration and use the JMESPATH selector outputs.instance_id.value. Hit save to set the value to the instance id output from the remote state file.
Don't forget to cleanup your deploy infrastructure by executing a terragrunt destroy in the instance folder. You will need to manually delete the Terragrunt created S3 bucket as the wrapper does not provide a way to delete generated backends.
Last modified 2mo ago