AWS Cloud Formation.

๐๐ฆ๐๐ณ๐จ๐ง ๐๐ฅ๐จ๐ฎ๐ ๐ ๐จ๐ซ๐ฆ๐๐ญ๐ข๐จ๐ง (๐๐ ๐) is a powerful service offered by Amazon Web Services (AWS) for infrastructure as code (IAC) management. It allows you to define and provision AWS infrastructure resources using templates. Hereโs a deep dive into AWS CloudFormation and how to write CFTs, along with features like Drift Detection:
๐. ๐๐ก๐๐ญ ๐ข๐ฌ ๐๐๐ ๐๐ฅ๐จ๐ฎ๐๐ ๐จ๐ซ๐ฆ๐๐ญ๐ข๐จ๐ง (๐๐ ๐)?
- AWS CloudFormation is a service that helps you model and provision AWS infrastructure resources in a safe and predictable manner.
โ It enables you to define your infrastructure in code using JSON or YAML templates.
๐. ๐๐ซ๐ข๐ญ๐ข๐ง๐ ๐๐ฅ๐จ๐ฎ๐๐ ๐จ๐ซ๐ฆ๐๐ญ๐ข๐จ๐ง ๐๐๐ฆ๐ฉ๐ฅ๐๐ญ๐๐ฌ:
- CFT templates are text files written in JSON or YAML.
โ Templates define the AWS resources, their properties, dependencies, and other configuration details.
๐. ๐๐๐ฒ ๐๐จ๐ฆ๐ฉ๐จ๐ง๐๐ง๐ญ๐ฌ ๐จ๐ ๐๐ ๐:
- Resources: These are the AWS resources you want to provision, such as EC2 instances, S3 buckets, RDS databases, etc.
โ Parameters: These are input values that can be customized when creating a stack.
โ Mappings: Define a mapping between input values and corresponding resource properties.
โ Conditions: Specify when resources should be created or when they should be skipped.
โ Outputs: Declare the values you want to retrieve once the stack is created.
๐. ๐๐๐ง๐๐๐ข๐ญ๐ฌ ๐จ๐ ๐๐๐ ๐๐ฅ๐จ๐ฎ๐๐ ๐จ๐ซ๐ฆ๐๐ญ๐ข๐จ๐ง:
- Infrastructure as Code (IaC): CFT allows you to define and manage your infrastructure as code, making it versionable, repeatable, and easier to manage.
โ Resource Management: CFT handles resource creation, updates, and deletion, ensuring resource consistency.
โ Automation: You can automate the provisioning and management of your infrastructure with CFT.
โ Change Sets: Before executing changes, you can review them using change sets to avoid unintended modifications.
โ Rollback: If a stack update fails, CFT can automatically roll back to the previous state to maintain stability.
โ Cost Estimation:You can estimate the cost of a CFT stack before creating or updating it.
โ Integration: CloudFormation is integrated with AWS Identity and Access Management (IAM) for security and control.
โ Drift Detection: Continuously monitor and detect differences between the desired stack configuration and the actual stack resources.
๐. ๐๐ซ๐ข๐๐ญ ๐๐๐ญ๐๐๐ญ๐ข๐จ๐ง:
- Drift detection in CFT allows you to identify differences between the desired stack configuration defined in your template and the current stack resources.
โ It helps you track changes and understand if the stack has drifted from its expected state.
โ Drift detection is useful for ensuring that your infrastructure stays compliant with your defined configurations.
๐. ๐๐จ๐ฐ ๐ญ๐จ ๐๐ซ๐ข๐ญ๐ ๐๐ ๐๐ฌ:
- Use the JSON or YAML template format to define your AWS resources, parameters, and other template components.
โ Define the resource properties, dependencies, and other settings for each resource.
โ Use CloudFormation intrinsic functions like Fn::Ref
, Fn::GetAtt
, and Fn::Sub
for dynamic configurations.
โ Use AWS-specific extensions like !Sub
and !ImportValue
for parameterization and cross-stack referencing.
โ Use conditions to control resource creation and to make your templates more flexible.
๐. ๐๐ฑ๐๐ฆ๐ฉ๐ฅ๐๐ฌ ๐จ๐ ๐๐ฅ๐จ๐ฎ๐๐ ๐จ๐ซ๐ฆ๐๐ญ๐ข๐จ๐ง ๐๐ฌ๐ ๐๐๐ฌ๐๐ฌ:
- Creating a VPC and related resources: Define a VPC, subnets, security groups, and routing tables.
โ Deploying an application stack: Define EC2 instances, load balancers, databases, and other application components.
โ Setting up monitoring and alarms:*Define CloudWatch alarms, SNS topics, and metric filters.
โ Provisioning storage resources: Define S3 buckets, EBS volumes, and RDS databases.
โ Managing security and compliance: Define IAM roles, policies, and security group rules.
In summary, AWS Cloud Formation is a fundamental tool for managing and provisioning AWS infrastructure as code. By writing templates, you can automate the deployment and management of resources, improve consistency, and leverage features like drift detection to maintain a reliable and compliant infrastructure. Itโs an essential component of infrastructure as code and DevOps practices in AWS.
๐๐จ๐ฐ ๐ญ๐จ ๐๐ซ๐๐๐ญ๐ ๐๐๐ ๐ข๐ง๐ฌ๐ญ๐๐ง๐๐ ๐๐ฒ ๐ฎ๐ฌ๐ข๐ง๐ ๐๐ ๐
To create an Amazon EC2 instance using AWS Cloud Formation (CFT), you need to define the EC2 resource within your CloudFormation template and then create a CloudFormation stack. Hereโs a step-by-step guide on how to do it:
๐๐ญ๐๐ฉ ๐: ๐๐ซ๐๐๐ญ๐ ๐ ๐๐ฅ๐จ๐ฎ๐๐ ๐จ๐ซ๐ฆ๐๐ญ๐ข๐จ๐ง ๐๐๐ฆ๐ฉ๐ฅ๐๐ญ๐
1. Open your favorite text editor and create a CloudFormation template in either JSON or YAML format.
2. Define the structure of your CloudFormation template, which includes specifying the resources you want to create. For an EC2 instance, you should use the AWS::EC2::Instance
resource type.
3. Define the properties for the EC2 instance, such as the instance type, key pair, security groups, and any other configuration details.
Hereโs a simplified YAML example of how to define an EC2 instance in your template:
In this example, weโre creating an EC2 instance with a t2.micro
instance type, associating it with the my-key-pair
key pair, and attaching it to a security group identified by sg-12345678
. The ImageId
property specifies the Amazon Machine Image (AMI) to use.
๐๐ญ๐๐ฉ ๐: ๐๐ซ๐๐๐ญ๐ ๐ ๐๐ฅ๐จ๐ฎ๐๐ ๐จ๐ซ๐ฆ๐๐ญ๐ข๐จ๐ง ๐๐ญ๐๐๐ค
1. Log in to the AWS Management Console.
2. Navigate to the AWS CloudFormation service.
3. Click on โCreate stackโ to start the stack creation process.
4. Choose the CloudFormation template that youโve created in Step 1, either by uploading it or specifying the Amazon S3 URL.
5. Click โNextโ and provide a stack name, set any optional parameters, and configure any advanced options as needed.
6. Review the details, acknowledge the creation of IAM resources, and click โCreate stackโ to initiate the stack creation.
๐๐ญ๐๐ฉ ๐: ๐๐จ๐ง๐ข๐ญ๐จ๐ซ ๐๐ญ๐๐๐ค ๐๐ซ๐๐๐ญ๐ข๐จ๐ง
1. Once youโve initiated the stack creation, CloudFormation will orchestrate the provisioning of the EC2 instance and other associated resources based on your template.
2. You can monitor the stackโs status in the AWS CloudFormation console.
๐๐ญ๐๐ฉ ๐: ๐๐๐๐๐ฌ๐ฌ ๐๐จ๐ฎ๐ซ ๐๐๐ ๐๐ง๐ฌ๐ญ๐๐ง๐๐
1. After the stack creation is complete, you can access your EC2 instance via SSH or any other method youโve configured in your template.
2. If you need to retrieve the public DNS or IP address of your instance, you can use the AWS Management Console, AWS CLI, or SDKs to query the information.
This is a basic example of how to create an EC2 instance using AWS CloudFormation. In practice, you can create more complex templates that include additional resources, specify dependencies, and apply conditions, enabling you to define and provision your entire infrastructure as code.
Ready to harness the power of Infrastructure as Code (IAC) with AWS CloudFormation? Letโs connect to explore advanced Cloud Formation techniques, discuss best practices, and uncover the secrets of drift detection. Together, weโll optimize your infrastructure management.
Connect me here
linkedin.com/in/raj-nandgaonkar-555848157
Thankyou.
Subscribe to my newsletter
Read articles from Raj Nandgaonkar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Raj Nandgaonkar
Raj Nandgaonkar
AWS Engineer | Cloud Solutions Architect | DevOps Enthusiast Hi there! Iโm Raj Nandgaonkar, an AWS Engineer with a passion for building scalable, efficient, and cost-effective cloud solutions. With experience in cloud architecture, infrastructure automation, and DevOps practices, I specialize in leveraging Amazon Web Services (AWS) to deliver high-performance applications and systems. Iโve had the privilege of working on a variety of projects, from designing serverless architectures to implementing secure cloud-native applications. My expertise includes EC2, Lambda, S3, CloudFormation, and many other AWS services. Iโm also a strong advocate for infrastructure-as-code and love using tools like Terraform and AWS CloudFormation to streamline the deployment process. Through this blog, I aim to share insights, tutorials, and best practices to help you navigate the AWS ecosystem and make the most of its powerful services. Whether you're a beginner looking to get started or an experienced developer seeking advanced cloud strategies, there's something here for you. Letโs connect and learn together as we explore the ever-evolving world of AWS! connect me on https://www.linkedin.com/in/raj-nandgaonkar-555848157/