0
votes

Terraform 0.9.2, AWS, OS X 10.12.5 main.tf, shared .tfstate in a S3 bucket:

#=================
# Terraform
#=================
terraform {
  backend "s3" {
    bucket     = "com.whd-design.terraform-s3-state-bucket"
    key        = "tfState/htmt-book"
    region     = "eu-west-2"
    encrypt    = true
    lock_table = "terraform-state-lock"
  }
}

This works fine, the state is written to the bucket (and I can see successive versions as changes are made), and a DynamoDB record is created. However trying to use the dot-notation bucket name in output causes an error:

output "State_S3_Bucket_ARN" {
  value = "${aws_s3_bucket.com.whd-design.terraform-s3-state-bucket.arn}"
}

* output 'State_S3_Bucket_ARN': unknown resource 'aws_s3_bucket.com' referenced in variable aws_s3_bucket.com.whd-design.terraform-s3-state-bucket.arn

Other output statements without dots in the name work fine - is it just that dot format cannot be used? (Odd as it works to actually create & use the S3 object). Is there some escaping that can be used around the resource name?

Many Thanks

1

1 Answers

0
votes

OK I was trying to reference an object that did not exist in the terraform state. It existed on AWS (Created by a different TF plan). Solution:

  1. Import it terraform import aws_s3_bucket.terraform_s3_state_bucket com.whd-design.terraform-s3-state-bucket using a valid TF resource name
  2. Add the definition, copied from another .TF file
  3. Correct the output to use the TF resource name not the AWS resource name.
  4. Make sure you do step 2 before terraform apply, it tries to destroy the 'orphan' S3!

All sorted.