Skip to content

EC2 User Data

  • It is possible to bootstrap our instances using an EC2 User data script
  • Bootstrapping means launching commands when a machine starts
  • That script is only run once at the instance first start
  • EC2 user data is used to automate boot tasks such as:
    • Installing updates
    • Installing software
    • Downloading common files from the internet
    • Anything you can think of
  • The EC2 User Data Script runs with the root user

We want to make sure that this EC2 instance has an Apache HTTP server installed on it - to display a simple web page

For it we are going to write a user-data script.

This script will be executed at the first boot of the instance.

Hands on

First we are going to terminate the instance we made previously.

Then launch a new instance. We are going to follow the same steps as for the previous instance creation process, but when we are in the Configure Instance section, there is a Advanced Details section with user data settings.

We will input it as text.

#!/bin/bash
yum update -y
yum install -y httpd.x86_64
systemctl start httpd.service
systemctl enable httpd.service
echo "Hello World from $(hostname -f)" > /var/www/html/index.html

For security group, this time we are going to use An existing security group.

For keypair, we can reuse the previously created one.

Once it has launched, we should be able to grab the Public IP and see the created web server publicly available.