Skip to content

feat: add support for provenance option in docker build #49

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/guides/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ Additionally, you can define arguments that will be passed to the `docker build`
- `buildArgs`: With the `buildArgs` property, you can define arguments that will be passed to `docker build` command with `--build-arg` flag. They might be later referenced via `ARG` within your `Dockerfile`. (See [Documentation](https://docs.docker.com/engine/reference/builder/#arg))
- `cacheFrom`: The `cacheFrom` property can be used to specify which images to use as a source for layer caching in the `docker build` command with `--cache-from` flag. (See [Documentation](https://docs.docker.com/engine/reference/builder/#usage))
- `platform`: The `platform` property can be used to specify the architecture target in the `docker build` command with the `--platform` flag. If not specified, Docker will build for your computer's architecture by default. AWS Lambda typically uses `x86` architecture unless otherwise specified in the Lambda's runtime settings. In order to avoid runtime errors when building on an ARM-based machine (e.g. Apple M1 Mac), `linux/amd64` must be used here. The options for this flag are `linux/amd64` (`x86`-based Lambdas), `linux/arm64` (`arm`-based Lambdas), or `windows/amd64`. (See [Documentation](https://docs.docker.com/engine/reference/builder/#from))
- `provenance` Use the `provenance` property to disable multi-architecture manifest generated from BuildKit or `docker buildx`, allows the architecture specified in `platform` to be recognized by AWS Lambda during deployment.

When `uri` is defined for an image, `buildArgs`, `cacheFrom`, and `platform` cannot be defined.

Expand All @@ -320,6 +321,7 @@ provider:
cacheFrom:
- my-image:latest
platform: linux/amd64
provenance: false
anotherimage:
uri: 000000000000.dkr.ecr.sa-east-1.amazonaws.com/test-lambda-docker@sha256:6bb600b4d6e1d7cf521097177dd0c4e9ea373edb91984a505333be8ac9455d38
```
Expand Down
15 changes: 14 additions & 1 deletion lib/plugins/aws/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,7 @@ class AwsProvider {
buildArgs: { type: 'object', additionalProperties: { type: 'string' } },
cacheFrom: { type: 'array', items: { type: 'string' } },
platform: { type: 'string' },
provenance: { type: 'string' },
},
additionalProperties: false,
},
Expand Down Expand Up @@ -2214,6 +2215,7 @@ Object.defineProperties(
buildArgs,
cacheFrom,
platform,
provenance,
scanOnPush,
}) {
const imageProgress = progress.get(`containerImage:${imageName}`);
Expand Down Expand Up @@ -2260,8 +2262,10 @@ Object.defineProperties(
imagePath,
];

// This is an optional argument, so we only append to the arguments if "platform" is specified.
// These are optional arguments, so we only append to the arguments
// if "platform" or "provenance" is specified.
if (platform !== '') buildDockerArgs.push(`--platform=${platform}`);
if (provenance !== '') buildDockerArgs.push(`--provenance=${provenance}`);

let imageSha;
try {
Expand Down Expand Up @@ -2396,6 +2400,7 @@ Object.defineProperties(
const defaultCacheFrom = [];
const defaultScanOnPush = false;
const defaultPlatform = '';
const defaultProvenance = '';

if (imageUri) {
return await this.resolveImageUriAndShaFromUri(imageUri);
Expand Down Expand Up @@ -2450,6 +2455,12 @@ Object.defineProperties(
'ECR_IMAGE_BOTH_URI_AND_PLATFORM_DEFINED_ERROR'
);
}
if (imageDefinedInProvider.uri && imageDefinedInProvider.provenance) {
throw new ServerlessError(
`The "provenance" property cannot be used with "uri" property "${imageName}"`,
'ECR_IMAGE_BOTH_URI_AND_PROVENANCE_DEFINED_ERROR'
);
}
if (imageDefinedInProvider.path) {
return await this.resolveImageUriAndShaFromPath({
imageName,
Expand All @@ -2458,6 +2469,7 @@ Object.defineProperties(
buildArgs: imageDefinedInProvider.buildArgs || defaultBuildArgs,
cacheFrom: imageDefinedInProvider.cacheFrom || defaultCacheFrom,
platform: imageDefinedInProvider.platform || defaultPlatform,
provenance: imageDefinedInProvider.provenance || defaultProvenance,
scanOnPush: imageScanDefinedInProvider,
});
}
Expand All @@ -2473,6 +2485,7 @@ Object.defineProperties(
buildArgs: imageDefinedInProvider.buildArgs || defaultBuildArgs,
cacheFrom: imageDefinedInProvider.cacheFrom || defaultCacheFrom,
platform: imageDefinedInProvider.platform || defaultPlatform,
provenance: imageDefinedInProvider.provenance || defaultProvenance,
scanOnPush: imageScanDefinedInProvider,
});
},
Expand Down