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

Published: March 18, 2019 (Updated: Mar 18, 2019)

Enjoying this content? Subscribe to the Channel!

Automate Your Setup: How to Bootstrap an AWS EC2 Linux Instance with Apache Userdata


Hello and welcome to Darren’s Tech Tutorials! I’m Darren O’Neill, and today we’re diving into one of the most powerful automation features on Amazon Web Services: EC2 Userdata.

If you’ve ever launched an EC2 instance only to spend the next 15 minutes manually logging in, installing software, and setting up configurations, you know how repetitive that process can be. The solution? Bootstrapping—using a script run automatically on first boot.

In this tutorial, we will show you exactly how to launch an Amazon Linux EC2 instance that automatically installs and starts the Apache web server, and even deploys a simple test website, all via Userdata.

What is EC2 Userdata and Why Use It?

Userdata is a crucial feature that allows you to pass commands, usually a shell script, to your EC2 instance. The script runs the very first time the instance boots up. This process is called bootstrapping.

The main advantages of using Userdata are:

  1. Automation: Zero manual steps required post-launch for initial setup.
  2. Consistency: Ensure every instance launched from the same AMI has the exact same baseline configuration.
  3. Speed: Your servers are ready to serve traffic much faster.

For our example, we are using Userdata to install httpd (Apache) and configure a simple web page on an Amazon Linux 2 instance.

Step 1: Launching the EC2 Instance

To get started, log into your AWS Management Console.

  1. Navigate to Services and click EC2.
  2. In the EC2 Dashboard, click the Launch Instance button.
  3. Choose an Amazon Machine Image (AMI): Select the Amazon Linux 2 AMI. (This ensures compatibility with the yum package manager and our script.)
  4. Choose an Instance Type: Select t2.micro (as this is free-tier eligible for testing). Click Next: Configure Instance Details.
  5. Configure Instance Details: Leave most of the defaults (ensuring the instance is in a VPC where it can be publicly accessible).

Step 2: Entering the Bootstrap Script

This is the most critical part of the process—telling the instance what to do when it boots.

  1. On the Configure Instance Details page, scroll down until you find the Advanced Details section and expand it.
  2. Locate the User data field.
  3. Paste the following bash script into the User data text area:

The Apache Bootstrap Script

This script performs a sequence of actions necessary to get Apache running and deploy our test site:

#!/bin/bash
# Update installed packages
yum update -y

# Install the Apache Web Server
yum install httpd -y

# Start the Apache service
systemctl start httpd

# Configure Apache to start automatically on every boot
systemctl enable httpd

# OPTIONAL: Stop the firewall (for simple testing purposes)
systemctl stop firewalld

# Navigate to the Apache web directory
cd /var/www/html

# Create a simple index.html file with our test message
echo "This is my test site – Successfully Bootstrapped with Userdata!" > index.html

Pro Tip: Always begin your Userdata script with a shebang (#!/bin/bash) to ensure the operating system knows which interpreter to use.

Step 3: Configuring Networking and Security

Before we launch, we need to ensure people can actually access our new web server. This requires configuring security groups to allow HTTP traffic.

  1. Click Next: Add Storage (leaving the defaults).
  2. Click Next: Add Tags. Add a key Tag for easier identification. We used:
    • Key: Name
    • Value: My Test Server
  3. Click Next: Configure Security Group.
  4. You must create a new security group (or modify an existing one) to allow inbound traffic on Port 80.
    • Type: Custom TCP Rule
    • Port Range: 80
    • Source: Anywhere (0.0.0.0/0)
    • This opens HTTP access to the world.

Step 4: Launching and Verifying the Web Server

  1. Click Review and Launch. Double-check that your Userdata script is present and Port 80 is open.
  2. Click Launch.
  3. AWS will prompt you to select a key pair. You won’t need to log in for this specific test, but choose your standard key pair and acknowledge that you possess it.
  4. Click Launch Instances.

Once the instance is launched, go back to the EC2 Dashboard and check its state. It will take a minute or two for the initial Userdata script to execute while the instance is initializing.

Verification

  1. Wait until your instance is in the Running state and the Status Checks are complete.
  2. Select your new server instance and copy its Public IPv4 Address.
  3. Paste the Public IP into a new browser tab.

If everything worked correctly, you should see the following message:

This is my test site – Successfully Bootstrapped with Userdata!

Congratulations! You successfully launched an EC2 instance, and without ever logging in, you installed a web server, started the service, and deployed a basic website, all thanks to the power of AWS Userdata.

Conclusion

Bootstrapping instances with Userdata is a foundational skill for infrastructure automation on AWS. It allows you to rapidly deploy pre-configured environments and move closer to infrastructure-as-code principles.

We hope this video was useful! If you enjoyed this tutorial and found the process clear, please give this post a like and make sure to subscribe to Darren’s Tech Tutorials for more clear, accessible technology guides! Thank you for watching!