Wednesday, October 22, 2025

AWS - Cloud Formation Elements & Templates

 An AWS CloudFormation template is a JSON or YAML formatted text file. It usually contains these main sections:

  1. AWSTemplateFormatVersion (optional)

    • The template version (latest = "2010-09-09").

  2. Description (optional)

    • Human-readable description of the template’s purpose.

  3. Metadata (optional)

    • Additional information about the template (can be used by tools).

  4. Parameters (optional)

    • Input values that let you customize templates at runtime.

  5. Mappings (optional)

    • Hardcoded lookup tables (e.g., AMI IDs per region).

  6. Conditions (optional)

    • Control resource creation depending on parameter values or other logic.

  7. Transform (optional)

    • For including and processing macros (like AWS::Include or AWS::Serverless).

  8. Resources (required) 

    • The core section – defines the AWS resources (EC2, S3, Lambda, etc.) that CloudFormation creates and manages.

  9. Outputs (optional)

    • Values you want to return (e.g., VPC ID, Load Balancer DNS, etc.).


Example 1: Minimal Template (S3 Bucket) — JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "Simple S3 bucket example", "Resources": { "MyS3Bucket": { "Type": "AWS::S3::Bucket", "Properties": { "BucketName": "my-sample-bucket-12345" } } } }

Example 2: EC2 Instance with Parameters — JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "Launch an EC2 instance with a KeyPair", "Parameters": { "KeyName": { "Description": "Name of an existing EC2 KeyPair", "Type": "AWS::EC2::KeyPair::KeyName" } }, "Resources": { "MyEC2Instance": { "Type": "AWS::EC2::Instance", "Properties": { "InstanceType": "t2.micro", "ImageId": "ami-0c02fb55956c7d316", "KeyName": { "Ref": "KeyName" } } } }, "Outputs": { "InstanceId": { "Description": "EC2 Instance ID", "Value": { "Ref": "MyEC2Instance" } } } }

Example 3: Using Mappings + Conditions — JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "EC2 with region-specific AMIs", "Mappings": { "RegionMap": { "us-east-1": { "AMI": "ami-0c02fb55956c7d316" }, "us-west-2": { "AMI": "ami-08962a4068733a2b6" } } }, "Parameters": { "EnvType": { "Type": "String", "AllowedValues": ["dev", "prod"] } }, "Conditions": { "CreateProdResources": { "Fn::Equals": [ { "Ref": "EnvType" }, "prod" ] } }, "Resources": { "MyEC2Instance": { "Type": "AWS::EC2::Instance", "Condition": "CreateProdResources", "Properties": { "InstanceType": "t2.micro", "ImageId": { "Fn::FindInMap": [ "RegionMap", { "Ref": "AWS::Region" }, "AMI" ] } } } } }


No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...