Skip to content

Commit

Permalink
Add tests for replicating lambda serialization problems
Browse files Browse the repository at this point in the history
  • Loading branch information
flostadler committed May 23, 2024
1 parent ba250f3 commit d815227
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/parallel-lambdas-s3/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: kms-alias
runtime: nodejs
description: Parallel Lambdas example
23 changes: 23 additions & 0 deletions examples/parallel-lambdas-s3/create_lambda.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

set -euo pipefail

temp_dir=$(mktemp -d)
trap "rm -rf $temp_dir" EXIT
pushd $temp_dir

printf "const { version } = require(\"@aws-sdk/client-s3/package.json\");\n\nexports.handler = async () => ({ version });\n" > index.js
dd if=/dev/urandom of=example.txt bs=25m count=1
zip lambda.zip index.js example.txt

# Check the size of lambda.zip
zip_size=$(stat -f %z lambda.zip)

# Compare the size with 25MB (25 * 1024 * 1024 bytes)
if [[ $zip_size -lt $((25 * 1024 * 1024)) ]]; then
echo "lambda.zip is smaller than 25MB. Actual size $zip_size."
exit 1
fi

popd
mv $temp_dir/lambda.zip .
68 changes: 68 additions & 0 deletions examples/parallel-lambdas-s3/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as std from "@pulumi/std";

const config = new pulumi.Config("aws");
const providerOpts = { provider: new aws.Provider("prov", { region: <aws.Region>config.require("envRegion") }) };

const assumeRole = aws.iam.getPolicyDocument({
statements: [{
effect: "Allow",
principals: [{
type: "Service",
identifiers: ["lambda.amazonaws.com"],
}],
actions: ["sts:AssumeRole"],
}],
}, providerOpts);
const role = new aws.iam.Role("parallel-iam-role", {
assumeRolePolicy: assumeRole.then(assumeRole => assumeRole.json)
}, providerOpts);

const bucket = new aws.s3.BucketV2("lambda-code-storage", {
forceDestroy: true,
}, providerOpts);

const versioning = new aws.s3.BucketVersioningV2("lambda-code-storage", {
bucket: bucket.id,
versioningConfiguration: {
status: "Enabled",
},
}, providerOpts);

for (let i = 0; i < 50; i++) {
const lambdaCode = new aws.s3.BucketObjectv2(`lambda-zip-${i}`, {
bucket: bucket.bucket,
key: `lambda-${i}.zip`,
source: new pulumi.asset.FileAsset("lambda.zip"),
sourceHash: std.filesha256({
input: "lambda.zip",
}).then(invoke => invoke.result),
}, {...providerOpts, dependsOn: [versioning]});

new aws.lambda.Function(`lambda-${i}`, {
role: role.arn,
handler: "index.handler",
runtime: "nodejs20.x",
s3Bucket: bucket.bucket,
s3Key: lambdaCode.key,
s3ObjectVersion: lambdaCode.versionId,
}, {
...providerOpts,
ignoreChanges: ["replacementSecurityGroupIds"],
});
}
16 changes: 16 additions & 0 deletions examples/parallel-lambdas-s3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "parallel-lambdas-s3",
"version": "0.0.1",
"license": "Apache-2.0",
"scripts": {
"build": "tsc"
},
"dependencies": {
"@pulumi/pulumi": "^3.0.0",
"@pulumi/aws": "^6.0.0",
"@pulumi/std": "^1.6.2"
},
"devDependencies": {
"@types/node": "^8.0.0"
}
}
18 changes: 18 additions & 0 deletions examples/parallel-lambdas-s3/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"strict": true,
"outDir": "bin",
"target": "es2016",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.ts"
]
}
3 changes: 3 additions & 0 deletions examples/parallel-lambdas/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: kms-alias
runtime: nodejs
description: Parallel Lambdas example
23 changes: 23 additions & 0 deletions examples/parallel-lambdas/create_lambda.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

set -euo pipefail

temp_dir=$(mktemp -d)
trap "rm -rf $temp_dir" EXIT
pushd $temp_dir

printf "const { version } = require(\"@aws-sdk/client-s3/package.json\");\n\nexports.handler = async () => ({ version });\n" > index.js
dd if=/dev/urandom of=example.txt bs=25m count=1
zip lambda.zip index.js example.txt

# Check the size of lambda.zip
zip_size=$(stat -f %z lambda.zip)

# Compare the size with 25MB (25 * 1024 * 1024 bytes)
if [[ $zip_size -lt $((25 * 1024 * 1024)) ]]; then
echo "lambda.zip is smaller than 25MB. Actual size $zip_size."
exit 1
fi

popd
mv $temp_dir/lambda.zip .
45 changes: 45 additions & 0 deletions examples/parallel-lambdas/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const config = new pulumi.Config("aws");
const providerOpts = { provider: new aws.Provider("prov", { region: <aws.Region>config.require("envRegion") }) };

const assumeRole = aws.iam.getPolicyDocument({
statements: [{
effect: "Allow",
principals: [{
type: "Service",
identifiers: ["lambda.amazonaws.com"],
}],
actions: ["sts:AssumeRole"],
}],
}, providerOpts);
const role = new aws.iam.Role("parallel-iam-role", {
assumeRolePolicy: assumeRole.then(assumeRole => assumeRole.json)
}, providerOpts);

for (let i = 0; i < 50; i++) {
new aws.lambda.Function(`lambda-${i}`, {
code: new pulumi.asset.FileArchive("lambda.zip"),
role: role.arn,
handler: "index.handler",
runtime: "nodejs20.x",
}, {
...providerOpts,
ignoreChanges: ["replacementSecurityGroupIds"],
});
}
16 changes: 16 additions & 0 deletions examples/parallel-lambdas/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "parallel-lambdas",
"version": "0.0.1",
"license": "Apache-2.0",
"scripts": {
"build": "tsc"
},
"dependencies": {
"@pulumi/pulumi": "^3.0.0",
"@pulumi/aws": "^6.0.0",
"@pulumi/std": "^1.6.2"
},
"devDependencies": {
"@types/node": "^8.0.0"
}
}
18 changes: 18 additions & 0 deletions examples/parallel-lambdas/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"strict": true,
"outDir": "bin",
"target": "es2016",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.ts"
]
}

0 comments on commit d815227

Please sign in to comment.