AWS User Data | How to run commands on startup Amazon Linux

Userdata on AWS allows you to run commands on start-up when you launch an EC2 instance on AWS. This is known as boot strapping your instance. In this example we will be boot strapping Amazon Linux with the Apache web server.

Step 1

First log into you AWS portal and then click services and EC2

Step 2

Next Click launch instance.

Step 3

Select Amazon Linux

Step 4

Leave the instance as mirco to get the free tier and click next Configure instance details

Step 5

Leave the defaults and scroll down to advanced details and expand it – enter the following lines into the textbox:

yum install httpd -y
systemctl start httpd
systemctl stop firewalld
cd /var/www/html
echo "this is my test site" > index.html

The text in the code block above installs apache and adds an simple webpage that reads this is my test site. Finally click Next add storage

Step 6

Leave the defaults and click next add tags

Step 7

Give your machine a name and click Next Configure Security Group.

Step 8

Allow port 80 from on the security group and click review and launch

Step 9

Click Launch

Step 10

You can proceed without a keypair as we will not log into this instance

Step 11

click your instance ID

Step 12

Wait until your instance is running and then copy it’s pubic IP or dns name

Step 13

Open a new browser and enter the pubic IP of your Linux instance into the address bar. You should now see the simple webpage we created.

A full video tutorial can be found here:

2 thoughts on “AWS User Data | How to run commands on startup Amazon Linux

  1. Pingback: AWS User Data | How to run commands on startup Amazon Linux – Linux directory

  2. Emmanuel Brefo

    Hello Sir,
    Thanks for this opportunity, I want to ask a question about the user data. I created one during the launching of my Instance, but I made and after laughing the Instance I did not get my desire result. I saw apache as I opened my public IP in the browser… I checked through the data I entered only to find out that I made a mistake. I’ve corrected the mistake but the browser keeps opening the Apache test page.
    Please can You help me out, I don’t know how to set it to start each time I start my Instance.
    Thank You.


Leave a Reply

Your email address will not be published. Required fields are marked *