top of page

Amazon EC2 User Data Scripts Configuration.

Updated: Oct 14, 2022

Amazon EC2 User Data Scripts Configuration.

Tutorial Objectives:

1. Learn to configure user data scripts, so as to run every time you restart your instance. Step 1: Log in to the AWS management console. Find for EC2 service in the search bar provided on the page. Click on the Launch Instances.

Step 2: After clicking on the launch instance you will be redirected to this page. Here search/select Amazon Linux 2 AMI.

Step 3: Choose an Instance: Select General purpose type t2.micro Instance Type. Click on Next: Configure Instance Details.


Step 4: Here you will Configure Instance Details.

In the Auto-assign Public IP field select on the dropdown list and select Enable option.

Click Next: Add Storage at bottom right of screen.


Step 5: Add Storage

Keep all the default settings as they are and click on the Next: Add Tags.


Step 6: Add Tags

Key: Name

Value: LinuxServer

Click on the Next: Configure Security Group at the bottom right of the screen.


Step 7: Configure Security Group

  • Keep the default SSH rule and add a new rule. From the drop-down list select HTTP Rule.

  • Warning: Rules with source of 0.0.0.0/0 allow all IP addresses to access your instance. We recommend setting security group rules to allow access from known IP addresses only.


Click on Review and Launch button on the bottom right corner.


Step 8: Review Instance Launch.

Select existing Key Pair is available, acknowledge the same and launch the instance.


Step 9: Now your instance is getting launched. scroll down the page and you will find view instance at the bottom right of the screen.

Click the button to see your launched instance.

On the newly loaded page, you will see the instance which is deployed.


Step 10: Select the instance. Go to Instance stateStop instance.

After the instance is stopped, select the Instance.

Go to Actions –> Instance settings –> Edit user data.


Paste the following script in user data text area.

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
/bin/echo "Hello World" >> /tmp/testfile.txt
--//--


Step 11: Start the instance again. Click on Instance – Start Instance.


SSH into the instance.

Click on Connect.


Click Connect.


Use the cd command to change directory to tmp folder.

cd /tmp/


Run the ls command to list the contents of this folder and check if the file has been created.

ls


Run cat command to view the contents of the file.

cat testfile.txt


Step 12: Close the SSH session and stop the instance.

Select the Instance.

Go to Actions – Instance settings – Edit user data.

Refer Step no 10.

Paste the following script mentioned below.


Content-Type: multipart/mixed; boundary="//"

MIME-Version: 1.0

Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
yum -y update
yum -y install httpd
chkconfig httpd on
service httpd start
echo "<html><h1>This a tested user data script</h1>
<h1>added after instance creation!</h1></html>" > /var/www/html/index.html
--//--

The script will look similar as shown below...


Step 13: Now start the instance again. Wait until it is in Running state.

Copy the Public IPv4 address and check whether the web server is running or not.


The following output will be visible.


Note: If you no longer need this instance make sure to terminate the instance.

Click on the drop-down menu beside the actions button.

Select the Instance State and click on Terminate.

This will terminate your instance


Was this document helpful? How can we make this document better? Please provide your insights. You can download PDF version for reference.

Utilization of user data blog by Archis Davanpelliv3 (1)-converted
.pdf
Download PDF • 1.09MB

We provide the best AWS training from Pune, India.






For more information on aws certification contact us now.

5,562 views16 comments
bottom of page