Reputation: 6960
I've the file structure below, terraform CLI is not using the runtime variable value in S3 file's content. It's always defaulting to 0 value.
Structure:
terraform
main.tf
api/
variables.tf
s3.tf
main.tf
module "api" {
source = "./api"
providers = {
aws = "aws.us-east-1"
}
}
variables.tf
variable "build-number" {
description = "jenkins build number"
type = "string"
default = "0"
}
s3.tf
resource "aws_s3_bucket_object" "api-build-version" {
bucket = "api-code"
key = "build-version"
content = "${var.build-number}"
etag = "${md5("${var.build-number}")}"
}
terraform plan -var "build-number=2" -target aws_s3_bucket_object.api-build-version
My Terraform version is v0.11.8
Upvotes: 1
Views: 1134
Reputation: 17594
In your structure, your variable is only under the API module
terraform
main.tf
api/
variables.tf
s3.tf
You need that same variable at the root, add it to the main.tf or own file and then pass it on like this:
module "api" {
source = "./api"
build-number = "${var.build-number}"
providers = {
aws = "aws.us-east-1"
}
}
All the sub-folders are modules in terraform, variables are not global... you do to declare variables on every level, and if they are identical you can do a symlink to one file to reduce copy-paste.
If it was just a flat structure you would not need that,
try this if you want:
variable "build-number" {
type = "string"
default = "0"
}
output "test" {
value = "${var.build-number}"
}
terraform apply -var "build-number=2"
That outputs the correct variable value
Upvotes: 2