Create an EC2 Instance with EC2 User Data to Have a Website

Create an EC2 Instance with EC2 User Data to have a website

In this article, I am going to discuss How to Create an EC2 Instance with EC2 User Data to have a Website Hands-On with Examples. Please read our previous article where we discussed AWS Budget Setup.

Create an EC2 Instance with EC2 User Data to Have a Website

We are going to launch our first E2 instance running Amazon Linux. We will be launching our first EC2 instance which is well a visual server and we’ll use the console for this.

Create an EC2 Instance with EC2 User Data to Have a Website

We will get a high-level approach to all the various parameters you have when launching an EC2 instance and you will see there are many but we’ll learn the most important ones and then we will launch a web server directly on the EC2 instance. Using a piece of code, we will pass it to the EC2 instance which is called the user data.

How to Create an EC2 Instance with EC2 User Data to have a Website Hands-On with Examples

Finally, we will learn how to start stop and terminate our instance. Let us get started and launch our first EC2 instance. For this I go into the EC2 console then I will click on instances and then click on launch instances. I am able to launch my first EC2 instance and to do I need to add a name and tags.

How to Create an EC2 Instance with EC2 User Data to have a Website Hands-On with Examples

The name is going to be My First Instance and that is the name tag. If you wanted to add additional tags to tag your instance differently then you could click there but you do not need to click on this. Using just the name My First Instance is good enough you need to choose a base image for your EC2 instance.

How to Create an EC2 Instance with EC2 User Data to have a Website Hands-On with Examples

This is the operating system of your instance. There is a full catalog that you can search from but we’re going to use the ones from the quick start that are very helpful.

How to Create an EC2 Instance with EC2 User Data to have a Website Hands-On

The one we will be using is Amazon Linux which is provided by AWS. I will choose the Amazon Linux 2 AMI and as you can see that one is free tier eligible. This gives me Amazon Linux 2 and the architecture I will choose is 64-bit x86. Everything is left pretty much as the defaults and we’ll see in this section, and moreover, in the other ones you can create your own AMIs and you can find them here but currently, we’re just going to use the ones provided by AWS as quick start.

How to Create an EC2 Instance with EC2 User Data to have a Website Hands-On

Next, we need to choose an instance type and so instant types are going to differ based on the number of CPUs they have the amount of memory they have, and how much they cost.

How to Create an EC2 Instance with EC2 User Data to have a Website

I have a T2 micro selected this one is free tier eligible it will be free to launch one of them during an entire month if we leave it running this is what we will be using. But here you could scroll down and look at other types of instances for example T1 micro is also free tier eligible but that’s the older generation. You have a bunch of instances right here available to you.

How to Create an EC2 Instance with EC2 User Data to have a Website

Some of them are going to be free tier eligible some of them will not and by default, the one that’s gonna be free tier eligible is a T2 micro we will be using that one a lot.

Create an EC2 Instance with EC2 User Data to have a Website

If you wanted to compare the instance types you will just click on that link and it shows you all the types of instances in here as well as how much memory they have and so on we will be using a T2 micro. Next, a key pair is to log into your instance. This is necessary if we use the SSH utility to access our instance and we will be using the SSH utility in this course therefore it is required for us to create a key pair. We can see right now there is no key pair and we could proceed without a key pair but for now, we will not do this.

Create an EC2 Instance with EC2 User Data to have a Website

Let’s go ahead and create a new key pair the name is going to be EC2 Tutorial then you need to choose a key pair type we’ll be using the RSA encrypted then the key pair formats if you have Mac or Linux or Windows 10 then you can use the PEM format if you have Windows less than version 10, for example, Windows 7 or Windows 8 then you can do a little shortcut and directly use a PPK which is going to be used for PUTTY and PUTTY is how you do SSH on Windows 7 and Windows 8 remember anything else but Windows 7 and Windows 8 choose PEM else to use PPK that should be clear enough.

Create an EC2 Instance with EC2 User Data to have a Website

I am going to create this key pair and it is downloaded for me directly now it is selected automatically here. We have to go into network settings for now I will not touch anything. My instance is going to get a public IP and then we need to connect to our instance for this there is going to be a security group attached to our instance which is going to control the traffic from and to our instance and therefore we can add rules.

Create an EC2 Instance with EC2 User Data to have a Website

The first security group created will be called launch-wizard-1 created by the console directly and we can define multiple rules the first rule we want to have been to allow SSH traffic from anywhere we leave it at this and this will create a rule in our security group to allow SSH traffic but we also want to allow HTTP traffic from the internet.

I will take that box and this is because we are going to launch a web server on our EC2 instance we need it as well now going to use HTTP for now we do not need to tick the second box. Let us configure the storage then we can compare the storage, and as we can see we have eight gigabytes of gp2 root volume that we will leave as is because, in the free tier, we can get up to 30 gigabytes of EBS General Purpose SSD storage we only have one volume necessary.

Create an EC2 Instance with EC2 User Data to have a Website

If you go into advanced you could configure them and see a little bit more information. The one important thing to note here is the deletion on termination. By default, it is enabled to yes, I just did advance to show you that one detail that means that once we terminate our EC2 instance then that volume is also going to be deleted we leave everything as is and we will get back into the simple mode. Next for advanced details, this is where it gets an interesting skip spot, I will skip the IAM instance profile. I will go over them once we need to explore them, I will skip all of that so let us scroll down let’s scroll down all the way to the bottom and at the bottom there is user data.

Create an EC2 Instance with EC2 User Data to have a Website

User data is when we pass a script with some comments to our EC2 instance to execute on the first launch of our E2 instance and only the first launch. Therefore, on the first launch we want to be able to pass these commands right here for this you go into your code you go to the EC2 fundamentals, and then the ec2-user-data.sh file you copy entirely this all of it and then you paste it here you paste everything and that means that this script is going to be executed when the instance is first started and only once in the whole life cycle of the instance. What it is going to do is that it’s going to update a few things then install the HTPD web server on the machine and then write a file an HTML file that will be a web server you don’t need to know code or know these commands this is provided to you to illustrate a few things on this lecture. Finally, to summary we want to start one instance this is great and we can review everything we have here you can see in the free tier we get a first year of 750 hours of t2 micro which is reading it running for one month that is every month. If you do not have a t2 micro in your region then it is going to be a t3 micro then also we get 30 gigabytes of EBS storage and so on. Let us launch this instance and the instance is going to be launched.

How to Create an EC2 Instance

Let us go to view all instances refresh and now my instance is in a pending state. It is going to take about 10, to 15 seconds for the instance to come up and this is the whole power of the Cloud. Thanks to the Cloud I am able to create an instance or 100 of them very quickly in less than 10 seconds without me owning any single server that is extremely powerful and we just scratched the surface of the power of the Cloud obviously because the course is just getting started but you can get a feeling of the advances and the speed we can have on the Cloud thanks to this you can see now my instance is running and right now I want to show you a few things the first one is that the instance name is my first instance and there’s an instance ID which is just a unique identifier for my instance. There is a public IPv4 address which is what we’re going to use to access our EC2 instance or there is a private IPv4 address which is how to access that instance internally on the AWS network which is private. The instance state is running and we get some information around hostname private DNS which instance that we have so t2 micro as well if you scroll down the AMI, we are using which is Amazon Linux 2, and the key pair we are using which is EC2 Tutorial you can have a look at a few details in here. You have more information for example on security. We get some information on the security group which was created called launch-wizard-1 with these in the rules. PORT22 is accessible from everywhere and port 80 is accessible from everywhere you should have something similar. If you do not start over because you probably missed a step the add rule allows all communication outwards which allows the instance to access the internet. For storage, we saw that we created one volume of a gigabyte we are good to go. Let us have a look at the web server running on my instance for this you go to a public IPv4 address. Copy this or click on the open address and as you can see it does not work if you click on it copy and then paste it you press enter it’s going to work.

How to Create an EC2 Instance

It depends on the web browsers you have and so on but the reason it does not work here is that in the URL you need to make sure that you are using the HTTP protocol. HTTP colon slashes and then the IP because if you use HTTPS this is not going to work it’s going to give you an infinite loading screen which was happening right here, please make sure to use HTTP colon slash and then the IP address and you’re going to get this screen in programming when you do something for the first time usually say hello world this web server is selling hello world from and this IP right here which is not the public IP.

How to Create an EC2 Instance

This IP right here 44.204.221.201 actually corresponds to the private IPv4 address this is something that I program myself we use the public IP address to access it but we have the private IP address in here and we have the hello world if you go too fast, you’re going to get no messages if you go too fast just wait five minutes get back to it refresh this page and you’ll see it we have a web server running this is great.

How to Create an EC2 Instance

Let us explore a few options we have an EC2 instance and it running but if we don’t need it, we can go to instance state and then click on stop instance in the Cloud you can start and stop instances just as you wish and why would you stop an instance? The longer you leave it running the more you are going to pay of course. If you decide to stop an instance then AWS will not bill you for it.

How to Create an EC2 Instance

The instance state is kept because you have a volume attached to it but at least you’re not paying for it we can see right now while the instance is in a stopping state and if we try to refresh this page is going to of course it’s not going to work because well you don’t have the server running anymore you can see it gets to some like infinite loading experience my instance is now stopped and if I wanted to actually I could get rid of it in the Cloud it’s very common to start instances and then get rid of them very quickly just to try it out because this is the Cloud and we can do whatever we want.

How to Create an EC2 Instance

We can do an instance state and then terminate the instance. If we do so we are going to get a warning message and do not click on terminate because I want to keep this instance with me but this is how we would get rid of it. I cancel this but what I am going to do now is I am going to start my instance again. I go to the instance state and then start the instance and now as you can see the state is pending it is getting started and I just wait for it to be started in the green state and I will show you something very interesting my instance is now running if I go here and stop the refresh and try again to refresh as you can see it still goes into an infinite loop you may say well the server is running Sandy66 why is it not displaying the message now? It is displayed here but like from the old one of course.

How to Create an EC2 Instance

The IP starts with 54 but here if you click on here now the public IP start with 3.250. The public IP has changed if you stop an instance and then you start it later then AWS will maybe change its public IPv4, therefore, you need to copy the new IPv4 and make sure to use HTTP and voila we have access back to our EC2 instance. But one thing that has not changed is the private IPv4 the private IP will always stay the same but the public IPv4 may change that is it for this hands-on we have seen quite a lot of things. We launched our first EC2 instance which is very exciting our first web server in the Cloud. We had to look at some of the power of the Cloud you are just using some API calls to stop an instance start the instance and so on.

In the next article, I am going to discuss AWS EC2 Instance Types. Here, in this article, I try to explain How to Create an EC2 Instance with EC2 User Data to have a Website Hands On. I hope you enjoy this How to Create an EC2 Instance with EC2 User Data to have a website article.

Leave a Reply

Your email address will not be published. Required fields are marked *