Automating Cloud Resources with AWS CloudFormation
In this blog, we are going to explore another service of AWS i.e. CloudFormationTempale (AWS CFT). It is the template that helps in cloud formation. Cloud in this case is AWS i.e. creating, managing, and updating the cloud resources.
AWS CFT implements the principle of IAC which is not implemented by the AWS CLI. There are different IAC tools like Terraform, Crossplane but AWS CFT is majorly for the AWS cloud only. IAC is a concept where we write codes to create resources in the cloud. IAC tool acts as a middleman between the user and the cloud provider.
For quick actions, we can simply use the AWS CLI, but to create multi-structured resources, we can use AWS CloudFormation (AWS CFT). AWS CFT supports JSON and YAML templates. YAML is the preferred template as it is the most widely used template. But if you are comfortable with JSON then it is also good to use, the limitation of JSON is that we cannot make comments in between the codes which makes it difficult to understand the code if it is complex.
To get deep knowledge about AWS CFT use the official documentation link:
https://docs.aws.amazon.com/cloudformation/
Let's dive into the AWS CFT practically.
Log into your AWS account. Search for CloudFormation and click on it.
In the cloud formation page, click on Create Stack, stack is the one that implements the template that we have written.
Then click on Create template in designer.
You will get into this page, you can simply drag the resources in the plane and the code will be generated in JSON or YAML based on your requirement.
We simply dragged the bucket and the code is generated in the below terminal.
Resources:
S3BUCKET:
Type: 'AWS::S3::Bucket'
Properties: {}
The code snippet represents an AWS CloudFormation template to create an Amazon S3 bucket. The logical name is "S3BUCKET," and it specifies the resource type as 'AWS::S3::Bucket.' The template is currently basic, without additional properties, indicating a simple S3 bucket creation.
Since we have already created a template we will upload the yaml template.
Our template file is uploaded. The sample yam file we uploaded is:
Resources:
S3BUCKET:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: "demo-aws-subash1122"
VersioningConfiguration:
Status: Enabled
Give a name to the stack and skip all the pages and submit it.
We can see our resource is being created. Let's verify whether the bucket is created or not.
The bucket is created successfully along with a template file in S3. AWS Cloud saves all the templates in the S3 bucket that we created.
In Cloudformation there is a feature of drift, that allows you to identify and understand any changes made to the stack resources outside of CloudFormation.
Let's delete the bucket that we had created and check the drift status in cloudformation.
The bucket is deleted.
Here we can see in the drifted results that the bucket is deleted.
In AWS CloudFormation, "drift" detects the difference between the expected stack resource configuration and the actual configuration of those resources in the deployed stack
Again let's create an Instance using the AWS CloudFormation Template.
createinstance.yaml
AWSTemplateFormatVersion: "2010-09-09"
Description: "This is our first instance using CloudFormation"
Resources:
mydemoinstance:
Type: 'AWS::EC2::Instance'
Properties:
ImageId: ami-0c7217cdde317cfec
KeyName: nod
SecurityGroupIds:
- sg-047bf8e20308ceb4f
InstanceType: t2.micro
Note: for key pair either use existing one or create the new key pair from the EC2dashboard
Let's upload it in the cloud formation.
Our template file is uploaded and named the stack as ec2-instance.
We can see the creation is completed and we will verify it in ec2 dashboard.
Hence the instance is also created successfully. You can also try the same template to create the instance and just replace the security groups and key pair files.
We have successfully accessed the instance that we have created using the CloudFormation.
Note: Don't forget to delete the resources that you created earlier and stay safe from the charges of AWS services
In a nutshell, AWS CloudFormation is like a wizard for setting up our cloud resources on Amazon. It uses written instructions (templates) to create and manage your cloud resources, making it easier and more consistent. We showed how it can create an S3 bucket and an EC2 instance just by following the template. This is handy for automating and organizing your cloud infrastructure without manual hassle.
Happy Learning!!
Subscribe to my newsletter
Read articles from Subash Neupane directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Subash Neupane
Subash Neupane
Computer Science graduate