aws / aws/aws-sam-cli

How to specify image name in sam build with template containing multiple functions

Open
#3,233 14 comments 2 reactions 0 assignees View on GitHub
area/docker maintainer/need-followup stage/pm-review type/feature type/ux
Dominant language
Python
Stars
6.7k
Forks
1.2k
Avg merge
1d 10h
Merged PRs (30d)
52

Description

I've had a look through the extensive documentation for AWS SAM and which CLI parameters are supported for the `sam build` command but I couldn't see any option to specify the name of the docker image that gets built? I am already using the `Metadata` section with the `DockerTag` option to specify the tag but I'd like to specify the image name.

I have two Functions defined which are built as a single Docker image which builds and runs perfectly fine, however, when I run a `sam build` it seems to just select the Logical Resource ID of the first `AWS::Serverless::Function` defined in the template.

Here is my template:

```
Transform: AWS::Serverless-2016-10-31

Parameters:
DockerTag:
Type: String
Default: local-dev

Resources:
TestFunction1:
Type: AWS::Serverless::Function
Properties:
FunctionName: TestFunction1
PackageType: Image
ImageUri: !Sub 'testfunction1:${DockerTag}'
ImageConfig:
Command:
- TestLambdas::TestLambdas.TestFunction1::Invoke
Role: !Sub 'arn:aws:iam::${AWS::AccountId}:role/lambda-execution-role'
MemorySize: 256
Timeout: 30
Events:
ScheduledEvent:
Type: Schedule
Properties:
Schedule: rate(3 minutes)
Name: test-schedule-rule
Enabled: true
Metadata:
DockerTag: !Ref DockerTag
DockerContext: .
Dockerfile: ./Dockerfile

TestFunction2:
Type: AWS::Serverless::Function
Properties:
FunctionName: TestFunction2
PackageType: Image
ImageUri: !Sub 'testfunction1:${DockerTag}'
ImageConfig:
Command:
- TestLambdas::TestLambdas.TestFunction2::Invoke
Role: !Sub 'arn:aws:iam::${AWS::AccountId}:role/lambda-execution-role'
MemorySize: 256
Timeout: 30
Events:
ScheduledEvent:
Type: Schedule
Properties:
Schedule: rate(3 minutes)
Name: test-schedule-rule
Enabled: true
Metadata:
DockerTag: !Ref DockerTag
DockerContext: .
Dockerfile: ./Dockerfile
```

And here is the output of running `sam build` (Notice this one image being built contains two functions)

```
PS C:\Workspace\Lambda.Handlers\Lambda.Handlers.Test> sam build
Building codeuri: C:\Workspace\Lambda.Handlers\Lambda.Handlers.Test runtime: None metadata: {'DockerTag': 'local-dev', 'DockerContext': 'C:\\Workspace\\Lambda.Handlers\\Lambda.Handlers.Test', 'Dockerfile': './Dockerfile'} functions: ['TestFunction1', 'TestFunction2']
Building image for TestFunction1 function
Setting DockerBuildArgs: {} for TestFunction1 function
Step 1/3 : FROM public.ecr.aws/lambda/dotnet:5.0
---> 41fed005ba97
Step 2/3 : COPY ./* /var/task/
---> Using cache
---> 6fac8005ce03
Step 3/3 : ENTRYPOINT ["/lambda-entrypoint.sh"]
---> Using cache
---> d691b293f1c1
Successfully built d691b293f1c1
Successfully tagged testfunction1:local-dev <--------- I WOULD LIKE TO SET testfunction1 TO SOMETHING ELSE

Build Succeeded

Built Artifacts : .aws-sam\build
Built Template : .aws-sam\build\template.yaml

Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Deploy: sam deploy --guided
```

If I re-order `TestFunction1` and `TestFunction2`, then the built image is then named `testfunction2`.

Looking forward to any help/advice if what I'm trying to achieve is even possible...

Thanks

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.