(#E018C2)How to Use the User Data Section in EC2 for Automation

User Data in AWS

The User Data section in AWS allows you to run custom scripts or commands automatically when an EC2 instance launches for the first time. It’s commonly used for tasks such as installing software, updating packages, or configuring services.

You can enter shell scripts (for Linux) or PowerShell scripts (for Windows) in the User Data field during instance creation under the Advanced Details section. This helps automate instance setup without manual intervention.

1. When launching the server, navigate to the “Advanced Details” section during the setup process.

2. At the bottom of the Advanced Details section, you will find the “User Data” option..

3. Now, upload your user data file or paste the script you created directly into the User Data field. In this example, I am pasting the Apache server installation script. Make sure the script matches your OS type (Amazon Linux or Ubuntu). Once done, click on “Launch Instance” to start the server.

Use this  script for this –

#!/bin/bash

# Update all packages

yum update -y

# Install Apache (httpd)

yum install -y httpd

# Enable Apache to start on boot

systemctl enable httpd

# Start Apache service

systemctl start httpd

# Optional: Add a simple homepage

echo “<h1>Welcome to Apache on Amazon Linux</h1>” > /var/www/html/index.html

4. Wait for the server to be in the “running” state, and ensure that HTTP (port 80) is allowed in the Security Group to access the web server.

5. Wait for the server to be fully up and running. Then, try to connect by copying the public IP address of the instance and pasting it into a new browser tab to see the result.

Now its working.

END

Leave a Comment

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