Skip to content

Commit

Permalink
test: Add bundling, integ test for custom Docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
setu4993 committed Dec 28, 2021
1 parent 412791a commit d813fe0
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/@aws-cdk/aws-lambda-python/test/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,26 @@ test('Bundling a function with poetry dependencies', () => {
}),
}));
});

test('Bundling a function with custom bundling image', () => {
const entry = path.join(__dirname, 'lambda-handler-custom-build');

Bundling.bundle({
entry: path.join(entry, '.'),
runtime: Runtime.PYTHON_3_9,
architecture: Architecture.X86_64,
outputPathSuffix: 'python',
image: DockerImage.fromBuild(path.join(entry)),
});

expect(Code.fromAsset).toHaveBeenCalledWith(entry, expect.objectContaining({
bundling: expect.objectContaining({
command: [
'bash', '-c',
'python -m pip install -r requirements.txt -t /asset-output/python && cp -R /asset-input /asset-output/python',
],
}),
}));

expect(DockerImage.fromBuild).toHaveBeenCalledWith(expect.stringMatching(entry));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"Resources": {
"myhandlerServiceRole77891068": {
"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"
]
]
}
]
}
},
"myhandlerD202FA8E": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": {
"Ref": "AssetParameters3dc2f7b8375fbf383f44eb8f798d324f60d516946c9f829fca3c5f747f973374S3Bucket07AE44EE"
},
"S3Key": {
"Fn::Join": [
"",
[
{
"Fn::Select": [
0,
{
"Fn::Split": [
"||",
{
"Ref": "AssetParameters3dc2f7b8375fbf383f44eb8f798d324f60d516946c9f829fca3c5f747f973374S3VersionKey01F8F2A1"
}
]
}
]
},
{
"Fn::Select": [
1,
{
"Fn::Split": [
"||",
{
"Ref": "AssetParameters3dc2f7b8375fbf383f44eb8f798d324f60d516946c9f829fca3c5f747f973374S3VersionKey01F8F2A1"
}
]
}
]
}
]
]
}
},
"Role": {
"Fn::GetAtt": [
"myhandlerServiceRole77891068",
"Arn"
]
},
"Handler": "index.handler",
"Runtime": "python3.7"
},
"DependsOn": [
"myhandlerServiceRole77891068"
]
}
},
"Parameters": {
"AssetParameters3dc2f7b8375fbf383f44eb8f798d324f60d516946c9f829fca3c5f747f973374S3Bucket07AE44EE": {
"Type": "String",
"Description": "S3 bucket for asset \"3dc2f7b8375fbf383f44eb8f798d324f60d516946c9f829fca3c5f747f973374\""
},
"AssetParameters3dc2f7b8375fbf383f44eb8f798d324f60d516946c9f829fca3c5f747f973374S3VersionKey01F8F2A1": {
"Type": "String",
"Description": "S3 key for asset version \"3dc2f7b8375fbf383f44eb8f798d324f60d516946c9f829fca3c5f747f973374\""
},
"AssetParameters3dc2f7b8375fbf383f44eb8f798d324f60d516946c9f829fca3c5f747f973374ArtifactHashDECBC32A": {
"Type": "String",
"Description": "Artifact hash for asset \"3dc2f7b8375fbf383f44eb8f798d324f60d516946c9f829fca3c5f747f973374\""
}
},
"Outputs": {
"FunctionArn": {
"Value": {
"Fn::GetAtt": [
"myhandlerD202FA8E",
"Arn"
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as path from 'path';
import { App, CfnOutput, DockerImage, Stack, StackProps } from '@aws-cdk/core';
import { Construct } from 'constructs';
import * as lambda from '../lib';

/*
* Stack verification steps:
* * aws lambda invoke --function-name <deployed fn name> --invocation-type Event --payload '"OK"' response.json
*/

class TestStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);

const entry = path.join(__dirname, 'lambda-handler-custom-build');
const fn = new lambda.PythonFunction(this, 'my_handler', {
entry: entry,
bundling: { image: DockerImage.fromBuild(path.join(entry)) },
});

new CfnOutput(this, 'FunctionArn', {
value: fn.functionArn,
});
}
}

const app = new App();
new TestStack(app, 'cdk-integ-lambda-custom-build');
app.synth();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM public.ecr.aws/sam/build-python3.7

CMD [ "python" ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import requests
from PIL import Image

def handler(event, context):
response = requests.get('https://a0.awsstatic.com/main/images/logos/aws_smile-header-desktop-en-white_59x35.png', stream=True)
img = Image.open(response.raw)

print(response.status_code)
print(img.size)

return response.status_code
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Lock versions of pip packages
certifi==2020.6.20
chardet==3.0.4
idna==2.10
urllib3==1.26.7
# Requests used by this lambda
requests==2.26.0
# Pillow 6.x so that python 2.7 and 3.x can both use this fixture
Pillow==8.4.0

0 comments on commit d813fe0

Please sign in to comment.