Backends
S3 Backend using Temporary Credentials
The Backend is configured using the backend
block. A config
resource holds the key configuration for the backend.
The Credentials for the backend are automatically read in via the AWS_
environment variables defined in credentials_config
.
The following needs to be defined in tf-be-config.yaml
resource definition:
.entity.driver_account
- Should be the ID of the Cloud Account that was configured..entity.driver_inputs.values.bucket
- Should be the ID of the S3 bucket..entity.driver_inputs.values.prefix
- Should be the prefix for state path.entity.driver_inputs.values.region
- The region that the bucket is in.
It is critical that the Identity defined in the driver account has access to the S3 bucket.
For example, using this policy document, replacing my-terraform-state-bucket
with your bucket ID:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:GetObject",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::my-terraform-state-bucket",
"arn:aws:s3:::my-terraform-state-bucket/*"
]
}
]
}
Resource Definitions
s3-backend.yaml
(
view on GitHub
)
:
# This Resource Definition uses an S3 bucket as the Terraform backend to store Terraform state
apiVersion: entity.humanitec.io/v1b1
kind: Definition
metadata:
id: example-terraform-s3-backend-s3
entity:
driver_account: ${resources.config#tf-backend.account}
driver_inputs:
values:
credentials_config:
environment:
AWS_ACCESS_KEY_ID: "AccessKeyId"
AWS_SECRET_ACCESS_KEY: "SecretAccessKey"
AWS_SESSION_TOKEN: "SessionToken"
script: |
terraform {
backend "s3" {
bucket = "${resources.config#tf-backend.outputs.bucket}"
key = "${resources.config#tf-backend.outputs.prefix}${context.app.id}/${context.env.id}/${context.res.type}.${context.res.class}/${context.res.id}"
region = "${resources.config#tf-backend.outputs.region}"
}
}
resource "random_id" "thing" {
byte_length = 8
}
output "bucket" {
value = "$\{random_id.thing.hex}"
}
driver_type: humanitec/terraform
name: s3-backend-example
type: s3
# Supply matching criteria
criteria: []
tf-be-config.yaml
(
view on GitHub
)
:
# This Resource Definition provides configuration for using an S3 bucket to store the Terraform state.
apiVersion: entity.humanitec.io/v1b1
kind: Definition
metadata:
id: tf-backend-config
entity:
criteria:
# This res_id is used in the resource reference in the s3-backend Resource Definition.
- res_id: tf-backend
driver_account: aws-ref-arch
driver_inputs:
values:
bucket: my-terraform-state-bucket
prefix: tf-state/
region: us-east-1
driver_type: humanitec/echo
name: tf-backend-config
type: config
s3-backend.tf
(
view on GitHub
)
:
resource "humanitec_resource_definition" "example-terraform-s3-backend-s3" {
driver_type = "humanitec/terraform"
id = "example-terraform-s3-backend-s3"
name = "s3-backend-example"
type = "s3"
driver_account = "$${resources.config#tf-backend.account}"
driver_inputs = {
values_string = jsonencode({
"credentials_config" = {
"environment" = {
"AWS_ACCESS_KEY_ID" = "AccessKeyId"
"AWS_SECRET_ACCESS_KEY" = "SecretAccessKey"
"AWS_SESSION_TOKEN" = "SessionToken"
}
}
"script" = <<END_OF_TEXT
terraform {
backend "s3" {
bucket = "$${resources.config#tf-backend.outputs.bucket}"
key = "$${resources.config#tf-backend.outputs.prefix}$${context.app.id}/$${context.env.id}/$${context.res.type}.$${context.res.class}/$${context.res.id}"
region = "$${resources.config#tf-backend.outputs.region}"
}
}
resource "random_id" "thing" {
byte_length = 8
}
output "bucket" {
value = "$\{random_id.thing.hex}"
}
END_OF_TEXT
})
}
}
tf-be-config.tf
(
view on GitHub
)
:
resource "humanitec_resource_definition" "tf-backend-config" {
driver_type = "humanitec/echo"
id = "tf-backend-config"
name = "tf-backend-config"
type = "config"
driver_account = "aws-ref-arch"
driver_inputs = {
values_string = jsonencode({
"bucket" = "my-terraform-state-bucket"
"prefix" = "tf-state/"
"region" = "us-east-1"
})
}
}
resource "humanitec_resource_definition_criteria" "tf-backend-config_criteria_0" {
resource_definition_id = resource.humanitec_resource_definition.tf-backend-config.id
res_id = "tf-backend"
}