I am trying to deploy ECS using Terraform and it's throwing the above error while I do terraform apply. Please help me out.
Here is the code chunk from main.tf file
resource "null_resource" "push" {
provisioner "local-exec" {
command = <<EOF
aws ecr get-login-password --region "$region" | docker login --username AWS --password-stdin "$ecr_fqdn"
docker tag "servian/techchallengeapp:latest" "$repository"
docker push "$repository:latest"
docker logout "$ecr_fqdn"
EOF
environment = {
ecr_fqdn = regex("^([^/]*)/", aws_ecr_repository.ecs.repository_url).0
region = var.region
repository = aws_ecr_repository.ecs.repository_url
}
}
depends_on = [aws_ecr_repository.ecs]
}
The code chunk from the variable.tf file
variable "region" {
description = "Region to deploy app"
default = "ap-southeast-2"
}
Please find the error in the attached image
CodePudding user response:
A ${ ... }
sequence is an interpolation in Terraform's configuration language, which evaluates the expression given between the markers. Unix shells typically use $..
As per the official documentation, the command
is evaluated in a shell, and can use environment variables or Terraform variables. So, basically, what you are trying to achieve should work fundamentally.
Now, let's look at the error:
Provided region_name '"$region"' doesn't match a supported format."
Please remove double quotes from the environment variables and try again.
If it fails, then please set TF_LOG
to log level TRACE
and try again. Also, please paste the error directly instead of a screenshot.
Good luck!
CodePudding user response:
Try first to add an echo $region
, to understand what value region was assigned in your environment
section.
As seen in this discussion, the command itself might need to use '$region'
instead of "$region"
, in order to avoid a variable expansion too soon (ie when defining command
, as opposed to when executing it)