Skip to content

Instantly share code, notes, and snippets.

@ChristophShyper
Last active November 13, 2020 17:51
Show Gist options
  • Save ChristophShyper/b5e2860675aeb8463675ec3f3bf2cbd4 to your computer and use it in GitHub Desktop.
Save ChristophShyper/b5e2860675aeb8463675ec3f3bf2cbd4 to your computer and use it in GitHub Desktop.
Pass metadata between Terraform modules using S3. Much faster than remote state.

Save outputs from a resource to a YAML file:

# Module Alpha
# https://www.terraform.io/docs/providers/aws/r/s3_bucket_object.html
resource "aws_s3_bucket_object" "outputs" {
  bucket = local.metadata_bucket
  key    = "${local.key_path}/organization.yml"
  content = yamlencode({
    "organization_arn" : aws_organizations_organization.default.arn
    "organization_id" : aws_organizations_organization.default.id
  })
  acl                    = "bucket-owner-full-control"
  content_type           = "text/plain"
  server_side_encryption = "aws:kms"
}

Read the YAML file in the second module

# Module Bravo
# https://www.terraform.io/docs/providers/aws/d/s3_bucket_object.html
data "aws_s3_bucket_object" "organization" {
  bucket = local.metadata_bucket
  key    = local.organization_root_metafile
}

Use data from the file as a value for other resources

organization_arn = yamldecode(data.aws_s3_bucket_object.organization.body).organization_arn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment