question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
solarmosaic-kflorencecommented, Jan 15, 2022

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”.

2reactions
mr-davidccommented, Jan 26, 2022

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sam build - AWS Serverless Application Model
The sam build command processes your AWS SAM template file, ... You specify your AWS Lambda function's package type with the PackageType property....
Read more >
How to Deploy a Lambda Function as a Container Image
This post will demonstrate, as the title suggests, how to deploy a Lambda function as a container image using Docker, SAM (Serverless Application...
Read more >
Managing Multiple Functions with AWS SAM and Webpack
These template versions don't change often and AWS is known for supporting backward compatibility. Globals: Function: CodeUri: build/ Runtime ...
Read more >
Let's Try Lambda Container Support with SAM CLI - hayao-k.dev
You can create a project from a template with sam init command. For package type questions, select 2 - image . I have...
Read more >
Packaging AWS Lambda functions as container images
1. Enter sam init to start the AWS SAM wizard. · 2. Choose '1 – AWS Quick Start Templates'. · 3. You have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found