AWS User Data | How to run commands on startup Amazon Linux
Enjoying this content? Subscribe to the Channel!
Bootstrap AWS EC2: Automatically Install Apache Using Userdata (Hands-On Tutorial)
Hello tech enthusiasts! This is Darren O’Neal from Darren’s Tech Tutorials, and today we are tackling one of the coolest automation features in Amazon Web Services (AWS): EC2 Userdata.
If you’ve ever wanted your newly launched server to automatically install software, configure settings, or even deploy a simple website the moment it boots up, then bootstrapping is your answer. In this short but powerful tutorial, I’m going to show you exactly how to use Userdata to launch an Amazon Linux instance and automatically install and configure the Apache Web Server.
Let’s dive right in and get these servers working for us!
What is EC2 Userdata?
EC2 Userdata is a powerful mechanism that allows you to pass scripts or configuration instructions to your EC2 instance when it is first launched. These scripts run one time upon startup, effectively “bootstrapping” or initializing the server.
For Linux AMIs like Amazon Linux, Userdata typically expects a bash script, which is executed as the root user. This means we can use standard commands like yum install and systemctl start to fully prepare our server without ever needing to manually SSH in!
The Essential Apache Bootstrap Script
Before we launch our server, we need the script we’re going to paste into the Userdata field. This script is designed for Amazon Linux and performs three key tasks: installing Apache, starting the service, and creating a simple test page.
Here is the exact script we will use:
#!/bin/bash
# Update system and install Apache web server (httpd)
yum update -y
yum install httpd -y
# Start and enable Apache so it runs on reboots
systemctl start httpd
systemctl enable httpd
# Create a simple index.html file to confirm success
cd /var/www/html
echo "<h1>This is my test site - Bootstrapped with Userdata!</h1>" > index.html
(Note: If you need the full text tutorial and script for easy copying, you can find it linked in the video description.)
Step-by-Step: Launching and Bootstrapping the EC2 Instance
We will perform this entire setup directly through the AWS console. Follow these steps to launch your pre-configured web server.
Step 1: Start the EC2 Launch Process
- Log into your AWS Management Console.
- Navigate to Services and click on EC2.
- Click the Launch Instance button.
- Choose an AMI: Select the Amazon Linux AMI (we’re using the standard, free-tier eligible option).
- Choose Instance Type: Select t2.micro (free tier eligible). Click Next: Configure Instance Details.
Step 2: Configure Userdata in Advanced Details
This is where the magic happens! We need to find the Userdata injection point.
- On the ‘Configure Instance Details’ page, leave the defaults (we want it publicly available in the default VPC).
- Scroll down to the bottom and locate the Advanced Details section. Expand it.
- Scroll further until you see the User data field.
- Paste the Apache bootstrap script (from the section above) directly into this text box.
Step 3: Configure Storage, Tags, and Security
We can use default storage, but we must set a name and ensure we can access the web server.
- Click Next: Add Storage (Leave defaults).
- Click Next: Add Tags. Add a key/value pair for easy identification:
- Key:
Name - Value:
my test server
- Key:
- Click Next: Configure Security Group. This is essential for viewing the website!
- You need a security group that allows incoming traffic on Port 80 (HTTP) from anywhere (
0.0.0.0/0). You can either use an existing security group that permits this or create a new one:- Type: Custom TCP Rule
- Port Range: 80
- Source: Anywhere
- You need a security group that allows incoming traffic on Port 80 (HTTP) from anywhere (
Step 4: Review and Launch
- Click Review and Launch. Double-check that your Userdata script is present and that port 80 is open in the security group.
- Click Launch.
- When prompted for a Key Pair, you can select an existing one or proceed without one (since we won’t need to SSH into this test server).
- Click Launch Instances.
Step 5: Verification and Success!
Once your instance is successfully launched, it will take a minute or two for the status checks to pass and, crucially, for the Userdata script to execute completely.
- Navigate back to the EC2 Instances dashboard and wait for your
my test serverto show a ‘Running’ state. - Select the instance and locate its Public IP address in the details pane below.
- Copy the Public IP address.
- Paste the Public IP into a new browser tab and hit Enter.
Perfect! If everything worked correctly, you should instantly see the HTML page we scripted:
This is my test site - Bootstrapped with Userdata!
This proves that Userdata successfully installed Apache, started the service, and deployed our index file, all completely hands-off!
Conclusion
Bootstrapping instances using EC2 Userdata is a fundamental skill for cloud engineers. It turns the manual server setup process into a highly automated, repeatable script, saving you time and ensuring consistency across your deployments. We successfully launched a functional Apache web server on Amazon Linux just by pasting a few lines of code into a text box!
If this tutorial was useful and helped you automate your AWS setup, please like this post and subscribe to Darren’s Tech Tutorials for more clear, practical technology guides! Thank you for watching!