When you launch an instance in Amazon EC2, you have the option of passing user data to the instance that can be used to perform common automated configuration tasks and even run scripts after the instance starts. You can pass two types of user data to Amazon EC2: shell scripts and cloud-init directives.
You can also pass this data into the launch instance wizard as plain text, as a file (this is useful for launching instances using the command line tools), or as base64-encoded text (for API calls).
This will save time and manual effort everytime you launch an instance and want to install any application on it like apache, docker, Jenkins etc
Introduction
When setting up instances (virtual servers) in Amazon Web Services (AWS), particularly via the EC2 service, there's an often overlooked yet powerful feature known as "User Data." User Data allows users to run scripts or pass data into an instance at the time of its creation. This feature can be immensely helpful for various automation and configuration tasks. Let's delve deeper into what User Data is, how it can be utilized, and some best practices to consider.
What is User Data?
At its core, User Data is data passed to the instance at launch time. This can be in the form of scripts (bash, PowerShell, etc.), cloud-init directives, or simply text data. The primary purpose of User Data is to automate boot tasks without the need for manual intervention.
Common Use Cases for User Data:
Installing Software: User Data can automate the installation of software packages when an instance is started. For instance, a bash script can update packages and then install a web server.
Configuration Management: You can configure instances to join specific clusters, domains, or any other kind of environment-specific setting.
Sending Notifications: Send a notification (maybe through an SNS topic) when a new instance is booted up.
Mounting Storage: Automate the process of attaching and mounting additional EBS volumes.
How to Use User Data:
AWS Management Console: When launching a new EC2 instance through the AWS Console, there's a step titled "Configure Instance Details." Here, there’s an option to add User Data in the form of text or scripts.
AWS CLI: If you're using the AWS Command-Line Interface, the
run-instances
command has a--user-data
parameter.AWS SDKs: Most AWS SDKs support setting User Data when creating instances.
EC2 Metadata: Once an instance is launched with User Data, it can be retrieved within the instance via EC2 metadata at the URL:
http://169.254.169.254/latest/user-data
.
Best Practices:
Keep It Short and Sweet: While User Data is powerful, it's recommended to keep the scripts or directives concise. For complex configurations, consider tools like AWS OpsWorks, AWS Elastic Beanstalk, or configuration management tools like Chef or Puppet.
Security: Never put sensitive information (like passwords or API keys) directly in User Data. Consider using AWS Secrets Manager or AWS Systems Manager Parameter Store.
Idempotency: Ensure that your User Data scripts are idempotent, meaning they can be run multiple times without causing issues or unintended side effects. This helps when you need to relaunch or reboot instances.
Error Handling: Always have error handling in place. If your script fails to run, it should log an error or notify you.
Task1:
Launch EC2 instance with already installed Jenkins on it. Once server shows up in console, hit the IP address in browser and you Jenkins page should be visible.
Take screenshot of Userdata and Jenkins page, this will verify the task completion.
Login to your AWS Console and select EC2 Service. On the right corner of the window, Click on Launch Instances
Give Name to the server and Select OS Image as Ubuntu
Select Instance t2.micro (Free) Type and Keypair - Create it if not done already.
Create New Key Pair - Give name and select .ppk to login using putty.
Click on Create key pair and make sure to keep it safe and handy to use when login to this instance.
Select the keypair and scroll down.
Network settings. VPC as Default and Update Security group as follows.
Reason for Updating Security Group - SSh Port 22 was already added by default to connect to it. Also, add a rule for Custom TCP protocol and 8080 port number. Jenkins by default runs on port 8080
Scroll Down and keep storage settings as default
Extend Advanced details. Add the following script in user data under Advanced Details to install Jenkins on the EC2 instance.
#!/bin/bash
sudo apt update -y
sudo apt install openjdk-11-jre-headless -y
java -version 2>/home/ubuntu/output.txt
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update -y
sudo apt-get install jenkins -y
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins >>/home/ubuntu/output.txt
Review and Launch Instance.
Instance Got Created, You can also see the logs.
Now when you see instance status it is initializing and hence we are unable to connect. That means the User Data scripts are running in backend.
Now after around 5 mins of you see the status turns as below and you are able to connect.
Login to EC2 Instance and hit below commands
ls
cat /home/ubuntu/output.txt
User Data in AWS is a versatile tool in the hands of developers and system administrators alike. Whether you're looking to simplify the initial setup, automate configurations, or improve the repeatability of instance launches, User Data can prove invaluable. As with all powerful tools, however, caution and best practices should be employed to ensure efficient and safe operations.