User Data

Get working knowledge of using the user data scripts in EC2 instances and how they can used to perform system-level tasks.

We'll cover the following

Automating the configuration process is essential when launching EC2 instances, especially in dynamic and scalable environments. Without automation, administrators would need to manually configure each instance after launch, which is time-consuming and error-prone, especially in large-scale deployments. In this lesson, we will understand what and why user data is essential and how it works with an example.

EC2 user data

User data is crucial for EC2 instances to enable automated, customized, and consistent configuration. It facilitates infrastructure as code practices, supports dynamic configuration, optimizes costs, and integrates with other AWS services. These capabilities are essential for managing and scaling infrastructure effectively in cloud environments.

User data is added through the additional details on the instance launch page. Once all the procedures to launch the instance are complete and the instance is launched. The instance may take some additional time to execute the user data script. The user data script is executed automatically after the instance is launched and the operating system boots up.

Press + to interact
User data flow
User data flow

User data allows to run commands/scripts when launching an EC2 instance. User data can be used to automate configuration tasks and even run scripts after the instance starts. It can be a script or cloud-init directives; scripts can be a shell script or any other scripting language supported by the chosen operating system. Let’s look at an example of user data.

Press + to interact
#!/bin/bash
sudo yum -y update &&\
sudo yum -y install git &&\
curl -fsSL https://rpm.nodesource.com/setup_21.x | sudo bash -
sudo yum install -y nodejs && sudo dnf install nodejs -y &&\
git clone https://github.com/Educative-Content/aws-compute-services-zero-to-hero-cl-frontend-app.git &&\
cd aws-compute-services-zero-to-hero-cl-frontend-app &&\
npm install &&\
npm start

The user data script is OS-dependent, as the above script works with Amazon Linux. The above user data script updates the system packages, installs Git and Node.js, clones a Git repository containing a Node.js application, installs the application’s dependencies, and starts the application.

In contrast, cloud-init directives can launch multiple instances with the same user data. Normally, the user data is executed only once when the instance is launched. However, it can also be configured to execute on instance reboot. Let’s take a look at the cloud-config file example.

Press + to interact
#cloud-config
packages:
- git
- npm
write_files:
- path: /opt/start_react_app.sh
content: |
#!/bin/bash
cd /opt/my-react-app
npm install
npm start
echo "React app started!"
runcmd:
- git clone https://github.com/Educative-Content/aws-compute-services-zero-to-hero-cl-frontend-app.git
- chmod +x /opt/my-reactapp.sh
- /opt/start_react_app.sh

This cloud-config file is designed to be used with cloud-init, a tool used in cloud environments to perform initialization tasks on virtual machines or instances when they are first launched. It clones a repository from GitHub and runs the React application.

How user data works

The user data script is executed with root privileges on the instance, allowing it to perform system-level tasks such as installing software, configuring services, and downloading files. The script has access to instance metadata, which includes information such as instance ID, Region, Availability Zone, and more. This metadata can be used to customize the behavior of the user data script based on the instance’s environment.

Let’s look at an example to deploy two applications in the private subnet. We can either deploy the application on an EC2 instance in the private subnet and then use Instance Connect Endpoint to execute the commands using the connect terminal. However, this approach is not scalable. Alternatively, we can use a script of commands and pass it as user data to the instance.

Press + to interact
Architecture diagram
Architecture diagram

The architecture diagram highlights the two different approaches to launching an application on an EC2 instance in the private subnet; such scenarios are very common during the testing phase of an application. During testing, applications are deployed in a private subnet to test for different features.

App-01 is launched, and then we created an Instance Connect Endpoint to connect to the terminal then, we executed the commands, which are launched with a user data script to launch the application directly. Whereas App-02 is launched using EC2 user data.

Get hands-on with 1300+ tech skills courses.