> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# EC2 Userdata demo

> Guide showing how to use EC2 user data with cloud-init to install, start, and enable nginx automatically on an instance's first boot.

This guide demonstrates how to use EC2 user data to run an initialization script automatically when a new Amazon EC2 instance boots. Using user data (via cloud-init) lets you perform first-boot configuration such as installing packages, starting services, or bootstrapping applications without SSHing into the instance.

Goal: Launch an EC2 instance that installs, starts, and enables nginx on first boot so the web server is reachable immediately after initialization.

Prerequisites:

* An AWS account with permissions to launch EC2 instances.
* A key pair to access the instance (optional for this demo since configuration is via user data).
* A security group that allows HTTP (80) and HTTPS (443) inbound access.

Step 1 — Launch a new EC2 instance

* Open the EC2 console and click "Launch instance".
* Give the instance a descriptive Name (for example, userdata-demo).
* Select an Amazon Linux AMI (or another supported Linux AMI that uses yum).

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/EvGInugvhj57xLLI/images/AWS-Certified-Developer-Associate/Elastic-Compute-CloudEC2/EC2-Userdata-demo/aws-ec2-console-dashboard-launch-button.jpg?fit=max&auto=format&n=EvGInugvhj57xLLI&q=85&s=6e3e226fd54b7daa7b263b609d77f2cb" alt="Screenshot of the Amazon Web Services EC2 console dashboard. It shows resource summaries, a &#x22;Launch instance&#x22; button, service health status, and the left-hand navigation for instances, images, and storage." width="1920" height="1080" data-path="images/AWS-Certified-Developer-Associate/Elastic-Compute-CloudEC2/EC2-Userdata-demo/aws-ec2-console-dashboard-launch-button.jpg" />
</Frame>

Step 2 — Choose instance configuration

* Choose an appropriate instance type (t2.micro is fine for testing and is often within the AWS Free Tier).
* Select your key pair or create one if you plan to SSH later.
* Configure networking and storage as needed.

Step 3 — Configure Security Group
Allow inbound access so nginx can serve traffic. At minimum, allow:

| Protocol | Port | Source                   | Purpose                          |
| -------- | ---- | ------------------------ | -------------------------------- |
| TCP      | 80   | 0.0.0.0/0                | HTTP (nginx default)             |
| TCP      | 443  | 0.0.0.0/0                | HTTPS (if serving TLS)           |
| TCP      | 22   | \<your-ip>/32 (optional) | SSH access — restrict to your IP |

Step 4 — Add your user data script
Open Advanced Details on the launch page and paste the script into the User data field (you can also upload a file). The script below runs as root during the instance's first boot and uses yum to install nginx, then starts and enables it:

```bash theme={null}
#!/bin/bash
sudo yum install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx
```

<Callout icon="lightbulb" color="#1CB2FE">
  User data scripts run as root on first boot via cloud-init. If you need to troubleshoot, check /var/log/cloud-init-output.log on the instance for the script's output and any error messages.
</Callout>

Step 5 — Launch and verify

* Launch the instance and wait for its status checks to pass.
* From the EC2 Instances page, note the instance's Public IPv4 address or Public DNS.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/EvGInugvhj57xLLI/images/AWS-Certified-Developer-Associate/Elastic-Compute-CloudEC2/EC2-Userdata-demo/aws-ec2-launch-instance-ami-t2-micro.jpg?fit=max&auto=format&n=EvGInugvhj57xLLI&q=85&s=e2c692c5bd577fe37a10aa6f3d6ab184" alt="A screenshot of the AWS EC2 &#x22;Launch Instance&#x22; console showing AMI options (Amazon Linux, macOS, Ubuntu, Windows, etc.) and details on the right summary panel with a t2.micro instance selected and a &#x22;Launch instance&#x22; button." width="1920" height="1080" data-path="images/AWS-Certified-Developer-Associate/Elastic-Compute-CloudEC2/EC2-Userdata-demo/aws-ec2-launch-instance-ami-t2-micro.jpg" />
</Frame>

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/EvGInugvhj57xLLI/images/AWS-Certified-Developer-Associate/Elastic-Compute-CloudEC2/EC2-Userdata-demo/aws-ec2-two-running-t2-micro.jpg?fit=max&auto=format&n=EvGInugvhj57xLLI&q=85&s=9fa93583a6aea6c12c369103d141c5df" alt="A screenshot of the AWS EC2 Instances dashboard showing two running t2.micro instances (one named &#x22;userdata-demo&#x22;) with status checks passed." width="1920" height="1080" data-path="images/AWS-Certified-Developer-Associate/Elastic-Compute-CloudEC2/EC2-Userdata-demo/aws-ec2-two-running-t2-micro.jpg" />
</Frame>

Open a browser and navigate to http\://\<public-ip> (replace \<public-ip> with the instance address). You should see the default nginx welcome page, confirming the user data script installed and started nginx successfully.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/EvGInugvhj57xLLI/images/AWS-Certified-Developer-Associate/Elastic-Compute-CloudEC2/EC2-Userdata-demo/nginx-welcome-page-ip-browser-screenshot.jpg?fit=max&auto=format&n=EvGInugvhj57xLLI&q=85&s=d4725184696eee18436270b01decd758" alt="A screenshot of a web browser displaying the default &#x22;Welcome to nginx!&#x22; page (showing the nginx welcome text and links) served from an IP address. Browser tabs and the address bar are visible at the top." width="1920" height="1080" data-path="images/AWS-Certified-Developer-Associate/Elastic-Compute-CloudEC2/EC2-Userdata-demo/nginx-welcome-page-ip-browser-screenshot.jpg" />
</Frame>

Troubleshooting tips

* Confirm the instance has a public IP and the security group allows inbound HTTP.
* Verify cloud-init ran by inspecting /var/log/cloud-init.log and /var/log/cloud-init-output.log on the instance.
* If the package manager fails, ensure your chosen AMI supports yum (Amazon Linux / RHEL / CentOS) or adjust the script for apt (Ubuntu/Debian).

<Callout icon="warning" color="#FF6B6B">
  User data runs only during the instance's initial boot. To re-run initialization you can: bake a new AMI with the changes, use configuration management (Ansible/Chef/Puppet), or manually re-run scripts via SSH or with cloud-init's re-run options.
</Callout>

Links and references

* [Amazon EC2 User Guide — Running Commands on Your Linux Instance at Launch](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html)
* [cloud-init Documentation](https://cloud-init.io/)
* [nginx Official Site](https://nginx.org/)
* [Amazon Linux AMI](https://aws.amazon.com/amazon-linux-ami/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/aws-certified-developer-associate/module/538040df-b323-4fd1-ad80-f5c22725c345/lesson/4d194068-131d-4c68-81d7-c6bb83184e48" />

  <Card title="Practice Lab" icon="flask-conical" cta="Learn more" href="https://learn.kodekloud.com/user/courses/aws-certified-developer-associate/module/538040df-b323-4fd1-ad80-f5c22725c345/lesson/94dee8ad-43a6-4e6d-8600-6c55be407222" />
</CardGroup>
