Skip to content

A project by

All articles →
Amazon Web Services

AWS CloudFormation Cheatsheet

Christopher Klein IT Architect

· 4 min read

General

Return values of !Ref and !GetAtt are not consistent

Depending on the resource, !Ref returns the name or the ARN, and the other value is only available through !GetAtt:

Service !Ref !GetAtt
AWS::ECS::Cluster Name Arn
AWS::ECS::Service Arn Name
AWS::ECS::TaskDefinition Arn TaskDefinitionArn

A full list can be found in the !Ref and !GetAtt cheatsheet.

Stacks

Export a list as output and import the list in another stack.

If you need to export a list of resource identifiers, e.g. all subnets of a VPC, as an output in one stack, you can join the subnets into a comma-separated string (outputs cannot export lists). Note that AWS::EC2::VPC has no Subnets attribute, so you have to reference the subnet resources yourself:

# environment-stack.yaml
Resources:
# ....
Outputs:
  AllSubnets:
    Description: All subnets
    Value: !Join
        - ','
        - [ !Ref SubnetA, !Ref SubnetB ]
    Export:
        Name: !Sub "${AWS::StackName}-AllSubnets"

In your other stack, you can then import the output with the following definition:

# other-stack.yaml
Parameters:
  EnvironmentStackName:
    Description: "Name of the stack containing VPC, subnets etc."
    Type: String
    Default: "environment"

Resources:
  MyCodeBuildProject:
    Type: AWS::CodeBuild::Project
    Properties:
      # ...
      VpcConfig:
        # ... VpcId, SecurityGroupIds
        Subnets:
          Fn::Split:
            - ","
            - Fn::ImportValue:
                !Sub "${EnvironmentStackName}-AllSubnets"

Exporting the first entry of a list and referencing it

If you just need the first entry of a list, e.g. the first subnet, you can use the following definition:

# environment-stack.yaml
Resources:
# ....
Outputs:
  FirstSubnet:
    Description: First subnet
    Value: !Select [0, [ !Ref SubnetA, !Ref SubnetB ]]
    Export:
        Name: !Sub "${AWS::StackName}-FirstSubnet"

It can then be referenced in the other stack with

# other-stack.yaml
Resources:
  MyLambda:
    Type: AWS::Lambda::Function
    Properties:
      VpcConfig:
        SubnetIds:
          - !ImportValue
            'Fn::Sub': '${EnvironmentStackName}-FirstSubnet'

CodeBuild and CodePipeline

Pushing a Docker image into ECR with CodeBuild

If you are using CodeBuild to push a Docker image into ECR, you have to set the PrivilegedMode setting to true. Otherwise, you'll receive the message Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

Resources:
  MyBuildProject:
    Type: AWS::CodeBuild::Project
    Properties:
      Name: "my-project"
      Environment:
        # We need the PrivilegedMode, otherwise the Docker daemon would not be accessible to pull and push the images
        PrivilegedMode: true

In addition to that, you need the correct permissions for the CodeBuild service role. If you don't have the permissions, the Docker daemon might hang while pulling or pushing the image. ecr:GetAuthorizationToken (required for docker login) cannot be scoped to a repository and needs Resource: '*'.

Resources:
  MyRepository:
    Type: AWS::ECR::Repository
    Properties:
      ImageTagMutability: IMMUTABLE 
      RepositoryName: "my-docker-image"

  MyBuildProject:
    Type: AWS::CodeBuild::Project
    Properties:
      ServiceRole: !GetAtt [MyCodeBuildRole, Arn]

  MyCodeBuildRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
      # ... codebuild.amazonaws.com
      Policies:
      - PolicyName: ecr-push
        PolicyDocument:
          Version: '2012-10-17'
          Statement:
          - Action:
            - ecr:GetAuthorizationToken
            Effect: Allow
            Resource: '*'
          # Allow CodeBuild to push to the ECR repository
          - Action:
            - ecr:GetDownloadUrlForLayer
            - ecr:BatchGetImage
            - ecr:CompleteLayerUpload
            - ecr:DescribeImages
            - ecr:DescribeRepositories
            - ecr:UploadLayerPart
            - ecr:ListImages
            - ecr:InitiateLayerUpload
            - ecr:BatchCheckLayerAvailability
            - ecr:PutImage
            Effect: Allow
            # the repository ARN itself, without a trailing /*
            Resource: !GetAtt [MyRepository, Arn]

"Insufficient permissions. The provided role does not have sufficient permissions to access ECS"

The error occurs during a CodePipeline build when executing an ECS deployment action. The role of the CodePipeline must allow the permission iam:PassRole in its PolicyDocument:

Resources:
  # region CodePipeline
  CodePipelineRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
      # ....
      Path: /
      Policies:
      - PolicyName: pass-role
        PolicyDocument:
          Version: '2012-10-17'
          Statement:
          - Action:
            - iam:PassRole
            Effect: Allow
            Resource: '*'

Lambda

The provided execution role does not have permissions to call DescribeNetworkInterfaces on EC2

The error The provided execution role does not have permissions to call DescribeNetworkInterfaces on EC2 occurs if your Lambda function is attached to a VPC and its execution role is missing the required EC2 network interface permissions. Alternatively, attach the AWS managed policy AWSLambdaVPCAccessExecutionRole.

Resources:
  MyLambda:
    Type: AWS::Lambda::Function
    Properties:
      Role: !GetAtt [ MyLambdaRole, Arn ]

  MyLambdaRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
        - Action:
          - sts:AssumeRole
          Effect: Allow
          Principal:
            Service:
            - lambda.amazonaws.com
      Policies:
      - PolicyName: lambda-vpc-access
        PolicyDocument:
          Statement:
          - Action:
            - ec2:CreateNetworkInterface
            - ec2:DeleteNetworkInterface
            - ec2:DescribeNetworkInterfaces
            Resource: '*'
            Effect: Allow
          Version: '2012-10-17'  

All files from this article, ready to copy and linted on every change: GitHub ↗