_This issue was originally opened by @Joe6547654 as hashicorp/terraform#15305. It was migrated here as part of the provider split. The original body of the issue is below._
I currently have custom AWS AMI images and Terraform is not able to execute a script or inline code in the user_data section. It works for an AWS AMI image but not when I use my own.
0.9.6
data "template_file" "user_data" {
template = "${file("sample")}"
}
resource "aws_launch_configuration" "test" {
image_id = "ami-xxxxxx"
instance_type = "t2.medium"
security_groups = ["xxxxx", "xxxxxxx"]
key_name = "xxxxxxx"
}
resource "aws_instance" "ZC1" {
ami = "ami-xxxxxxx" #ZC AMI IMAGE
instance_type = "t2.medium"
subnet_id = "subnet-xxxxx"
private_ip = "x.x.x.x"
key_name = "xxxxxx"
vpc_security_group_ids = ["xxxxx", "xxxxxxx"]
user_data = "${data.template_file.user_data.rendered}"
tags {
"Name" = "Terraform Script Test"
}
}
The bash file that I have references in Terraform's code should run when the instance is created.
The instance got created (no errors) but when I check the instance to verify that the script executed, nothing is there.
Please list the steps required to reproduce the issue, for example:
When I change the AMI ID to a generic image from AWS, it works.
I had the same issue and I was thinking the same, after taking a look in /var/log/cloud-init-output.log, I realized that my script was wrong and that takes a delay in executing. I am using 0.9.11 version.
There is a discussion on stack overflow of two years ago https://stackoverflow.com/questions/27086639/user-data-scripts-is-not-running-on-my-custom-ami-but-working-in-standard-amazo
Also, I recommend going to launch configuration on aws console and take a look what is in userdata option, I was making a few mistakes there was well.
Worth noting that userdata isn't run more than once by default, so if you make a custom AMI you have to conceptually untick that box before shutting it down for AMI creation.
Edit: That's what the SO discussion is about!
In case others hit this snag...
We created a minimal hardened ami of centos7 that did not include cloud-init. I was trying the bash script approach and did not realize that user_data, no matter what approach you use, requires cloud-init installed.
@TinajaLabs thanks you just saved me from a wild goose chase..
Looking in /var/log/cloud-init-output.log saved me as well.
It would be quite useful if terraform could fail if the user_data script fails. That way we would be failing fast and hard.
Thank you for using Terraform and for opening up this question. It appears this question has more than one answer or workaround, so we will be closing it.
As a reminder, issues on GitHub are intended to be related to bugs or feature requests with the provider codebase. Please use our new community forum https://discuss.hashicorp.com/c/terraform-providers for community discussions, and questions around Terraform.
If you believe that your issue was miscategorized as a question or closed in error, please create a new issue using one of the following provided templates: bug report or feature request. Please make sure to provide us with the appropriate information so we can best determine how to assist with the given issue.
I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.
If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!
Most helpful comment
@TinajaLabs thanks you just saved me from a wild goose chase..