aws / aws/aws-sam-cli

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

Open
#2,672 5 comments 27 reactions 0 assignees View on GitHub
area/deploy maintainer/need-followup type/feature
Dominant language
Python
Stars
6.7k
Forks
1.2k
Avg merge
1d 10h
Merged PRs (30d)
52

Description

### 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 https://github.com/aws/aws-sam-cli/issues/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?"

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.