Infrastructure as a Code with AWS
If we do manual way for starting the instances, creating load balancers, managing VPC’s and other services, the same work will be very tough to produce in another region, in another AWS account, or within the same region if everything was deleted.
Wouldn’t it be great, if all of the infrastructure was code ? By using that code we could deploy and create, update, delete our infrastructure in no time.
CloudFormation, it is a declarative way of outlining your AWS Infrastructure, for any resource.
Benefits of AWS CloudFormation
- None of the resources are created manually, which is great for control
- All the changes to the infrastructure are reviewed through code
- Can easily estimate the costs of your resources using CloudFormation Template
- You can apply different saving strategies
- Since it uses declarative programming we are able to create and destroy infrastructure on the fly
Deploying CloudFormation Templates
- One way is by editing templates in the CloudFormation Designer
- Can use console to input parameters
- We can edit templates in YAML file
CloudFormation Building Blocks
- Resources: AWS resources declared in the template
- Parameters : The dynamic input for your template. It is a way for providing the inputs to your AWS CloudFormation template. If the CloudFormation resource configuration is likely to change in future then we can make that as parameter.
- Mappings: The static variable of your template. These are fixed variables. All the values are hardcoded with in template.
- Outputs: All the references to what are the changes have been done. You can not delete a CloudFormation Stack if its outputs are being referenced by another CloudFormation Stack.
- Conditionals: It is the list of conditions to perform resource creation.
- Metadata
Let’s start with Hands-On Part
- In this demo we will create a CloudFormation Stack.
- We will Update and Delete the CloudFormation Stack.
So let’s get started.
In this demo we will be using the sample template written in yaml shown below.
- YAML 1
---
Resources:
MyInstance:
Type: AWS::EC2::Instance
Properties:
AvailabilityZone: ap-south-1a
ImageId: ami-0d81306eddc614a45
InstanceType: t2.micro
- In the template shown above we are creating a resource of type EC2 Instance in the ap-south-1 region, selecting the t2.micro instance type. We are here using the Amazon Linux 2 ami.
YAML 2
Note : The intrinsic function Ref returns the value of the specified parameter or resource.
Parameters:
SecurityGroupDescription:
Description: Security Group Description
Type: String
# our EC2 security group
SSHSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH access via port 22
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: 22
IpProtocol: tcp
ToPort: 22
# our second EC2 security group
ServerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: !Ref SecurityGroupDescription
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 192.168.1.1/32
In this yaml we have defined the parameters “SecurityGroupDescription” and we can use it’s value later. We are using !Ref to refer the parameters or resources. In this yaml we can see two security groups will be created.
YAML 3
Resources:
MyInstance:
Type: AWS::EC2::Instance
Properties:
AvailabilityZone: ap-south-1a
ImageId: ami-0d81306eddc614a45
InstanceType: t2.micro
SecurityGroups:
- !Ref SSHSecurityGroup
- !Ref ServerSecurityGroup
# an elastic IP for our instance
MyEIP:
Type: AWS::EC2::EIP
Properties:
InstanceId: !Ref MyInstance
In the above yaml we are using Ref function in order to get the security groups that we want to apply on that instance. Secondly, we are using the Ref function to refer to the instance created and attaching the elastic ip to that instance.
Now just create a stack on CloudFormation.
- Choose the option to upload a template file and click on next.
This is the github link for the template.
2. Since we have used the parameters in our template. It’s asking for the value of the parameter. It will all by himself add this value to our configuration.
3. Review the stack.
4. Visual Representation for the CloudFormation Stack.
5. Successful implementation of the CloudFormation Stack.
We can move to Resources tab and see all the resources have been created completely.
6. We can entirely delete this infrastructure in one click. Now we will delete the stack so to prevent from unnecessary charges.
Deletion will be initiated and all the resources created will be deleted by CloudFormation itself.
That’s all for now. Thanks for reading.