aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

ECR URL regex matcher does not support LocalStack (localhost:4513)

Open ghost opened this issue 5 years ago • 7 comments

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

ghost avatar Sep 20 '21 13:09 ghost

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

ghost avatar Sep 23 '21 16:09 ghost

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.

mndeveci avatar Oct 19 '21 21:10 mndeveci

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

gkpty avatar Nov 17 '22 07:11 gkpty

@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

jmamin avatar Feb 13 '23 17:02 jmamin

hi @mndeveci I would be able to look into this and work on a fix if no one is working

sleepwithcoffee avatar Mar 29 '23 05:03 sleepwithcoffee

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

qingchm avatar Apr 07 '23 19:04 qingchm

⚠️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.

github-actions[bot] avatar Apr 07 '23 19:04 github-actions[bot]