Skip to content

Commit

Permalink
Add tests to ensure SAM supports CDK apps where Docker image asset `f…
Browse files Browse the repository at this point in the history
…ile` is a path to a docker file via subdirectories.
  • Loading branch information
i18n-tribe committed Oct 11, 2022
1 parent 8cc2b5d commit 752ac7f
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 4 deletions.
10 changes: 10 additions & 0 deletions tests/iac_integration/cdk/test_sam_cdk_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ def random_port():
("/restapis/normal/functionGoRuntime", "Hello World from function construct with go runtime"),
("/restapis/normal/dockerImageFunction", "Hello World from docker image function construct"),
("/restapis/normal/functionImageAsset", "Hello World from function construct with image asset"),
(
"/restapis/normal/dockerImageFunctionWithSharedCode",
"Hello World from docker image function construct "
"with a Dockerfile that shares code with another Dockerfile",
),
(
"/restapis/normal/functionImageAssetWithSharedCode",
"Hello World from function construct with image asset "
"with a Dockerfile that shares code with another Dockerfile",
),
]
)
@pytest.mark.flaky(reruns=3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,23 @@ public JavaStack(final Construct scope, final String id, final StackProps props)
.tracing(Tracing.ACTIVE)
.build();

// both ways work when 'file' is a path via subfolders to the Dockerfile
// this is useful when multiple docker images share some common code
DockerImageFunction dockerImageFunctionWithSharedCode = DockerImageFunction.Builder.create(this, "DockerImageFunctionWithSharedCode")
.code(DockerImageCode.fromImageAsset("../../src/docker/ImagesWithSharedCode",
AssetImageCodeProps.builder().file("DockerImageFunctionWithSharedCode/Dockerfile").build()
)
).tracing(Tracing.ACTIVE)
.build();

Function functionImageAssetWithSharedCode = Function.Builder.create(this, "FunctionImageAssetWithSharedCode")
.code(Code.fromAssetImage("../../src/docker/ImagesWithSharedCode",
AssetImageCodeProps.builder().file("FunctionImageAssetWithSharedCode/Dockerfile").build()))
.handler(Handler.FROM_IMAGE)
.runtime(Runtime.FROM_IMAGE)
.tracing(Tracing.ACTIVE)
.build();

//Rest APIs

// Spec Rest Api
Expand Down Expand Up @@ -239,6 +256,10 @@ public JavaStack(final Construct scope, final String id, final StackProps props)
.addMethod("GET", new LambdaIntegration(dockerImageFunction));
normalRootResource.addResource("functionImageAsset")
.addMethod("GET", new LambdaIntegration(functionImageAsset));
normalRootResource.addResource("dockerImageFunctionWithSharedCode")
.addMethod("GET", new LambdaIntegration(dockerImageFunctionWithSharedCode));
normalRootResource.addResource("functionImageAssetWithSharedCode")
.addMethod("GET", new LambdaIntegration(functionImageAssetWithSharedCode));

// Nested Stack
new NestedStack1(this, "NestedStack");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
self,
"DockerImageFunction",
code=lambda1.DockerImageCode.from_image_asset(
"../../src/docker/DockerImageFunctionConstruct",
directory="../../src/docker/DockerImageFunctionConstruct",
file="Dockerfile",
),
tracing=lambda1.Tracing.ACTIVE,
Expand All @@ -228,14 +228,38 @@ def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
self,
"FunctionImageAsset",
code=lambda1.Code.from_asset_image(
"../../src/docker/FunctionConstructWithImageAssetCode",
directory="../../src/docker/FunctionConstructWithImageAssetCode",
file="Dockerfile",
),
handler=lambda1.Handler.FROM_IMAGE,
runtime=lambda1.Runtime.FROM_IMAGE,
tracing=lambda1.Tracing.ACTIVE,
)

# both ways work when 'file' is a path via subfolders to the Dockerfile
# this is useful when multiple docker images share some common code
docker_image_function_with_shared_code = lambda1.DockerImageFunction(
self,
"DockerImageFunctionWithSharedCode",
code=lambda1.DockerImageCode.from_image_asset(
directory="../../src/docker/ImagesWithSharedCode",
file="DockerImageFunctionWithSharedCode/Dockerfile",
),
tracing=lambda1.Tracing.ACTIVE,
)

function_image_asset_with_shared_code = lambda1.Function(
self,
"FunctionImageAssetWithSharedCode",
code=lambda1.Code.from_asset_image(
directory="../../src/docker/ImagesWithSharedCode",
file="FunctionImageAssetWithSharedCode/Dockerfile",
),
handler=lambda1.Handler.FROM_IMAGE,
runtime=lambda1.Runtime.FROM_IMAGE,
tracing=lambda1.Tracing.ACTIVE,
)

# Rest APIs

# Spec Rest Api
Expand Down Expand Up @@ -287,6 +311,12 @@ def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
normal_root_resource.add_resource("functionImageAsset").add_method(
"GET", LambdaIntegration(function_image_asset)
)
normal_root_resource.add_resource("dockerImageFunctionWithSharedCode").add_method(
"GET", LambdaIntegration(docker_image_function_with_shared_code)
)
normal_root_resource.add_resource("functionImageAssetWithSharedCode").add_method(
"GET", LambdaIntegration(function_image_asset_with_shared_code)
)

# Nested Stack
NestedStack1(self, "NestedStack")
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,25 @@ export class CDKSupportDemoRootStack extends cdk.Stack {
tracing: lambda.Tracing.ACTIVE,
});

// both ways work when 'file' is a path via subfolders to the Dockerfile
// this is useful when multiple docker images share some common code
const dockerImageFunctionWithSharedCode = new lambda.DockerImageFunction(this, "DockerImageFunctionWithSharedCode", {
code: lambda.DockerImageCode.fromImageAsset("../../src/docker/ImagesWithSharedCode", {
file: "DockerImageFunctionWithSharedCode/Dockerfile",
}),
tracing: lambda.Tracing.ACTIVE,
});

// another way
const functionImageAssetWithSharedCode = new lambda.Function(this, "FunctionImageAssetWithSharedCode", {
code: lambda.Code.fromAssetImage("../../src/docker/ImagesWithSharedCode", {
file: "FunctionImageAssetWithSharedCode/Dockerfile",
}),
handler: lambda.Handler.FROM_IMAGE,
runtime: lambda.Runtime.FROM_IMAGE,
tracing: lambda.Tracing.ACTIVE,
});


//Rest APIs

Expand Down Expand Up @@ -246,6 +265,12 @@ export class CDKSupportDemoRootStack extends cdk.Stack {

normalRootResource.addResource('functionImageAsset')
.addMethod('GET', new LambdaIntegration(functionImageAsset));

normalRootResource.addResource('dockerImageFunctionWithSharedCode')
.addMethod('GET', new LambdaIntegration(dockerImageFunctionWithSharedCode));

normalRootResource.addResource('functionImageAssetWithSharedCode')
.addMethod('GET', new LambdaIntegration(functionImageAssetWithSharedCode));

// Nested Stack
new NestedStack1(this, 'NestedStack' ,{});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,23 @@ public JavaStack(final Construct scope, final String id, final StackProps props)
.tracing(Tracing.ACTIVE)
.build();

// both ways work when 'file' is a path via subfolders to the Dockerfile
// this is useful when multiple docker images share some common code
DockerImageFunction dockerImageFunctionWithSharedCode = DockerImageFunction.Builder.create(this, "DockerImageFunctionWithSharedCode")
.code(DockerImageCode.fromImageAsset("../../src/docker/ImagesWithSharedCode",
AssetImageCodeProps.builder().file("DockerImageFunctionWithSharedCode/Dockerfile").build()
)
).tracing(Tracing.ACTIVE)
.build();

Function functionImageAssetWithSharedCode = Function.Builder.create(this, "FunctionImageAssetWithSharedCode")
.code(Code.fromAssetImage("../../src/docker/ImagesWithSharedCode",
AssetImageCodeProps.builder().file("FunctionImageAssetWithSharedCode/Dockerfile").build()))
.handler(Handler.FROM_IMAGE)
.runtime(Runtime.FROM_IMAGE)
.tracing(Tracing.ACTIVE)
.build();

//Rest APIs

// Spec Rest Api
Expand Down Expand Up @@ -240,6 +257,11 @@ public JavaStack(final Construct scope, final String id, final StackProps props)
.addMethod("GET", new LambdaIntegration(dockerImageFunction));
normalRootResource.addResource("functionImageAsset")
.addMethod("GET", new LambdaIntegration(functionImageAsset));
normalRootResource.addResource("dockerImageFunctionWithSharedCode")
.addMethod("GET", new LambdaIntegration(dockerImageFunctionWithSharedCode));
normalRootResource.addResource("functionImageAssetWithSharedCode")
.addMethod("GET", new LambdaIntegration(functionImageAssetWithSharedCode));

// Nested Stack
new NestedStack1(this, "NestedStack");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
self,
"DockerImageFunction",
code=lambda1.DockerImageCode.from_image_asset(
"../../src/docker/DockerImageFunctionConstruct",
directory="../../src/docker/DockerImageFunctionConstruct",
file="Dockerfile",
),
tracing=lambda1.Tracing.ACTIVE,
Expand All @@ -228,14 +228,38 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
self,
"FunctionImageAsset",
code=lambda1.Code.from_asset_image(
"../../src/docker/FunctionConstructWithImageAssetCode",
directory="../../src/docker/FunctionConstructWithImageAssetCode",
file="Dockerfile",
),
handler=lambda1.Handler.FROM_IMAGE,
runtime=lambda1.Runtime.FROM_IMAGE,
tracing=lambda1.Tracing.ACTIVE,
)

# both ways work when 'file' is a path via subfolders to the Dockerfile
# this is useful when multiple docker images share some common code
docker_image_function_with_shared_code = lambda1.DockerImageFunction(
self,
"DockerImageFunctionWithSharedCode",
code=lambda1.DockerImageCode.from_image_asset(
directory="../../src/docker/ImagesWithSharedCode",
file="DockerImageFunctionWithSharedCode/Dockerfile",
),
tracing=lambda1.Tracing.ACTIVE,
)

function_image_asset_with_shared_code = lambda1.Function(
self,
"FunctionImageAssetWithSharedCode",
code=lambda1.Code.from_asset_image(
directory="../../src/docker/ImagesWithSharedCode",
file="FunctionImageAssetWithSharedCode/Dockerfile",
),
handler=lambda1.Handler.FROM_IMAGE,
runtime=lambda1.Runtime.FROM_IMAGE,
tracing=lambda1.Tracing.ACTIVE,
)

# Rest APIs

# Spec Rest Api
Expand Down Expand Up @@ -287,6 +311,12 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
normal_root_resource.add_resource("functionImageAsset").add_method(
"GET", LambdaIntegration(function_image_asset)
)
normal_root_resource.add_resource("dockerImageFunctionWithSharedCode").add_method(
"GET", LambdaIntegration(docker_image_function_with_shared_code)
)
normal_root_resource.add_resource("functionImageAssetWithSharedCode").add_method(
"GET", LambdaIntegration(function_image_asset_with_shared_code)
)

# Nested Stack
NestedStack1(self, "NestedStack")
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,25 @@ export class CDKSupportDemoRootStack extends cdk.Stack {
tracing: lambda.Tracing.ACTIVE,
});

// both ways work when 'file' is a path via subfolders to the Dockerfile
// this is useful when multiple docker images share some common code
const dockerImageFunctionWithSharedCode = new lambda.DockerImageFunction(this, "DockerImageFunctionWithSharedCode", {
code: lambda.DockerImageCode.fromImageAsset("../../src/docker/ImagesWithSharedCode", {
file: "DockerImageFunctionWithSharedCode/Dockerfile",
}),
tracing: lambda.Tracing.ACTIVE,
});

// another way
const functionImageAssetWithSharedCode = new lambda.Function(this, "FunctionImageAssetWithSharedCode", {
code: lambda.Code.fromAssetImage("../../src/docker/ImagesWithSharedCode", {
file: "FunctionImageAssetWithSharedCode/Dockerfile",
}),
handler: lambda.Handler.FROM_IMAGE,
runtime: lambda.Runtime.FROM_IMAGE,
tracing: lambda.Tracing.ACTIVE,
});


//Rest APIs

Expand Down Expand Up @@ -246,6 +265,12 @@ export class CDKSupportDemoRootStack extends cdk.Stack {
normalRootResource.addResource('functionImageAsset')
.addMethod('GET', new LambdaIntegration(functionImageAsset));

normalRootResource.addResource('dockerImageFunctionWithSharedCode')
.addMethod('GET', new LambdaIntegration(dockerImageFunctionWithSharedCode));

normalRootResource.addResource('functionImageAssetWithSharedCode')
.addMethod('GET', new LambdaIntegration(functionImageAssetWithSharedCode));

// Nested Stack
new NestedStack1(this, 'NestedStack' ,{});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM public.ecr.aws/lambda/nodejs:14

# Add the lambda handler for this feature
COPY DockerImageFunctionWithSharedCode/app.js ./

# Add the shared code
COPY SharedCode/ ./SharedCode/

# Add the shared dependencies
COPY package.json ./

# Install the dependencies
RUN npm install

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "app.lambdaHandler" ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var gen = require('unique-names-generator');
const {sayHelloWorld} = require("SharedCode/shared");

const colorName = gen.uniqueNamesGenerator({
dictionaries: [gen.colors]
});


exports.lambdaHandler = async(event, context) => {
let response;

try {
response = {
'statusCode': 200,
'body': JSON.stringify({
message: sayHelloWorld("docker image function construct"),
}),
};
} catch (err) {
console.log(err);
return err;
}
return response;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM public.ecr.aws/lambda/nodejs:14

# Add the lambda handler for this feature
COPY FunctionImageAssetWithSharedCode/app.js ./

# Add the shared code
COPY SharedCode/ ./SharedCode/

# Add the shared dependencies
COPY package.json ./

# Install the dependencies
RUN npm install

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "app.lambdaHandler" ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var gen = require('unique-names-generator');
const {sayHelloWorld} = require("SharedCode/shared");

const colorName = gen.uniqueNamesGenerator({
dictionaries: [gen.colors]
});


exports.lambdaHandler = async(event, context) => {
let response;

try {
response = {
'statusCode': 200,
'body': JSON.stringify({
message: sayHelloWorld("function construct with image asset"),
}),
};
} catch (err) {
console.log(err);
return err;
}
return response;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export function sayHelloWorld(from) {
return `Hello World from ${from} with a Dockerfile that shares code with another Dockerfile`;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "randomColors",
"version": "0.1.0",
"bin": {
"randomColors": "bin/app.js"
},
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"test": "jest",
"cdk": "cdk"
},
"dependencies": {
"unique-names-generator": "^4.6.0"
}
}

0 comments on commit 752ac7f

Please sign in to comment.