I have provided an .sh file as user_data in my Terraform script:
resource "aws_instance" "alpha-terraform"{
ami=var.amis[var.region]
instance_type="t2.micro"
vpc_security_group_ids=[aws_security_group.alpha-terraform-sg.id]
user_data = file("user_data_bootstrap.sh")
key_name = "alpha-terraform"
tags={
type="alpha"
purpose="terraform"
}
}
The .sh file contains:
#Jenkins Installation:
sudo apt update
sudo apt install openjdk-8-jdk --yes
wget –q –O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add –
sudo sh -c 'echo deb https://pkg.jenkins.io/debian binary/ >> /etc/apt/sources.list'
sudo apt update
sudo apt install Jenkins
sudo systemctl status jenkins
find /usr/lib/jvm/java-1.8* | head -n 3
My instance is getting created all right. However, I believe my user_data is not getting executed. I can see the user_data is being copied to /var/lib/cloud/instance/ as user-data.txt and user-data.txt.i but still not getting executed.
Why is this happening and how can I debug it further?
Update
I tried accessing the server through and executing each command one by one but getting following error while updating apt packages:
ubuntu@ip-172-31-52-**:~$ apt update
Reading package lists... Done
W: chmod 0700 of directory /var/lib/apt/lists/partial failed - SetupAPTPartialDirectory (1: Operation not permitted)
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)
ubuntu@ip-172-31-52-**:~$ sudo apt update
Reading package lists... Done
E: Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)
E: Unable to lock directory /var/lib/apt/lists/

/var/log/cloud-init-ouput.logUbuntu 16.04.6 LTS (Xenial). Nothing unusual in logs except for device, route and key info and below at the end:WARNING: apt does not have a stable CLI interface. Use with caution in scripts. Err:1 http://security.ubuntu.com/ubuntu xenial-security InRelease Cannot initiate the connection to security.ubuntu.com:80aptitself, so its presence confirms that your script is being executed, but that it isn't succeeding to do what you intended. When you tried to reproduce the script from the command line you ran it as userubuntu, which has different access thancloud-initdoes; for a more realistic reproduction, usesudo apt updateto runaptwith superuser permissions.httpandhttpsaddresses using a command likecurl. If no host on the internet is accessible, I'd suggest starting a new question about configuring outbound internet access for an EC2 instance, and include in that question the configuration for your VPC(s), subnet(s), and security group(s) too.