Cloudformation
/SKILLAWS CloudFormation infrastructure as code for stack management. Use when writing templates, deploying stacks, managing drift, troubleshooting deployments, or organizing infrastructure with nested stac
--- name: cloudformation description: AWS CloudFormation:infrastructure as code for stack management. Use it when writing templates, deploying stacks, managing drift, troubleshooting deployments, or organizing infrastructure with nested stacks. lastupdated : "2026-01-07" docsource :https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/ --- # AWS CloudFormation AWS CloudFormation provisions and manages AWS resources using templates. Define infrastructure as code, use version control, and deploy consistently across environments. ## Table of Contents - [Core Concepts](#core-concepts) - [Common Patterns](#common-patterns) - [CLI Reference](#cli-reference) - [Best Practices](#best-practices) - [Troubleshooting](#troubleshooting) - [References](#references) ## Core Concepts ### Templates JSON orYAML files defining AWS resources. Key sections: - Parameters: Input values - Mappings: Static lookup tables - Conditions: Conditional resource creation - Resources: AWS resources (required) - Outputs: Return values ### Stacks A collection of resources managed as a single unit. Created from templates. ### Change Sets Preview changes before executing updates. ### Stack Sets Deploy stacks across multiple accounts and regions. ## Common Patterns ### Basic Template Structure ``yaml AWSTemplateFormatVersion: '2010-09-09' Description: My infrastructure template Parameters: Environment: Type: String AllowedValues: [dev, staging, prod] Default: dev Mappings: EnvironmentConfig: dev: InstanceType: t3.micro prod: InstanceType: t3.large Conditions: IsProd: !Equals [!Ref Environment, prod] Resources: MyBucket: Type: AWS::S3::Bucket Properties: BucketName: !Sub 'my-app-${Environment}-${AWS::AccountId}' VersioningConfiguration: Status: !If [IsProd, Enabled, Suspended] Outputs: BucketName: Description: S3 bucket name Value: !Ref MyBucket Export: Name: !Sub '${AWS::StackName}-BucketName' ### Deploy a Stack **AWS CLI:** bash # Create stack aws cloudformation create-stack \ --stack-name my-stack \ --template-body file://template.yaml \ --parameters ParameterKey=Environment,ParameterValue=prod \ --capabilities CAPABILITY_IAM # Wait for completion aws cloudformation wait stack-create-complete --stack-name my-stack # Update stack aws cloudformation update-stack \ --stack-name my-stack \ --template-body file://template.yaml \ --parameters ParameterKey=Environment,ParameterValue=prod # Delete stack aws cloudformation delete-stack --stack-name my-stack ### Use Change Sets bash # Create change set aws cloudformation create-change-set \ --stack-name my-stack \ --change-set-name my-changes \ --template-body file://template.yaml \ --parameters ParameterKey=Environment,ParameterValue=prod # Describe changes aws cloudformation describe-change-set \ --stack-name my-stack \ --change-set-name my-changes # Execute change set aws cloudformation execute-change-set \ --stack-name my-stack \ --change-set-name my-changes ### Lambda Function yaml Resources: LambdaFunction: Type: AWS::Lambda::Function Properties: FunctionName: !Sub '${AWS::StackName}-function' Runtime: python3.12 Handler: index.handler Role: !GetAtt LambdaRole.Arn Code: ZipFile: | def handler(event, context): return {'statusCode': 200, 'body': 'Hello'} Environment: Variables: ENVIRONMENT: !Ref Environment LambdaRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: lambda.amazonaws.com Action: sts:AssumeRole ManagedPolicyArns: - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole ### VPC with Subnets ``yaml Resources: VPC: Type: AWS::EC2::VPC Properties: CidrBlock: 10.0.0.0/16 EnableDnsHostnames: true Tags: - Key: Name Value: !Sub '${AWS::StackName}-vpc' PublicSubnet1: Type: AWS::EC