2

I'm trying to pass user data to my EC2 instance at the time of creation using AWS CLI run-instances command.

I placed the below command in a file:

#!/bin/bash
sudo su
yum update -y
yum install httpd -y
systemctl start httpd
systemctl enable httpd
echo "<html><h1>Welcome to the server</h1><html>" >> /var/www/html/index.html

All the AWS CLI command I ran(with .txt and without):

  1. aws ec2 run-instances --image-id ami-062f7200baf2fa504 --count 1 --instance-type t2.micro --key-name lab115key --security-groups my-sg --region us-east-1 --user-data file://./text1.txt

  2. aws ec2 run-instances --image-id ami-062f7200baf2fa504 --count 1 --instance-type t2.micro --key-name lab115key --security-groups my-sg --region us-east-1 --user-data file://./text1

  3. aws ec2 run-instances --image-id ami-062f7200baf2fa504 --count 1 --instance-type t2.micro --key-name lab115key --security-groups my-sg --region us-east-1 --user-data text1.txt

  4. aws ec2 run-instances --image-id ami-062f7200baf2fa504 --count 1 --instance-type t2.micro --key-name lab115key --security-groups my-sg --region us-east-1 --user-data text1

Then I tried to encode the script to Base64 still the User data is not accepted.

I created a Security group with port 80 and 22 with source 0.0.0.0/0 using Default VPC and Subnet.

AMI: Amazon Linux 2 - t2.micro

Region: us-east-1

ISSUE:

  1. HTTPD is not getting installed.
  2. index.html is not getting created in the path.

Am I doing something wrong? Why is it not working?

Any other way to solve this issue?


New Updates on this issue:

The User data that I passed to the run-instances command is getting installed but its taking too much of time.

Not all the time its getting installed. I ran the code five times, only 3 EC2 got User data installed.

From AWS Status Webpage: I found that us-east-1 Is having lots of latency issues.

4
  • 1
    Is the instance in a public subnet so that it can download files from the Internet? Have you looked in /var/log/cloud-init-output.log to confirm that the script is being executed and to check for errors? Commented Feb 13, 2020 at 10:33
  • I agree with @JohnRotenstein, check the log files. I was unable to recreate the issue you were having. Are you sure the default subnet it's being installed in has a route outside your VPC? Can you run these commands manually? Commented Feb 13, 2020 at 15:43
  • @JohnRotenstein Yes, the instance is placed in a public subnet. Commented Feb 14, 2020 at 3:50
  • @kenlukas, Im able to SSH into that instance and run each command manually. Commented Feb 14, 2020 at 3:51

1 Answer 1

2

Commands provided via User Data are executed as the foot user.

Therefore, do not use sudo commands within User Data scripts.

Any errors from executing a User Data script on Amazon Linux can be found in:

/var/log/cloud-init-output.log

If you are not sure whether the script is being executed, add a line like this after the header line:

echo Script started > /tmp/script.log

You can then check the contents of /tmp/script.log to determine whether the script was executed.

Sign up to request clarification or add additional context in comments.

1 Comment

I was able to run my script through AWS CLI with sudo but its taking too much of time to run the command. At first I thought the code is not getting executed but it takes time. Now its working properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.