Terraform: Can terraform save hash of S3 object in tfstate?

Created on 18 Apr 2016  ·  4Comments  ·  Source: hashicorp/terraform

Hi there,

I'm trying to make terraform to detect local changes to file that was used to create S3 object - so in case local file is updated then terraform should create new version of S3 object from it.

Here's what I'm talking about:

resource "aws_s3_bucket" "s3_bucket" {
  bucket = "anosulchik-s3-test"
  acl = "private"
}

resource "aws_s3_bucket_object" "s3_object" {
  bucket = "${aws_s3_bucket.s3_bucket.bucket}"
  key = "s3_file"
  source = "local_file"
}

After first 'terraform apply' S3 bucket and object in it are created like a charm. But once I change contents of local_file and do 'terraform apply' then no changes are made to S3 object:

$ terraform apply
aws_s3_bucket.s3_bucket: Refreshing state... (ID: anosulchik-s3-test)
aws_s3_bucket_object.s3_object: Refreshing state... (ID: s3_file)

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Is there any options to make terraform to store hash for local file that is used to create S3 object, and in case that hash changes (file is updated) then terraform will create new version of S3 object too.

Thank you!!!

P.S. I'm using terraform 0.6.14. I can workaround this by tainting S3 object but it's what I'm looking for.

provideaws question

Most helpful comment

Hey @anosulchik – you can set the etag attribute on the bucket object:

So, it would look like this:

resource "aws_s3_bucket_object" "s3_object" {
  bucket = "${aws_s3_bucket.s3_bucket.bucket}"
  key = "s3_file"
  source = "local_file"
  etag = "${md5(file("path/to/local_file"))}"
}

That will calculate an MD5 of the object and detect changes.

Let me know if you have any other questions!

All 4 comments

Hey @anosulchik – you can set the etag attribute on the bucket object:

So, it would look like this:

resource "aws_s3_bucket_object" "s3_object" {
  bucket = "${aws_s3_bucket.s3_bucket.bucket}"
  key = "s3_file"
  source = "local_file"
  etag = "${md5(file("path/to/local_file"))}"
}

That will calculate an MD5 of the object and detect changes.

Let me know if you have any other questions!

Thanks @catsby !

@anosulchik
While this works ... there are some caveats - see https://github.com/terraform-providers/terraform-provider-aws/issues/6668

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings