Skip to content

Create ECR Repository with SAM to be used in Lambda Function with PackageType: Image #2672

@vleandersson

Description

@vleandersson

Describe your idea/feature/enhancement

When deploying Lambda Functions with SAM, using PackageType: Image I would like to define my ECR Repository in the same SAM template file

Proposal

It seems right now I am supposed to create my ECR repository separately, and then use the --image-repository flag on sam deploy (e.g. sam deploy --image-repository "123456789.dkr.ecr.us-east-1.amazonaws.com/my-repo"). This works fine, but it creates some deployment dependencies I would be keen to avoid.

To avoid this, I would propose to allow creating + deploying the ECR Repo with SAM, and then use the GetAtt function to attach the created repository URI to the function I intend to use this repository, e.g. ImageUri: !GetAtt MyECRRepository.RepositoryUri.

Note; I am actually not sure today how the ImageUri field is used on the lambda definition, as we are required to supply SAM with the --image-repository flag. I would personally be happy renaming this to EctRepositoryUri or similar to clarify the field usage.

So in my SAM template yaml file:

Resources:
  ###################################
  #          ECR Repositories
  ###################################
  MyECRRepository:
    Type: AWS::ECR::Repository
    Properties:
      RepositoryName: 'my-repository'

  ###################################
  #          LAMBDAS
  ###################################
  MyLambdaFunction:
    Type: AWS::Serverless::Function
    DependsOn:
      - MyECRRepository
    Properties:
      PackageType: Image

      # URI of the Amazon Elastic Container Registry (Amazon ECR) repository for the Lambda function's container image.
      ImageUri: !GetAtt MyECRRepository.RepositoryUri
    Metadata:
      Dockerfile: Dockerfile
      DockerContext: ./workspaces/myDockerContext
      DockerTag: my-own-tag

This would allow us to create repositories and images easier in SAM, while keeping deployments to different environments intact in one place.

Additional Details

Relates to #2447
@metaskills would be good to get your thoughts on this as well
@sriram-mv I believe you might have been adding some thoughts to this earlier
@stephenbawks added the following comment in the linked issue above
"The thing I was going to add here since I am little confused on the workflow. I was hoping to just create an ECR repository along with my Lambda but I do not see a way I can create that before it tries to upload the container when I do a deploy. So I guess I am just wondering with the way this works, do I need to have ECR created beforehand?"

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions