Container image support for AWS Lambda via cloudformation

Viewed 3071

AWS Lambda now supports container images as a packaging format. Ref

Does cloudformation support using Container image for deploying AWS Lambda?

Something similar to packaging and deployment support via S3 Ref

4 Answers

Does cloudformation support using Container image for deploying AWS Lambda?

Yes it does. AWS::Lambda::Function has new properties specific to container images, such as:

  • ImageConfig
  • PackageType
  • ImageUri

@Marcin points to the correct documentation. @Jenson 's answer relies on AWS SAM.

Here is the pure CloudFormation template.

AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  EcrImageUri:
    Description: ECR image URI
    Type: String
Resources:
  LambdaShogi:
    Type: AWS::Lambda::Function
    Properties:
      PackageType: Image
      Role:
        Fn::GetAtt:
        - LambdaShogiRole
        - Arn
      Code:
        ImageUri: !Ref 'EcrImageUri'
      Architectures:
        - x86_64
      MemorySize: 1024
      Timeout: 300
  LambdaShogiRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - lambda.amazonaws.com
          Action:
          - sts:AssumeRole
      Path: /
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

By passing the image URI, you can deploy the container Lambda function.

$ aws cloudformation deploy \
  --stack-name lambda-container-test \
  --template-file ./cfn.yml \
  --parameter-overrides EcrImageUri=<YOUR-ACCOUNT-ID>.dkr.ecr.<REGION>.amazonaws.com/test:latest \
  --capabilities CAPABILITY_IAM

Sample template:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  TestNode12Lambda:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
      FunctionName: testnode12lambda
      Role:
        Fn::GetAtt:
        - TestNode12LambdaRole
        - Arn
      ImageUri: {aws_account_id}.dkr.ecr.us-east-1.amazonaws.com/lambda-container:latest
      Timeout: 300
  TestNode12LambdaRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: TestNode12LambdaRole
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - lambda.amazonaws.com
          Action:
          - sts:AssumeRole
      Path: /
      Policies:
      - PolicyName: AWSLambdaBasicExecutionRole
        PolicyDocument:
          Version: '2012-10-17'
          Statement:
          - Effect: Allow
            Action:
            - logs:CreateLogGroup
            - logs:CreateLogStream
            - logs:PutLogEvents
            Resource: '*'

Command to deploy: aws cloudformation deploy --template-file resources.yml --stack-name lambdacft --capabilities CAPABILITY_NAMED_IAM

Sample code to deploy node12 Lambda as container image: https://github.com/jensonjoseph/lambda-cft

Try to add these under your lambda function properties

Properties: PackageType: Image ImageUri: # get it from ECR

Remove code , CodeUri , runtime , layers and handler properties from the yaml file for that lambda function

Related