In my user-data, I have:
export INSTANCEID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
And Terraform:
resource "aws_launch_template" "launch_template" {
user_data = base64encode(
templatefile(
"${path.module}/user_data/user_data.sh.tpl",
{
environment = var.environment
}
)
)
In result, I'm getting error:
Invalid value for "vars" parameter: vars map does not contain key "INSTANCEID", referenced at ./user_data/user_data.sh.tpl
As we see, INSTANCEID should be set at launch time on EC2 side, but Terraform is trying to substitute this variable on the moment of plan. How can solve it?
CodePudding user response:
There are 2 solutions:
- Escape that variable in shell script using an extra
$before it. For example,INSTANCEID=$${INSTANCEID} - Add that variable as
INSTANCEID = "$INSTANCEID"under the list of variables for file in terraform script and it will replace${INSTANCEID}with$INSTANCEIDand the script will still work.
