ECR URL regex matcher does not support LocalStack (localhost:4513)
Describe your idea/feature/enhancement
I am using AWS SAM with LocalStack. When we try and package our Dockerfile-backed Lambda functions, SAM tries to publish to an ECR repository. We create the ECR repository ahead of time, then we provide its URI to SAM (samlocal):
awslocal ecr create-repository --repository-name my-repo
awslocal ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin localhost:4513
samlocal package \
--template-file "/tmp/.aws-sam/template.yaml" \
--s3-bucket "sam-bucket" \
--image-repository "localhost/my-repo" \
--output-template-file "/tmp/.aws-sam/template_packaged.yaml" --debug
Proposal
Add a compatible regex match pattern to the following code which will support localhost:4513 (or honestly any domain name with port): https://github.com/aws/aws-sam-cli/blob/2592135cd21cb5fb1559e866cce2d5383ee49536/samcli/lib/package/regexpr.py#L5
Error output
setup_resources | Usage: samlocal package [OPTIONS]
setup_resources |
setup_resources | Error: Invalid value for '--image-repository': Invalid Image Repository ECR URI: localhost:4513/aws-sam-image-repo
This could potentially work:
import re
ECR_URL = (
r"(^[a-zA-Z0-9][a-zA-Z0-9-_]*)\.dkr\.ecr\.([a-zA-Z0-9][a-zA-Z0-9-_]*)\.amazonaws\.com(\.cn)?|"
r"(?:([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+:[0-9]{1,5})"
r"\/(?:[a-z0-9]+(?:[._-][a-z0-9]+)*\/)*[a-z0-9]+(?:[._-][a-z0-9]+)*"
)
def is_ecr_url(url):
return bool(re.match(ECR_URL, url)) if url else False
print(is_ecr_url('000000000000.dkr.ecr.eu-west-1.amazonaws.com/my-repo'))
print(is_ecr_url('localhost:4566/my-repo'))
print(is_ecr_url('000000000000.invalid.amazonaws.com/my-repo'))
print(is_ecr_url('localhost:4566'))
print(is_ecr_url(':4566'))
print(is_ecr_url('asdf'))
print(is_ecr_url(':'))
print(is_ecr_url('/my-repo'))
$ python3 ecr_rgx.py
True
True
False
False
False
False
False
False
Thanks for suggestion @danw-mpl, the only change I can suggest is to limit the regex to allow localhost or 127.0.0.1.
Please feel free to raise a PR for the change you requested, we love to get more contributions.
Ive just run into this issue as well. In my case im using docker-compose so the URI of my ECR repository starts with the service name, in my case aws-local. It would be amazing to have a CLI option that can turn off regex validation for the ECR URI
@danw-mpl @mndeveci @gkpty Hello,
Any advice how to fix this issue? image name is compatible with format and regex but still errors out!:
MacBook-Pro:lambda-local jamin$ sam deploy --guided Usage: sam deploy [OPTIONS] Try 'sam deploy -h' for help.
Error: Invalid value for '--image-repositories': --image-repositories is not a valid format, it needs to be of the form function_logical_id=ECR_URI
samconfig.toml version = 0.1 [default] [default.deploy] [default.deploy.parameters] stack_name = "everflowclicks" s3_bucket = "aws-sam-cli-managed-default-samclisourcebucket-1785ltpwmjlwc" s3_prefix = "everflowclicks" region = "us-west-2" confirm_changeset = true capabilities = "CAPABILITY_IAM" image_repositories = ["880954911558.dkr.ecr.us-west-2.amazonaws.com/everflow-app"] disable_rollback = true
hi @mndeveci I would be able to look into this and work on a fix if no one is working
This has been addressed in our most recent release 1.79.0 (https://github.com/aws/aws-sam-cli/releases/tag/v1.79.0). Closing the issue here. Thanks for all the engagement, especially the contribution from @sleepwithcoffee
⚠️COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.