Create an EC2 Instance with EC2 User Data in Amazon Web Services

Armond Holman
8 min readJul 12, 2023
from logical

What is an EC2 Instance?

Amazon EC2 is an infrastructure as service (IaaS) solution. This means underlying software for running a virtual server are managed for you. Customers are responsible for the operating system and software installed including installing patches on operating sytem as part of maintenance. Amazon EC2 is like having your own virtual computer that you can use whenever your want. No need to buy a physical computer, instead, you can just rent a virtual computer in the cloud that you can access from anywhere with an internet connection.

In this article, we will be creating an EC2 Instance with EC2 User Data step by step!

Step 1:

Sign into the AWS Management Console as a Root user, utilizing your IAM credentials. You are authenticated using your User name and Password.

Link:https://aws.amazon.com/console/

Then Log in using your Root user credential.

Link:

Step 2:

Navigate to the EC2 Workspace on your AWS Console. This will be where we will create a Security group. We will zone our protocols and insert our User Data, for now. We will be putting this EC2 instance on the web through AWS running on a Linux server.

SECURITY GROUP FLOW

Step 3: In the next steps we will be securing our server. That is creating a firewall to control incoming and outgoing traffic. We do this by taking the steps to create a new Security group.

Without a security group, the server will not be protected. A security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic. To do this on the EC2 console, go to Network & Security in the navigation and click on “Security Groups.” This will open a list of available security groups. If there are no security groups, there will just be ONE listed there. Which is called the default. DO NOT TOUCH THAT ONE!

What you want to do instead is click on the button that says Create security group.

Step 4:

Next, we will be naming our Security group.

  • *Tip: Label the security group intelligently(in a way you can remember) so in the future, we can remember the function of that security group.**
Also include a description of the group

Step 5:

Set inbound rules. This is the inbound traffic rules of our EC2 instance for this virtual machine. For the source, we always use AnywhereIPv4.

Step 6:

Always add tags. Have a tagging policy. Tagging helps to search and filter your resources or track your AWS costs.

**TIP: If your client insists on making tags optional, then push tag. Tagging is how you find the resources and track your costs for Amazon Web Services for that Enterprise. Always tag. Always***

Step 7:

Your security group has been created. In the blue link at the top of that screens card, click security groups. It will be in the blue(link).

Step 8:

On the listing of Security groups, find your security group. Copy the security group name → and paste → to the “Name” field, in that row of that data table.

Remember: Do not TOUCH, SELECT, CLICK or ANYTHING the default group!

Step 9:

In the next steps, we will be creating an EC2 instance which will attach our newly made security group. Go into the EC2 console. Then click on instances and then click on launch instances.

Step 10:

In there I am able to launch an instance and to do so i need to add a Name and tags. Name your instance. Then for tags, “Name” is good enough for now.

Step 11:

The next thing we need to do is choose a base image Operating System(OS) for our EC2 instance, also known as Amazon Machine Image(AMI). We can choose Linux, Ubuntu, Windows, Red Hat, or any of those systems plus more AMIs from AWS, Marketplace and the community. The Amazon Linux 2 AMI is the Free tier eligible (selected by default). Also, the architecture will be 64-bit (x86).

What is Amazon Machine Imaging?

AMI is a pre-configured virtual machine image that is used to create EC2 instances. You might want to use an AMI if you want to launch an EC2 instance with specific configurations, software, or operating systems installed. It is necessary because it saves you time and effort. An AMI is a blueprint for virtual machines that contains all the necessary information to create and launch an instance of an EC2 virtual machine. This can be useful when you need to launch multiple EC2s with the same configurations.

Step 12:

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

You can compare instance types by clicking on compare instance types. You click on that link and it shows you all the type of instances in here, as well as how much memory they have and so on.

Step 13:

Next, create a Key pair to login to your instance. Key airs allow you to connect to your instance securely. This is necessary if we use the SSH utility to launch an instance. We will be using the SSH utility. Therefore its is required for us to create a Key pair.

Name your Key pair.

Key pair type should be RSA, which is RSA encrypted private and public key pair.

Next is the key pair format. The Private key file format is OS-dependent. If you have Mac, or Linux, or Windows 10, then you can use the .pem format.

If you have Windows less than version 10 (Windows 7 or Windows 8), then you can do a little shortcut, and use .ppk which is what you gonna be using for PuTTY and PuTTY is how you do SSH on Windows 7 and Windows 8.

So remember, anything less than Windows 10, use .ppk. Otherwise use .pem.

Since I have a Mac, I will be using .pem

Click Create key pair and it is downloaded for me directly. It is selected for me automatically.

Step 14:

Next, I will have to go into Network Settings. Within the Network Settings, in the Security Groups (Firewall), I will either Create a Security Group or Select Existing Security Group. Remember, we have already created a Security Group. So, we will select that Security Group, by clicking Select existing security group.

Step 15:

Next, we Configure storage. We leave the default settings which are:

1x (volume) 8 GIB gp2 root volume.

Because in the free tier we can get up to 30 GB of EBS General purpose necessary.

A quick note. On the Storage, if we click the link Advanced, you can configure them and see more information. The Delete on termination is set to yes. This means that once we terminate our EC2 instance, then that volume is going to be deleted.

Step 16:

Next, click Advanced Details. In Advanced details, scroll all the way down to the User Data section.

User Data is when we pass a script, to our EC2 instance, to execute on the first launch, of our EC2 instance. And ONLY the first launch. And only the first launch. Scripts are commands.

On the first launch we want to be able to pass the commands from the ec2-user-data.sh file. To do this, from your Code editor or IDE, copy the ec2-user-data.sh commands, and paste them into the User Data section box.

Step 17

Finally, for Summary we want to start 1 instance. And we can review everything. In Free tier, we get 750 hours of t2.micro, running it for one month in our Region, and other information.

So let’s launch this instance. Click Launch instance. The page will reload with Success message. Next, click View all instances, to view all of your instances.

Step 18

On Instances, you will see a list of your instances. Click the refresh button if you do not see your instance there, but it typically will be there, even if in a Pending State.

In Conclusion

Creating an EC2 instance on the AWS cloud provides businesses with a flexible and scalable solution for their computing needs. By following the simple steps outlined above, users can swiftly set up and configure their virtual servers, allowing for seamless deployment of applications and services. With AWS’s extensive range of features and comprehensive management tools, organizations can leverage the power of cloud computing to enhance their productivity, efficiency, and overall performance.

--

--