AWS user-data is not getting executed by terraform

6,945 views
Skip to first unread message

Bubunia Patra

unread,
Apr 21, 2018, 6:30:45 AM4/21/18
to Terraform
Hi all,

I have created the AMI through packer and launching the AMI through terraform. I have also user-data which runs during the bootstrap of the EC2 instance. I noticed that user-data contents is not getting called from terraform as when I created the file inside the user-data script to add the debug logs I noticed the file is not created nor I added some statements i could not see anything it. In the /var/log/cloud-init.log there is no data.
Any idea how can I debug and fix it? 

Regards
Pradeep

Brian Lalor

unread,
Apr 21, 2018, 7:05:09 AM4/21/18
to terrafo...@googlegroups.com
Can you provide your Terraform config?  Or a representative sample?  Also check /var/lib/cloud/instance (I think…) where you should see what user data, if any, is getting onto the instance.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/5714c6f1-bef9-426f-87b0-eaeb6d178cef%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

— 
Brian Lalor

Bill Anderson

unread,
Apr 23, 2018, 2:07:14 AM4/23/18
to Terraform
Terraform doesn't run your user-data script, cloud-init does. it may seem inconsequential, but the distinction is important (after all, your cloud init config could skip user data scripts). You mention you build the AMI with Packer, is there where you are installing the user-data script? If so you'll probably be better off on the Packer list (and the answer will likely involve running Packer in debug mode so you can step through the sequence and verify each step to see why it doesn't get added to the AMI). If not, the terraform config will be crucial for us to help.

Cheers,
Bill

Michael Smith

unread,
Apr 24, 2018, 10:06:55 AM4/24/18
to Terraform
Does your AMI install python 3.6?  I ran into a similar issue with an AMI I created.  I didn't realize cloud init depended on python.

shiva dontu

unread,
Apr 4, 2019, 8:10:04 PM4/4/19
to Terraform
As per my experience with Userdata and AMIs,  Userdata will be executed by ec2-user, and i am assuming to run/install software, Ec2-user mush have sudo permissions.

If you your AMI is created from AWS AMIs, Ec2-user has full permissions including sudo. so User data will be executed
If your AMI is a snapshot of the machine ( lift and Shifted from On-Prem), Ec2-user will not be available, So make sure Ec2-user is setup with sudo permissions.

Hope this resolve the issue.


Also Please let me know if any of you were able to resolve the issue, if yes, could you please suggest me how this issue was resolved.
-Shiva

hmun...@gmail.com

unread,
Apr 5, 2019, 9:38:40 AM4/5/19
to Terraform
Hi Pradeep, 

Declare userdata template as data source and use it as user_data in your ec2 instance resource or autoscaling resources. then your instance will use the bootstrap the userdata. Hope this helps 

declare userdata as data source
-------------------------------------------

data "template_file" "user_data" {
  template = "${file("userdata.tpl")}"
}


under ec2 or autoscaling resource 
----------------------------------------------
user_data    =  "${data.template_file.user_data.rendered}"



Thank you 
--Hima 
Reply all
Reply to author
Forward
0 new messages