Terraform 1.4: how to to Pass Environment Variables to EC2 User Data Script
I'm testing a new approach and I'm relatively new to this, so bear with me... I'm sure I'm missing something obvious here, but I'm currently using Terraform version 1.4.0 to provision an EC2 instance on AWS, and I'm trying to pass environment variables into the user data script... However, I'm working with issues where the environment variables don't seem to be available when the instance boots up. Hereโs the relevant part of my Terraform configuration: ```hcl resource "aws_instance" "example" { ami = "ami-0abcdef1234567890" instance_type = "t2.micro" user_data = <<-EOF #!/bin/bash export MY_VAR=${var.my_var} echo "Environment variable MY_VAR is set to: $MY_VAR" EOF tags = { Name = "ExampleInstance" } } variable "my_var" { description = "An example environment variable" default = "Hello, World!" } ``` When I SSH into the instance after it's launched and check the value of `MY_VAR`, itโs not set. I also looked at the instance's user data log in `/var/log/cloud-init-output.log` and saw no errors, but it seems the variable wasn't exported as expected. I've tried changing the script to use `set` instead of `export`, and I've also attempted to directly echo the value without exporting, but nothing appears to solve the question. Iโd appreciate any insights on why the environment variable is not being recognized in the user data script, and if thereโs a recommended approach to ensure that it gets set correctly. Thank you! My development environment is Windows. What am I doing wrong? This is part of a larger service I'm building. Am I missing something obvious? Thanks for any help you can provide!