TechWriterDev
  • Cloud
    • AWS
      • 00_Doubts
      • CloudPractitioner
        • Cloud Computing
        • AWS Global Infrastructure
        • Introduction to AWS EC2
        • Elastic load balancer(ELB)
        • 04_Messaging_Queuing
        • Aditional Computing Service
        • Accessing AWS resources
        • AWS Networking
        • Storage
        • Amazon Database Solutions
        • Monitoring Tools
        • AWS Security
        • Distributed Denial Of Service Attacks
      • DeveloperAssociate
        • References
        • AWS DVA-C02 Services Index
        • Services
          • 00_IAM
            • Identity and Access Management (IAM)
            • Account Protection Mechanisms
            • Access Mechanism of AWS Resources
            • Security Tools
            • Responsibility Model
            • Advanced Concepts
          • 01_EC2
            • Elastic Compute Cloud (EC2)
            • EC2 Volume Types
            • Amazon Machine Image (AMI)
            • AWS charges for IPv4 address
          • 02_SecurityGroups
            • Security Groups
          • 03_Elastic_LoadBalancing
            • Terminology
            • Elastic load balancer
            • Features
            • Basics
          • 04_AutoScaling
            • Auto Scaling
          • 05_RDS
            • Relational Database Service (RDS)
            • Aurora
            • Security
            • RDS Proxy
          • 06_ElastiCache
            • Cache
            • Cache Offerings
          • 07_Route53
            • Basics of DNS
            • Route 53
          • 08_VPC
            • Virtual Private Cloud (VPC)
          • 09_S3
            • Simple Storage Service (S3)
            • S3 Features
            • S3 Encryption
            • S3 Features
            • S3 Bucket Policy and IAM Policy
          • 10_ECS
            • Elastic Container Service (ECS)
            • Elastic Container Registry (ECR)
            • AWS Copilot
          • 11_EKS
            • Elastic Kubernetes Service (EKS)
          • 12_SDK_CLI_Tips
            • Access AWS Resources
          • 13_CloudFront
            • Cloud Front
          • 14_Messaging
            • Simple Queue Service (SQS)
            • Simple Notification Service (SNS)
            • Fan Out Pattern
            • Kinesis
            • Compare and Contrast
          • 15_ElasticBeanStalk
            • Elastic Beanstalk
          • 16_CloudFormation
            • CloudFormation
            • Dynamic References
          • 17_Monitoring
            • AWS Monitoring
            • AWS CloudWatch
            • CloudWatch Alarms
            • Synthetics Canary
            • Amazon EventBridge (formerly CloudWatch Events)
            • X-Ray
            • OpenTelemetry
            • CloudTrail
          • 18_Lambda
            • Lambda
            • Lambda Integrations
            • Configuring Lambda
            • Lambda Layers
          • 19_API_Gateway
            • API Gateway
            • API Gateway Integrations
          • 20_DynamoDB
            • DynamoDB
            • Operations
            • Indexes
            • DynamoDB Accelerator (DAX)
            • DynamoDB Streams
            • Transactions
            • Integrations
          • 21_CICD
            • CICD
            • CodeCommit
            • CodePipeline
            • CodeBuild
            • CodeDeploy
            • CodeArtifact
            • CloudGuru
          • 22_SAM
            • Serverless Application Model (SAM)
          • 23_CDK
            • Cloud Development Kit (CDK)
          • 24_StepFunctions
            • Step Functions
            • Types of step function
          • 25_AppSync
            • AppSync
          • 26_Amplify
            • Amplify
          • 27_STS
            • Security Token Service (STS)
          • 28_DirectoryService
            • Active Directory
          • 29_KMS
            • Encryption
            • KMS API
            • Features
            • Cloud Hardware Security Module (HSM)
          • 30_SSM_Store
            • SSM Parameter Store
          • 31_SecretsManager
            • Secrets Manager
          • 32_Cognito
            • Cognito
      • Questions
        • AWS_Region
        • EC2
        • IAM
  • Database
    • MongoDb
      • Mongo db Basics
      • Mongo DB Atlas
      • Document
      • Import-Export based on Data Format
      • Mongo Shell Commands
      • Query Operators
      • Indexes
      • Upsert
      • MongoDB Aggregation Framework
      • Aggregation Framework Operators
    • PostgreSQL
      • POSTGRE SQL DataTypes
      • About table
      • Constraints
  • Technologies
    • RabbitMQ
      • RabbitMQ Concepts
      • Introduction to Exchanges
      • Introduction to Queues
    • Terraform
      • 00_Introduction
      • Configuration blocks
      • Commands
      • Variables
      • Terraform Cloud
      • Modules
  • Languages
    • Java
      • Logging
        • Getting Started
      • 00_Core
        • 00_Basics
          • Java Vs C++
          • Object oriented principles
          • Steps to compile a java program
          • JVM Internals
          • Understanding Java Development Kit
          • What is JIT Compiler?
          • Java data types
          • 07_identifiers_type_conversion
          • 08_references_and_packages
          • Steps for attaching scanner
        • Concurrency
          • 00_Threads
            • Threads
          • 01_ExecutorFramework
            • Executor Framework
            • Asynchronous Computation
      • 01_Backend
        • 01_HttpAndWebServerBasics
          • HTTP
          • Content Type
          • Web Server
        • 02_J2EE_Basics
          • J2EE_Basics
          • Why HttpServlet classs is declared as abstract class BUT with 100 % concrete functionality ?
        • 03_TomCatAndSession
          • What is a Session?
          • WebContainer
        • 04_PageNavigation
          • Cookies Additional Information
          • Page Navigation Techniques
        • 05_AboutServlet
          • CGI v/s Servlet
          • Executor Framework
          • Servlet Life cycle
          • SERVLET CONFIG
          • Servlet Context
          • Servlet Listener (web application listener)
        • 08_SpringBoot
          • Spring Boot
          • Some common annotations used in spring eco system
        • 09_SpringDataJPA
          • Spring Data JPA
        • Java_Language_Changes
          • JDK enhancement tracking reference
        • 06_ORM_Hibernate
          • readmes
            • Hibernate
            • Advantages of Hibernate
            • Hibernate Caching
            • Hibernate API
            • Hibernate Query API
            • Hibernate Annotations and JPQL
            • Entity and Value Type
        • 07_SpringFramework
          • bean_validation
            • Bean Validation
          • core
            • readme
              • Spring
              • Spring Framework Modules
              • Spring MVC Request flow
              • Dependency Injection
              • Spring Beans
              • 06_Spring_Framework_Annotations
      • 03_Tools
        • Maven
          • Maven
  • SoftwareEngineering
    • DesignPatterns
      • Notes
        • Basics
        • OOP
        • SOLID Principles
        • 03_Creational
          • Abstract Factory (aka Kit)
          • Builder
          • Factory Method (aka Virtual constructor)
          • Prototype
          • Singleton
        • 04_Structural
          • Adapter (aka Wrapper)
          • Bridge (aka Handle | Body)
          • Composite
          • Decorator (aka Wrapper)
          • Facade
          • Flyweight
          • Proxy (aka Surrogate)
        • 05_Behavioral
          • Chain of Responsibility
          • Command (aka Action | Transaction)
          • Iterator (aka Cursor)
          • Observer (aka Publish-Subscribe | Dependents)
          • Strategy (aka Policy)
    • Principles
      • REST
        • REST
  • Tools
    • Containers
      • Docker
        • Docker
        • Docker Image
        • Commands
        • Compose
        • Best Practices
      • Kubernetes
        • Kubernetes
    • VCS
      • Git
        • Quick reference of useful Git commands
Powered by GitBook
On this page
  • Iac
  • Icon
  • About
  • Concepts
  • Working
  • Rollback
  • Service Role
  • Capabilities
  • Deletion Policy
  • Delete
  • Retain
  • Snapshots
  • Stack Policies
  • Termination Protection
  • Custom Resources
  • StackSets
  • References
  1. Cloud
  2. AWS
  3. DeveloperAssociate
  4. Services
  5. 16_CloudFormation

CloudFormation

Previous16_CloudFormationNextDynamic References

Last updated 4 months ago

Iac

  • enables us to provision and support your computing infrastructure using code instead of manual processes and settings.

  • Manual infrastructure management is time-consuming and prone to error, especially when you manage applications at scale.

  • Infrastructure as code lets you define your infrastructure's desired state without including all the steps to get to that state.

  • It automates infrastructure management so developers can focus on building and improving applications instead of managing environments.

  • Organizations use infrastructure as code to control costs, reduce risks, and respond with speed to new business opportunities.

  • The most common use of IaC is in software development to build, test, and deploy applications.

  • It lets you,

    • Reduce configuration errors and simplify infastructure management

    • Easily duplicate environments

    • Track configuration changes

    • Iterate on best practices

Icon

Cloud Formation

About

  • CloudFormation is Iac service offered by AWS.

  • Its declarative, meaning developer does not need to say how the resource needs to be provisioned, the developer just needs to describe the resources needed and CloudFormation will take care of provisioning them in the order required.

Concepts

Stack

  • When you use CloudFormation, you manage related resources as a single unit called a stack.

  • You create, update, and delete a collection of resources by creating, updating, and deleting stacks.

  • If you need to make changes to the running resources in a stack, you update the stack.

  • Before making changes to your resources, you can generate a change set, which is a summary of your proposed changes.

  • Change sets allow you to see how your changes might impact your running resources, especially for critical resources, before implementing them.

Template

  • A CloudFormation template (CFT) is a JSON or YAML formatted text file. You can save these files with any extension, such as .json, .yaml, .template, or .txt. CloudFormation uses these templates as blueprints for building your AWS resources.

  • Eg:

    {
        "AWSTemplateFormatVersion": "2010-09-09", 
        "Description": "A sample template",
        "Resources": {
            "MyEC2Instance": {
                "Type": "AWS::EC2::Instance",
                "Properties": {
                    "ImageId": "ami-0ff8a91507f77f867",
                    "InstanceType": "t2.micro",
                    "KeyName": "testkey",
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/dev/sdm",
                            "Ebs": {
                                "VolumeType": "io1",
                                "Iops": 200,
                                "DeleteOnTermination": false,
                                "VolumeSize": 20
                            }
                        }
                    ]
                }
            }
        }
    }
    AWSTemplateFormatVersion: 2010-09-09 #identifies capabilities of template
    Description: A sample template
    Resources:
        MyEC2Instance:
            Type: 'AWS::EC2::Instance'
            Properties:
            ImageId: ami-0ff8a91507f77f867
            InstanceType: t2.micro
            KeyName: testkey
            BlockDeviceMappings:
                - DeviceName: /dev/sdm
                Ebs:
                    VolumeType: io1
                    Iops: 200
                    DeleteOnTermination: false
                    VolumeSize: 20

Template Components

AWS Template Format Version

  • Identifies capabilities of template.

Description

  • Comments about code

Resources

  • Represents difference AWS components needed to configure your application or infrastructure.

  • Resources can be declared and can be referenced.

  • Resource identifiers are of the form,

        service-provider:service-name:data-type-name
  • Almost all AWS services are available as part of CloudFormation resources and if any resources are not available, they can be created using CloudFormation custom resources.

  • Fn:Ref (short-hand !Ref) in templates can be used to reference parameters and components within a template.

Parameters

  • Allows to specify inputs to your AWS CloudFormation template.

  • They allow to make reusable templates.

  • Following types are allowed,

    • String

    • Number

    • CommaDelimitedList

    • List

    • AWS-Specific parameter

    • List AWS specific Parameter

    • SSM Parameter

  • Can place constraints on parameters.

Pseudo Parameters

  • These are AWS predefined parameters which can be referenced in CloudFormation script.

Mappings

  • These are fixed variables within your CloudFormation.

  • Very handy to differentiate between different environments, regions and AMI types.

  • They are great when you want configurtion based approach based on values chosen at run time.

Outputs

  • The outputs declares optional output values that can be imported by using !ImportValue into other stacks, provided they are exported first by using Export section.

  • Suppose a stack exports a value then other stacks that use this exported value should not reference the exported value to successfully delete the stack.

  • Exported output names must be unique in the AWS region.

Conditional

  • Allows to give conditions within templates to feature conditional resource creation.

Intrinsic Function

  • These are functions that provide inbuilt functions to manage the stack in CloudFormation

Working

  • S3 is used as the CloudFormation script repository and these scripts are referenced in CloudFormation.

  • Updation is done by reuploading the change set to S3.

  • Stacks are identified by name.

  • Deletion of a stack also deletes the artifact that was created by the CloudFormation.

Rollback

  • Following are the stack failure options available as per the scenarios possible,

    • Stack Creation Fails: Everything gets roll backed (gets deleted) or preserve the successfully provisioned ones.

    • Stack Updation Fails: Everything gets roll backed _(gets deleted) to previous working state.

    • Rollback Failure: If failure happens while rollback, fix the resources manually and then issue ContinueUpdateRollback API from Console.

Service Role

  • A service role is an AWS Identity and Access Management (IAM) role that allows AWS CloudFormation to create, update, or delete stack resources.

  • If you do not provide a service role, CloudFormation uses the credentials of the IAM principal to perform the stack operations.

  • If you create a service role for CloudFormation and specify the service role during stack creation, then CloudFormation uses the credentials of the service role to perform the operations, instead of the credentials of the IAM principal.

  • Using this will allow to provide access based on Least Priviledged Principle.

  • User must have iam::PassRole permissions to give a role to a specific service.

Capabilities

  • CAPABILITY_NAMED_IAM and CAPABILITY_IAM is necessary when creation IAM resources and use the former is the resources are named. If not specified you will receive InsufficientCapabilitiesException.

  • CAPABILITY_AUTO_EXPAND is necessary when you include Macros or Nested Stacks to perform dynamic transformations.

Deletion Policy

Delete

  • To control what happens when the CloudFormation is deleted or when the resource is removed from CloudFormation.

  • It also provides safety measures to preserve and backup resources.

  • Default Policy is to delete the resource. However for S3 bucket, delete policy will not delete the bucket if it is not empty. To fix this, either empty the bucket manually or make a custom resource to delete the S3 bucket.

Retain

  • This allows to preserve the resources in case of CFT deletes.

Snapshots

  • This policy enables CFT to make a snapshot before deletion of resources.

Stack Policies

  • All update actions are allowed on all resources by default.

  • Stack policy is a JSON document that defines the update actions that are allowed on specific resources during stack updates.

Termination Protection

  • By default this protection is deactivated.

  • Once enabled, on stack deletion the resource will not be deleted until this protection is deactivated.

Custom Resources

  • Define resources not supported by CloudFormation.

  • Define custom provisioning logic for resources that can be outside CloudFormation like on-premise.

  • This also allows to run custom scripts during create/update/delete through Lambda functions.

  • Template is defined using AWS::CloudFormation::CustomResource or Custom::MyCustomResourceTypeName type.

StackSets

  • Create, Update or delete stacks across multiple accounts and regions with a single operation/template.

  • When you update a stack set, all associated stack instances are updated throughout all accounts and regions.

  • Most common use case are to apply this to AWS Organization (which is a group of accounts). This can only be created by user who has administrative access.

References

All resource types details can be found .

List of parameters can be found .

More about mappings can be found .

Conditions can be found .

Full reference to all the functions are available in the .

Working of CloudFormation can be found .

here
here
here
here
documentation
here
AWS CloudFormation Blog
Infrastructure as Code (IaC)