
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create an EC2 Instance with User Data Script to Launch Website
AWS EC2 is a fundamental building block of AWS that provides scalable and resizable compute capacity in the cloud. It is one of the oldest AWS services launched back in 2008. But what purpose does it serve? Well, AWS EC2 allows users like you and me to rent virtual computers and we can then run our own computer applications on it.
So, for example, you have created a nice, beautiful-looking website that you want to host and make it publicly accessible, but you don't have the infra to run it, then you can use the AWS EC2 service and host this website.
What is a User Data Script?
Now that we know the purpose of the EC2 instance, let's explore one more concept, which will help us set up our website on EC2. The EC2 service offers a feature which allows us to execute custom initialization scripts during the instance's launch. In other words, when the EC2 is being launched, it can execute a predefined, custom set of commands which you manage, to do whatever you want EC2 to do.
The place where you can write such pre-defined, custom code is called user data section and the script that runs automatically when EC2 is getting launched is called the user data script.
Based on the functionality which I have mentioned above for a user data script, you can probably guess why this might be helpful. Given below is a set of typical usage for the user data script commonly found in AWS for EC2 -
- You can automate instance setup tasks, such as installing software. So by automating software installation with EC2 User Data, you can ensure that the instances which are getting launched are ready to be used immediately right after launch. In this way, we are saving time and reducing manual effort and thus may help reduce manual errors.
- Another potential use case where you may want to add the user data script is to ensure the instance is up-to-date by applying OS updates and security patches at launch.
- In many applications, it is important to install agents to collect the logs. In AWS, it is quite common to install and configure the AWS CloudWatch agent to collect system metrics such as CPU, memory, disk usage etc. This installation can be done right at the time when AWS is launching the EC2 instance with user data script.
How to Add the User Data Script?
Now that we know the use cases and when to make a use of user data script, let's understand how you can practically do it. The simplest way is to use AWS Console. You can specify the user data when you launch an instance with the Launch Wizard in the EC2 console.
You would need an AWS account to login into the AWS console and select the EC2 as a service. The User data field is located in the Advanced details section of the launch instance wizard.
Refer to the image below for how this would look in AWS console,
There are couple of things to note here,
1. The user data section is optional. It means when you are launching the EC2 instance, you may add this or else skip it.
2. You can either enter or write the script directly in the given box or else you may also upload a file with your user data.
What to Write in a User Data Script to Run a Website?
OK, so far, we know the purpose/usage of user data script and also know how to reach it using AWS Console and by invoking the launch Wizard in the EC2 console. Our next step should be to write a script -- which is essentially a set of Linux commands, (I am assuming we are launching a Linux Instance here) that will help us set up and run our website.
Here is how it would look -
#!/bin/bash # Update the package repository. yum update -y # Install Apache web server yum install httpd -y # Start and enable Apache systemctl start httpd systemctl enable httpd # Below line create a simple HTML page, which shows the simple text echo "<html><body>Welcome to Website which is running on EC2 via user data script</body></html>" >/var/www/html/index.html
In the above code, we are installing the Apache web server, using yum install httpd -y command. Then, we have the following two commands -
- systemctl start httpd: Starts the Apache web server (httpd) immediately and this service will be running until the instance is restarted or stopped manually.
- systemctl enable httpd: Configures Apache (httpd) to start automatically on boot. This Ensures that the web server remains active even after a system reboot.
Together, these commands ensure that Apache starts now and also persists across reboots.
And, the following command simply creates an HTML page, which shows the plain text, of your choice.
echo "<html><body>Welcome to Website which is running on EC2 via user data script"</body></html> > /var/www/html/index.html
Test Your Website
So, now we have our small little website "hosted" on the AWS EC2 instance. Yes I agree it is not the most beautiful looking or most useful website in the world as all it is doing is displaying a simple text message. But, still, we want external people to access this website using their phones or PC/laptops.
To do that, we need the DNS of our EC2 instance. We can get that by following the steps given below -
- Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/
- From the navigation pane, you should choose Instances.
- Next, select your instance (which is hosting our simple website )from the list.
- In the details pane, the Public DNS (IPv4) and Private DNS fields display the DNS hostnames.
You can copy the DNS name and paste it in the browser and it will open our simple website which should show the text which we added in the user data script.
Conclusion
And there we are! We just launched our website and have it up and running on EC2 instance. And we did this using the user data script feature of EC2 Instance. I am sure you will agree that the whole set up was easy to do and fairly straightforward.
You can of course, share the DNS to other people and ask them to visit your website using their phones or PC/Laptops.