Launch an EC2 instance in a VPC using a Cloud Formation Template
Hi everyone, in this post I thought to write about how to launch an EC2 instance in a private VPC using AWS cloud formation template in YAML format. Before that, I would like to thank my colleagues for inspiring me to write this article.
What does the cloud formation mean ?
First of all, I talk about the cloud formation service in AWS. While we are using AWS resources, we have to manually create and configure them again and again. By using cloud formation templates, we can do it easily. This templates describe the resources which are going to use and their dependencies, therefore we can easily configure them, launch and delete them together as a stack.
For setting up the fundamentals to do this, first let’s look in to what is EC2 instances and VPC.
What is Amazon VPC ?
Amazon Virtual Private Cloud (VPC) likes your private working table. It is a service that we can launch our AWS resources in a logically isolated virtual network which we defined. We have the complete control in our VPC to select our own IP address range, creation of subnets, configure route table and creation of security groups. And also we can easily connect to the internet from our VPC through public subnets.
What is AWS EC2 ?
Imagine that there is our own computer in the VPC. That’s called Amazon Elastic Compute Cloud(EC2) which is a service provided by amazon. It provides secure and re sizable compute capacity in the cloud. Amazon EC2 offers the compute platform with choice of processor, storage, networking, operating system, and purchase model. We must do a proper selection according to our needs. Always try to select free and minimum valued services for the project.
Now you have a basic idea about the VPC and EC2. Let’s move in to our today task. Below figure shows that how cloud formation works.
First you have to create a yaml file to write the cloud formation template. The first part of the code is the user inputs. From that you can give user inputs according to your needs likes key pair name, EC2 instance type, EC2 image id and so on.
Then you have to create an IAM role for your task and give permission to use AWS resources and configure them. Make sure not to give full access for AWS resource , give access for needed resources in a well restricted way. Otherwise you have to face lot of troubles and bills for services that we are using unknowingly. Example code is below for creation of IAM role. In here, give access for EC2 instance profile to create Elastic Loadbalancer and IAM role.
After that you have to create a VPC. In here you can give suitable IP range for our task.
Then you have to configure public subnet and private subnet. For that, should write a route table for both public and private subnets. I attached the git hub repository at the end. By studying that you can get an idea about how to configure both public and private subnets.
And also you have to create a security group for our task. You can easily create a security group as below. For that you have to give VPC id which is created above.
Finally you have to create the EC2 instance in the created VPC as below exmple. For that you should give the image id, instnace type, IAM instance profile name and so on.
Following above steps you can easily create an EC2 instance in a VPC. You can launch cloud formation template via AWS Console or AWS CLI.
- In AWS CLI you can launch cloud formation template by following command. Before that you have to configure AWS account in your command prompt and download key pair of your account and copy it in to cloud formation template location (In Windows you have to change the access for the key pair file manually).
2. In AWS Console, It is a very easy to launch cloud formation template because of user friendly GUI.
Hope you got idea to launch EC2 in a private VPC using a cloud formation template correctly. The git hub repository URL of full code for this task is shown below. If you have any question regarding this, you can refer that.
And also you can put a comment below.