'How to use an existing ECR image in my AWS account when using AWS SAM to define Lambda Function resource backed by container image
I'm following the tutorial linked below and would like to know if it is possible to reference a prebuilt ECR image for a Lambda function resource rather than the image being built by AWS SAM on deployment.
For example, is it possible to reference an existing ECR image in my AWS account built by another CI/CD pipeline?
What parts of the YAML template would I need to modify to achieve this if possible?
https://aws.amazon.com/blogs/compute/using-container-image-support-for-aws-lambda-with-aws-sam/
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: demo app
Globals:
Function:
Timeout: 3
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
Metadata:
DockerTag: nodejs12.x-v1
DockerContext: ./hello-world
Dockerfile: Dockerfile
HolaWorldFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
Events:
HolaWorld:
Type: Api
Properties:
Path: /hola
Method: get
Metadata:
DockerTag: nodejs12.x-v1
DockerContext: ./hola-world
Dockerfile: Dockerfile
Outputs:
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
HolaWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hola World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hola/"
Solution 1:[1]
There is an ImageUri field in the function Properties that lets you specify the ECR image. Additionally you can overwrite some image parameters like Command. Note that I used environment variables in the buildstep and pulled those in as parameters for the account ID and image tag in this example
Properties:
ImageUri: !Sub ${AccountIDParameter}.dkr.ecr.us-west-2.amazonaws.com/lambda:${EnvironmentParameter}
ImageConfig:
Command: [ "app.CommandOverwrite" ]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Aaron Melgar |
