0
0
AWScloud~30 mins

Instance metadata and user data in AWS - Mini Project: Build & Apply

Choose your learning style9 modes available
AWS EC2 Instance Metadata and User Data
📖 Scenario: You are setting up a new AWS EC2 instance for a small web application. You want to configure the instance so it can access its own metadata and run a simple startup script automatically when it launches.
🎯 Goal: Build an AWS EC2 instance configuration that includes accessing instance metadata and setting user data to run a startup script.
📋 What You'll Learn
Create a variable with the instance metadata URL
Create a variable with a simple user data script
Write code to fetch instance metadata using the metadata URL
Add the user data script to the EC2 instance configuration
💡 Why This Matters
🌍 Real World
Accessing instance metadata and using user data scripts are common tasks when configuring cloud servers to automate setup and retrieve instance details.
💼 Career
Cloud engineers and DevOps professionals often write user data scripts and query instance metadata to automate server configuration and monitoring.
Progress0 / 4 steps
1
Create the instance metadata URL variable
Create a variable called metadata_url and set it to the exact string "http://169.254.169.254/latest/meta-data/".
AWS
Need a hint?

The instance metadata service is accessed via a special IP address. Use that exact URL.

2
Create the user data script variable
Create a variable called user_data_script and set it to the exact multi-line string that contains a bash script starting with #!/bin/bash and includes the line echo "Hello from user data" > /var/tmp/user_data_output.txt.
AWS
Need a hint?

The user data script runs on instance launch. Use triple quotes for multi-line string.

3
Fetch instance metadata using the metadata URL
Write code to import the requests library and create a variable called instance_id that fetches the instance ID by sending a GET request to metadata_url + 'instance-id' and getting the text response.
AWS
Need a hint?

Use requests.get() to fetch metadata and .text to get the string.

4
Add the user data script to the EC2 instance configuration
Create a dictionary called ec2_config with a key 'UserData' set to the variable user_data_script.
AWS
Need a hint?

The EC2 configuration dictionary should include the user data script under the 'UserData' key.