Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(package/deploy): new option --image-repositories #2465

Merged
merged 7 commits into from Dec 16, 2020

Conversation

sriram-mv
Copy link
Contributor

@sriram-mv sriram-mv commented Dec 8, 2020

Which issue(s) does this change fix?

#2447
It does not fix this issue, but allows for mapping of function to different ECR if required.

Why is this change necessary?

Allow for function to ecr mapping. each function can now be uploaded to a different ECR.

How does it address the issue?

Guided deploy prompts for an ECR per function and saves it.

What side effects does this change have?

if both --image-repository and --image-repositories are specifed. One has to be removed. Guided flow looks to fill --image-repositories but is not destructive to the configuration file and does not remove --image-repository if its already set.

Checklist

  • Write design document (Do I need to write a design document?)
  • Write unit tests
  • Write/update functional tests
  • Write/update integration tests
  • make pr passes
  • make update-reproducible-reqs if dependencies were changed
  • Write documentation

NOTE: WIP and needs more tests.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@jfuss jfuss marked this pull request as draft December 9, 2020 17:26
samcli/commands/_utils/options.py Outdated Show resolved Hide resolved
samcli/cli/types.py Outdated Show resolved Hide resolved
samcli/lib/cli_validation/image_repository_validation.py Outdated Show resolved Hide resolved
samcli/commands/deploy/utils.py Show resolved Hide resolved
samcli/commands/deploy/guided_config.py Show resolved Hide resolved
@sriram-mv sriram-mv changed the title [WIP] feat(package/deploy): new option --image-repositories feat(package/deploy): new option --image-repositories Dec 10, 2020
@sriram-mv sriram-mv marked this pull request as ready for review December 10, 2020 17:57
samcli/commands/deploy/guided_config.py Show resolved Hide resolved
samcli/commands/_utils/options.py Outdated Show resolved Hide resolved
Comment on lines +281 to +282
for resource_id, resource in template_dict.get("Resources", {}).items():
if resource.get("Properties", {}).get("PackageType", ZIP) == artifact and resource.get("Type") in [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was an existing function collector, which is been used in build command, can we use that one instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does that process the template? I will take a look at it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its very specific to build, we would need some refactoring to make it more generic so that we can share that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still use SamFunctionProvider https://github.com/aws/aws-sam-cli/blob/develop/samcli/lib/providers/sam_function_provider.py#L16 which will handle parameter overrides etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some circular dependencies that we need to take care of here, I vote we do this in a separate PR.

@sriram-mv
Copy link
Contributor Author

sriram-mv commented Dec 11, 2020

@mndeveci : if we dont special case how we save to the config file. It saves as below, thats why we need it.

[default.deploy.parameters.image_repositories]
HelloWorldFunction = "123456789012.dkr.ecr.us-east-1.amazonaws.com/sam-cli"

Copy link
Contributor

@mndeveci mndeveci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on the feedbacks, left only one comment for collecting the functions from the template. Other than that it looks good.

@@ -129,7 +131,7 @@ def run(self):
print_deploy_args(
self.stack_name,
self.s3_bucket,
self.image_repository,
self.image_repositories if isinstance(self.image_repositories, dict) else self.image_repository,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] I think if you check that self.image_repositories is not None will be more readable

@metaskills
Copy link
Contributor

There is still no documentation on sam package (https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-package.html) guides page. Trying to find the format to use here because I think this arg will allow me to deploy two functions with the same image.

bramkoot pushed a commit to bramkoot/aws-sam-cli that referenced this pull request Jan 15, 2021
* feat(package/deploy): new option `--image-repositories`

* tests(package/deploy): unit and integration tests

* fix(lint): make lint happy

* fix(comments): address PR comments

* fix(image-repositories): change callback to default to {}

Co-authored-by: Mehmet Nuri Deveci <5735811+mndeveci@users.noreply.github.com>
mndeveci added a commit to mndeveci/aws-sam-cli that referenced this pull request Jan 19, 2021
* feat(package/deploy): new option `--image-repositories`

* tests(package/deploy): unit and integration tests

* fix(lint): make lint happy

* fix(comments): address PR comments

* fix(image-repositories): change callback to default to {}

Co-authored-by: Mehmet Nuri Deveci <5735811+mndeveci@users.noreply.github.com>
@JamesDougherty
Copy link

Hopefully this will help someone and save them time. There is no documentation on this as @metaskills mentioned and no examples on how to use it. So after spending more time than needed on it and looking through the source code on this, I was able to figure out what they intended.

Let's say you have two functions in your template file that use the Image package type, like the following:

FooFunction:   # <------------------This is a functions logical ID, take note of this.
  Type: AWS::Serverless::Function
  Properties:
    PackageType: Image
    ...
  Metadata:
    DockerTag: latest
    DockerContext: ./opt/dist/functions/foo-function/        # Wherever your Dockerfile is
    Dockerfile: Dockerfile

BarFunction:   # <------------------This is a functions logical ID, take note of this.
  Type: AWS::Serverless::Function
  Properties:
    PackageType: Image
    ...
  Metadata:
    DockerTag: latest
    DockerContext: ./opt/dist/functions/bar-function/        # Wherever your Dockerfile is
    Dockerfile: Dockerfile

After you run the sam build command it will add the following ImageUri to the template it generates in the .aws-sam/build directory. The ImageUri consists of the image repository name (functions logical ID in all lower case) and DockerTag concatenated. Here is an example of what it will add for the functions defined above:

FooFunction:
  Type: AWS::Serverless::Function
  Properties:
    PackageType: Image
    ImageUri: foofunction:latest  # <------------- Added by SAM Build
    ...
  Metadata:
    DockerTag: latest
    DockerContext: ./opt/dist/functions/foo-function/
    Dockerfile: Dockerfile

BarFunction:
  Type: AWS::Serverless::Function
  Properties:
    PackageType: Image
    ImageUri: barfunction:latest  # <------------- Added by SAM Build
    ...
  Metadata:
    DockerTag: latest
    DockerContext: ./opt/dist/functions/bar-function/
    Dockerfile: Dockerfile

Now when you run the sam deploy command you need to add the --image-repositories in the following format:

--image-repositories <Function Logical ID>=<Function Image Repository>, <Function Logical ID>=<Function Image Repository>, ...

Where:
Function Logical ID = The functions logical ID as defined in the SAM template
Function Image Repository = The functions ECR image repository that SAM created in ECR URL format

So using the above example we would call sam deploy using the following:

sam deploy ... --image-repositories FooFunction=<account id>.dkr.ecr.<region>.amazonaws.com/foofunction, BarFunction=<account id>.dkr.ecr.<region>.amazonaws.com/barfunction

Where:
Account ID = Your AWS account ID
Region = Your AWS region (like us-east-1)

I have this automated using a PowerShell script so you will just need to figure out how to handle it using your tooling / use case.

Enjoy.

@ygorth
Copy link
Contributor

ygorth commented Aug 20, 2021

The example from @JamesDougherty is wrong and will throw an error like this:

Error: Got unexpected extra argument (Function_Logical_ID=account_id.dkr.ecr.us-east-1.amazonaws.com/Function_Image_Repository)

Please, find the correct command below:
sam deploy ... --image-repositories <Function Logical ID>=<account id>.dkr.ecr.<region>.amazonaws.com/foofunction --image-repositories <Function Logical ID>=<account id>.dkr.ecr.<region>.amazonaws.com/barfunction

Option description: --image-repositories Specify mapping of Function Logical ID to ECR Repo uri, of the form Function_Logical_ID=ECR_Repo_Uri. This option can be specified multiple times.

My current version is 1.29.0.

@JamesDougherty
Copy link

Hello @ygorth - Thank you for the correction. I was only testing with one repository at the moment and made the assumption that they designed it how the parameter overrides work "sam deploy ... --parameter-overrides foo=bar, hello=world". Of course it would be different. This is why it would be nice to have some official documentation so we don't have to assume anything. Anyway, thanks again.

@jmamin
Copy link

jmamin commented Feb 13, 2023

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
MacBook-Pro:lambda-local jamin$

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants