aws / aws/aws-sam-cli

Reuse image for functions with same build metadata

Open
#2,466 15 comments 21 reactions 0 assignees View on GitHub
area/build maintainer/need-followup stage/needs-attention type/question
Dominant language
Python
Stars
6.7k
Forks
1.2k
Avg merge
1d 10h
Merged PRs (30d)
52

Description

### Description:

It looks like if you have 2 functions using `PackageType: Image` using the same Dockerfile, `sam build` does not correctly add `ImageUri` to all functions.

I have traced through the code that all functions are added to the same `build_definition` but the build results are not copied to all functions. There is an explicit if condition to only do this to `ZIP`, is there a reason for this or it's just a feature not supported yet?

https://github.com/aws/aws-sam-cli/blob/12efe15b682926b006ce2a846d7cc6c71f5f2d74/samcli/lib/build/build_strategy.py#L117

### Steps to reproduce:

Example template:

```yaml
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31

Parameters:
Tag:
Type: String
Default: latest
Description: Docker tag to build and deploy.

Globals:
Function:
Timeout: 5

Resources:
FooFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
ImageConfig:
Command:
- foo
Metadata:
DockerTag: !Ref Tag
DockerContext: .

BarFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
ImageConfig:
Command:
- bar
Metadata:
DockerTag: !Ref Tag
DockerContext: .
```

### Observed result:

The "built" template only has `ImageUri` on the BarFunction:

```yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Parameters:
Tag:
Type: String
Default: latest
Description: Docker tag to build and deploy.
Globals:
Function:
Timeout: 5
Resources:
BarFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
ImageConfig:
Command:
- bar
ImageUri: barfunction:latest
Metadata:
DockerTag:
Ref: Tag
DockerContext: .
FooFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
ImageConfig:
Command:
- foo
Metadata:
DockerTag:
Ref: Tag
DockerContext: .

```

### Expected result:

Expect to re-use the same image for multiple functions.

I can however see an issue because the image name includes the first function name.

### Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

1. OS: mac
2. `sam --version`: 1.13.2

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.