Using a variable for a file path

2,242 views
Skip to first unread message

Ben Sullivan

unread,
Aug 22, 2016, 7:03:58 AM8/22/16
to Terraform
Hi

I'm using the aws_s3_bucket_object resource:

resource "aws_s3_bucket_object" "distribution_zip" {
bucket = "veriluma-lambda-functions"
key = "mna-create-assessment.zip"
source = "${var.distribution_zip}"
etag = "${md5(file(var.distribution_zip))}"
}

I've defined the variable distribution_zip in a separate variables.tf file:

variable "distribution_zip" {}

and I'm passing in a value via:


terraform destroy -var distribution_zip=/the/path/to/my/zip/file

I'm getting this error:

Errors:

  * file: open : no such file or directory in:

${md5(file(var.distribution_zip))}
 
Is it possible to use a variable for a file path in this manner?

I am running terraform 0.7.1 on Ubuntu 16.04.

I've tried adding a single quote around the variable value on the command line which doesn't seem to work either.

If what I'm doing isn't supported, is there another workaround for parameterising the path of a file to send to the file function?  The filename is likely to change as incremental releases are published to our distribution zip.  The file function does not appear to support the double period to move to a directory above the path.cwd if I was to use a path variable.

Any tips would be greatly appreciated...

Many thanks

Ben

Jeremy Young

unread,
Aug 22, 2016, 3:42:20 PM8/22/16
to Terraform
Try this:

${md5(file("${var.distribution_zip}")))

Ben Sullivan

unread,
Aug 22, 2016, 4:48:05 PM8/22/16
to Terraform
Thanks Jeremy.  If I add the quotes I now get this:

Error loading config: Error parsing /home/verilumadev/work/mna-create-assessment/deploy/s3.tf: At 6:2: literal not terminated

Any ideas?

Jeremy Young

unread,
Aug 22, 2016, 7:11:58 PM8/22/16
to Terraform
It's hard to say without seeing the file s3.tf .  Mind sharing that file?

Ben Sullivan

unread,
Aug 22, 2016, 8:12:12 PM8/22/16
to terrafo...@googlegroups.com
Yeah it's what I put in the original post - just the was_s3_bucket_object resource…

I've tried using a template to get around this with no success either…

Thanks so much for your help!
--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to a topic in the Google Groups "Terraform" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/terraform-tool/B5j2XswL9_M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/2d0ed5c2-ca88-4a1e-b0ca-e92a9484ed9b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeremy Young

unread,
Aug 23, 2016, 8:21:51 AM8/23/16
to Terraform
Sorry Ben, I missed a typo in my original comment.  

Instead of this, which I can use to reproduce the "literal not terminated message":
"${md5(file("${var.distribution_zip}")))"  ## Note 3 parentheses, not 2 and a }

replace the last parentheses with a curly brace.
"${md5(file("${var.distribution_zip}"))}"


I can produce a successful plan when I use the correct syntax :-)

Ben Sullivan

unread,
Aug 23, 2016, 3:58:40 PM8/23/16
to Terraform
Thanks Jeremy

I've tried this and I still get the same error:


Errors:


  * file: open : no such file or directory in:

${md5(file("${var.distribution_zip}"))}

I'm running terraform from the directory containing my tf files, like this:

terraform plan -state=development.tfstate -var distribution_zip=/home/verilumadev/work/mna-create-assessment/build/distributions/mna-create-assessment-0.1.0-SNAPSHOT.zip -var stage=development

I'm wondering if there's a problem with having slashes in a -var value?

These don't appear to work either (same error):

terraform plan -state=development.tfstate -var 'distribution_zip=/home/verilumadev/work/mna-create-assessment/build/distributions/mna-create-assessment-0.1.0-SNAPSHOT.zip' -var stage=development

terraform plan -state=development.tfstate -var distribution_zip='/home/verilumadev/work/mna-create-assessment/build/distributions/mna-create-assessment-0.1.0-SNAPSHOT.zip' -var stage=development

What command did you run to get your interpolation to function correctly?

Thanks

Ben

Jeremy Young

unread,
Aug 23, 2016, 5:01:19 PM8/23/16
to Terraform
It looks like the fully-qualified path is creating this error.  

Following these steps I can create a successful plan:
[jyoung@hostname testing]$ cat s3.tf 
resource "aws_s3_bucket_object" "distribution_zip" {
  bucket = "veriluma-lambda-functions"
  key = "mna-create-assessment.zip"
  source = "${var.distribution_zip}"
  etag = "${md5(file("${var.distribution_zip}"))}"
}

[jyoung@hostname testing]$ cat vars.tf 
variable "distribution_zip" {}


[jyoung@hostname testing]$ terraform -version
Terraform v0.7.1


[jyoung@hostname testing]$ AWS_PROFILE=account.profile terraform plan -var distribution_zip=testing.zip
provider.aws.region
  The region where AWS operations will take place. Examples
  are us-east-1, us-west-2, etc.

  Default: us-east-1
  Enter a value: [PRESSED ENTER TO ACCEPT DEFAULT]

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but
will not be persisted to local or remote state storage.


The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed. Cyan entries are data sources to be read.

Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.

+ aws_s3_bucket_object.distribution_zip
    acl:           "private"
    bucket:        "veriluma-lambda-functions"
    content_type:  "<computed>"
    etag:          "6b3d110594e3ebe5fa2cc22363cd2b02"
    key:           "mna-create-assessment.zip"
    source:        "testing.zip"
    storage_class: "<computed>"
    version_id:    "<computed>"


Plan: 1 to add, 0 to change, 0 to destroy.


However, when doing something like this, I get the error you're receiving.  
terraform plan -var distribution_zip=$( pwd )/testing.zip

OR

terraform plan -var distribution_zip="$( pwd )/testing.zip"
Reply all
Reply to author
Forward
0 new messages