forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lambda): add docker platform support for lambda (aws#21405)
Based on [this](aws#16770) PR Add the missing part to add platform support when using lambda `fromImageAsset` As we are not allowed to specify `platform` flag for `DockerImageAsset`, users deploying cdk on x86_64 platform will not be able to bundle lambda.DockerImageFunction for the new arm64 architecture. Similarly, users deploying cdk on arm64 architecture like Mac M1 will not be able to bundle images for AWS Fargate, which is x86_64 only. # builder experience with aws-lambda For x86_64 users deploying Lambda functions with container runtime on Lambda Graviton2(arm64) from local container image assets with multi-architecture docker base images. Specify the platform to ensure the image would be built for ARM64 architecture. ``` new DockerImageFunction(stack, 'Lambda', { code: DockerImageCode.fromImageAsset(path.join(__dirname, 'docker-arm64-handler')), architecture: Architecture.ARM_64, }); ``` Fixes: aws#12472, aws#20907 ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
14 changed files
with
542 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/@aws-cdk/aws-lambda/test/docker-arm64-handler/Dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FROM public.ecr.aws/lambda/python:latest | ||
|
||
ARG FUNCTION_DIR="/var/task" | ||
COPY index.py ${FUNCTION_DIR} | ||
|
||
CMD [ "index.handler" ] |
6 changes: 6 additions & 0 deletions
6
packages/@aws-cdk/aws-lambda/test/docker-arm64-handler/index.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import json, platform | ||
def handler(event, context): | ||
return { | ||
'statusCode': 200, | ||
'body': json.dumps( f'Hello CDK from Lambda({platform.platform()})!') | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/@aws-cdk/aws-lambda/test/integ.lambda.docker-arm64.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import * as path from 'path'; | ||
import { App, Stack } from '@aws-cdk/core'; | ||
import * as integ from '@aws-cdk/integ-tests'; | ||
import { Architecture, DockerImageCode, DockerImageFunction } from '../lib'; | ||
|
||
const app = new App(); | ||
|
||
const stack = new Stack(app, 'lambda-ecr-docker-arm64'); | ||
|
||
new DockerImageFunction(stack, 'MyLambda', { | ||
code: DockerImageCode.fromImageAsset(path.join(__dirname, 'docker-arm64-handler')), | ||
architecture: Architecture.ARM_64, | ||
}); | ||
|
||
new integ.IntegTest(app, 'lambda-docker-arm64', { | ||
testCases: [stack], | ||
}); | ||
|
||
app.synth(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as path from 'path'; | ||
import { Template } from '@aws-cdk/assertions'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import { Architecture, DockerImageCode, DockerImageFunction } from '../lib'; | ||
|
||
describe('lambda platform', () => { | ||
test('can choose lambda architecture arm64', () => { | ||
// GIVEN | ||
const app = new cdk.App(); | ||
const stack = new cdk.Stack(app, 'stack'); | ||
|
||
// WHEN | ||
new DockerImageFunction(stack, 'Lambda', { | ||
code: DockerImageCode.fromImageAsset(path.join(__dirname, 'docker-arm64-handler')), | ||
architecture: Architecture.ARM_64, | ||
}); | ||
|
||
// THEN | ||
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', { | ||
Architectures: [ | ||
'arm64', | ||
], | ||
}); | ||
}); | ||
|
||
test('can choose lambda architecture x86_64', () => { | ||
// GIVEN | ||
const app = new cdk.App(); | ||
const stack = new cdk.Stack(app, 'stack'); | ||
|
||
// WHEN | ||
new DockerImageFunction(stack, 'Lambda', { | ||
code: DockerImageCode.fromImageAsset(path.join(__dirname, 'docker-arm64-handler')), | ||
architecture: Architecture.X86_64, | ||
}); | ||
|
||
// THEN | ||
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', { | ||
Architectures: [ | ||
'x86_64', | ||
], | ||
}); | ||
}); | ||
}); |
1 change: 1 addition & 0 deletions
1
packages/@aws-cdk/aws-lambda/test/lambda.docker-arm64.integ.snapshot/cdk.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"version":"20.0.0"} |
11 changes: 11 additions & 0 deletions
11
packages/@aws-cdk/aws-lambda/test/lambda.docker-arm64.integ.snapshot/integ.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"version": "20.0.0", | ||
"testCases": { | ||
"lambda-docker-arm64/DefaultTest": { | ||
"stacks": [ | ||
"lambda-ecr-docker-arm64" | ||
], | ||
"assertionStack": "lambdadockerarm64DefaultTestDeployAssert07D408EF" | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...ws-lambda/test/lambda.docker-arm64.integ.snapshot/integ.lambda.docker-arm64.expected.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
{ | ||
"Resources": { | ||
"MyLambdaServiceRole4539ECB6": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "lambda.amazonaws.com" | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"ManagedPolicyArns": [ | ||
{ | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"arn:", | ||
{ | ||
"Ref": "AWS::Partition" | ||
}, | ||
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" | ||
] | ||
] | ||
} | ||
] | ||
} | ||
}, | ||
"MyLambdaCCE802FB": { | ||
"Type": "AWS::Lambda::Function", | ||
"Properties": { | ||
"Code": { | ||
"ImageUri": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
{ | ||
"Ref": "AWS::AccountId" | ||
}, | ||
".dkr.ecr.", | ||
{ | ||
"Ref": "AWS::Region" | ||
}, | ||
".", | ||
{ | ||
"Ref": "AWS::URLSuffix" | ||
}, | ||
"/aws-cdk/assets:166017f6f6f219800dee266c149e208fe18dea1788d822b1783afbc008c25db7" | ||
] | ||
] | ||
} | ||
}, | ||
"Role": { | ||
"Fn::GetAtt": [ | ||
"MyLambdaServiceRole4539ECB6", | ||
"Arn" | ||
] | ||
}, | ||
"Architectures": [ | ||
"arm64" | ||
], | ||
"PackageType": "Image" | ||
}, | ||
"DependsOn": [ | ||
"MyLambdaServiceRole4539ECB6" | ||
] | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...dk/aws-lambda/test/lambda.docker-arm64.integ.snapshot/lambda-ecr-docker-arm64.assets.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"version": "20.0.0", | ||
"files": { | ||
"e57a612d901fd500f4eae1beabe146b3d7a4f81dd10a81199b9c884d1e227a1e": { | ||
"source": { | ||
"path": "lambda-ecr-docker-arm64.template.json", | ||
"packaging": "file" | ||
}, | ||
"destinations": { | ||
"current_account-current_region": { | ||
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", | ||
"objectKey": "e57a612d901fd500f4eae1beabe146b3d7a4f81dd10a81199b9c884d1e227a1e.json", | ||
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" | ||
} | ||
} | ||
} | ||
}, | ||
"dockerImages": { | ||
"027b9b499ce9e488d4c3cfa41abdbdc6afe203989a5bd77258f471da03f3f040": { | ||
"source": { | ||
"directory": "asset.027b9b499ce9e488d4c3cfa41abdbdc6afe203989a5bd77258f471da03f3f040", | ||
"platform": "linux/arm64" | ||
}, | ||
"destinations": { | ||
"current_account-current_region": { | ||
"repositoryName": "cdk-hnb659fds-container-assets-${AWS::AccountId}-${AWS::Region}", | ||
"imageTag": "027b9b499ce9e488d4c3cfa41abdbdc6afe203989a5bd77258f471da03f3f040", | ||
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-image-publishing-role-${AWS::AccountId}-${AWS::Region}" | ||
} | ||
} | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
.../aws-lambda/test/lambda.docker-arm64.integ.snapshot/lambda-ecr-docker-arm64.template.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
{ | ||
"Resources": { | ||
"MyLambdaServiceRole4539ECB6": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "lambda.amazonaws.com" | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"ManagedPolicyArns": [ | ||
{ | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"arn:", | ||
{ | ||
"Ref": "AWS::Partition" | ||
}, | ||
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" | ||
] | ||
] | ||
} | ||
] | ||
} | ||
}, | ||
"MyLambdaCCE802FB": { | ||
"Type": "AWS::Lambda::Function", | ||
"Properties": { | ||
"Code": { | ||
"ImageUri": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
{ | ||
"Ref": "AWS::AccountId" | ||
}, | ||
".dkr.ecr.", | ||
{ | ||
"Ref": "AWS::Region" | ||
}, | ||
".", | ||
{ | ||
"Ref": "AWS::URLSuffix" | ||
}, | ||
"/aws-cdk/assets:027b9b499ce9e488d4c3cfa41abdbdc6afe203989a5bd77258f471da03f3f040" | ||
] | ||
] | ||
} | ||
}, | ||
"Role": { | ||
"Fn::GetAtt": [ | ||
"MyLambdaServiceRole4539ECB6", | ||
"Arn" | ||
] | ||
}, | ||
"Architectures": [ | ||
"arm64" | ||
], | ||
"PackageType": "Image" | ||
}, | ||
"DependsOn": [ | ||
"MyLambdaServiceRole4539ECB6" | ||
] | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ocker-arm64.integ.snapshot/lambdadockerarm64DefaultTestDeployAssert07D408EF.template.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
Oops, something went wrong.