Hello Folks.. I hope you all are doing fine. In this Blog, we will be learning how to create an ec2 instance with the help of the AWS Cloud Formation template.
Now you must be wondering what is this AWS Cloud formation, So it is a service that is provided by AWS that helps you to model and set up the resources, it is provided by Amazon.
Why use it?
Suppose you got a requirement where you have to create ec2 instances and you are doing it manually and this is the same task that you might need to do in other accounts as well maybe in the future.
So Creating it manually will be very tedious and you can make mistakes it is a time-consuming task.
So here comes the cloud formation where you can maintain a template file for this and by using this you can create the AWS Resources.
Cloud formation deploys the AWS services and also it can scale it must faster and securely.
It is also display as infrastructure automation, like terraform but in the case of cloud formation, it is specific to AWS.
Creating EC2 using a simple cloud formation template file
Let’s take the example of this file that I have created :
Resources: MyNewEC2Instance: Type: AWS::EC2::Instance Properties: ImageId: <ami-id> InstanceType: <ec2-instance-type> SubnetId: <subnet-id> SecurityGroupIds: - <security-group-id> KeyName: <ec2-key-name>
This is a basic format that we follow in order to create a ec2 instance.
In addition to the essential configurations shown above, there are more than 40 parameters that one can configure when launching an EC2 instance.
Let me create a basic template and show you how you can import those on AWS and create a ec2 instance.
Resources: MyEC2Instance: Type: AWS::EC2::Instance Properties: ImageId: ami-0717cbd2f49a61ed0 InstanceType: t2.micro
Here in this, I have used an image id of ubuntu and instance type t2 micro , if you want you can use specific subnets also otherwise if you leave it blank it will take the default values.
Creating a Stack
Once you complete the template file, create the CloudFormation stack. This can be done by clicking on the Create stack button from the CloudFormation console.
Now you will see to enter all the details in order to create the stack and if you want you can also use AWS Command-line interface or maybe an SDK to automate this and minimize the manual intervention.
So in the first option, we will choose the template is a ready option which is also selected by default there.
Now next comes to choose the source, you can either choose any s3 URL or you can also directly upload the template file.
As you can see when you give a template file, it will also give you the s3 location of this file.
After this you have to specify the stack details
for the stack name, Let me write ec2, you can write anything there as per your convenience. For the parameters, I will leave this blank for now and proceed to the next.
Now you have to configure the stack options
I will keep everything by default for now, or if you want you can edit other options such as notifications options, Stack failure options, Stack policy, and rollback configuration as per your requirements, after this just click on next and you’re good to go.
At the last stage, you will be asked to review all the configurations and then you can finally click and create the stack. After this, a dashboard will appear and in the events, you can see the status will be create in progress.
You can refresh it after few seconds and you will see the status as create complete
We can also confirm this by going to ec2 Dashboard , You will see an instance is running
This was all about how to create an ec2 instance with the help of a cloud formation template. This was just a basic configuration. You can try for more if you want. To learn more you can visit the official website.