How to specify image name in sam build with template containing multiple functions
See original GitHub issueI’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
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (6 by maintainers)
It would be nice to be able to specify the name of the image, instead of it having to be the lowercase version of the function resource name. For example, if my resource name is “MyFunction”, it would result in an image name of “myfunction” – I’d prefer if it were “my-function”.
@qingchm Yes, I think if that page is at least updated to mention all the discovery we did above for this issue ☝️ ,that would be a good start. As @solarmosaic-kflorence said, I think long term, a nice feature would be having the option to specify the image name somehow.