From 2c13b9f2e7d5a622699f3a1669a752990f53bc81 Mon Sep 17 00:00:00 2001 From: Luke Hoban Date: Thu, 16 Dec 2021 18:23:54 -0800 Subject: [PATCH] Add Lambda invocation example/test Part of #11. --- .gitignore | 3 ++- examples/examples_nodejs_test.go | 13 +++++++++++++ examples/lambda/Pulumi.yaml | 3 +++ examples/lambda/index.ts | 21 +++++++++++++++++++++ examples/lambda/package.json | 12 ++++++++++++ examples/lambda/tsconfig.json | 18 ++++++++++++++++++ 6 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 examples/lambda/Pulumi.yaml create mode 100644 examples/lambda/index.ts create mode 100644 examples/lambda/package.json create mode 100644 examples/lambda/tsconfig.json diff --git a/.gitignore b/.gitignore index f0e680fa..73c7e7c4 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,5 @@ sdk/dotnet/version.txt rsa* password.txt password2.txt -private_ip.txt \ No newline at end of file +private_ip.txt +out.txt \ No newline at end of file diff --git a/examples/examples_nodejs_test.go b/examples/examples_nodejs_test.go index d037869f..2ae6c973 100644 --- a/examples/examples_nodejs_test.go +++ b/examples/examples_nodejs_test.go @@ -86,6 +86,19 @@ func TestEc2RemoteTs(t *testing.T) { integration.ProgramTest(t, &test) } +func TestLambda(t *testing.T) { + test := getJSBaseOptions(t). + With(integration.ProgramTestOptions{ + Dir: filepath.Join(getCwd(t), "lambda"), + ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) { + out, ok := stack.Outputs["output"].(string) + assert.True(t, ok) + assert.Len(t, out, 10) + }, + }) + integration.ProgramTest(t, &test) +} + func getJSBaseOptions(t *testing.T) integration.ProgramTestOptions { base := getBaseOptions(t) baseJS := base.With(integration.ProgramTestOptions{ diff --git a/examples/lambda/Pulumi.yaml b/examples/lambda/Pulumi.yaml new file mode 100644 index 00000000..238a2ea6 --- /dev/null +++ b/examples/lambda/Pulumi.yaml @@ -0,0 +1,3 @@ +name: command-lambda +runtime: nodejs +description: A simple command example diff --git a/examples/lambda/index.ts b/examples/lambda/index.ts new file mode 100644 index 00000000..ca12f98c --- /dev/null +++ b/examples/lambda/index.ts @@ -0,0 +1,21 @@ +import { local } from "@pulumi/command"; +import * as aws from "@pulumi/aws"; +import * as crypto from "crypto"; + +const f = new aws.lambda.CallbackFunction("f", { + publish: true, + callback: async (ev: any) => { + return crypto.randomBytes(ev.len/2).toString('hex'); + } +}); + +const rand = new local.Command("execf", { + create: `aws lambda invoke --function-name "$FN" --payload '{"len": 10}' --cli-binary-format raw-in-base64-out out.txt >/dev/null && cat out.txt | tr -d '"' && rm out.txt`, + environment: { + FN: f.qualifiedArn, + AWS_REGION: aws.config.region!, + AWS_PAGER: "", + }, +}) + +export const output = rand.stdout; diff --git a/examples/lambda/package.json b/examples/lambda/package.json new file mode 100644 index 00000000..3e8cad9d --- /dev/null +++ b/examples/lambda/package.json @@ -0,0 +1,12 @@ +{ + "name": "command-lambda", + "version": "0.1.0", + "devDependencies": { + "@types/node": "latest" + }, + "dependencies": { + "@pulumi/aws": "^4.32.0", + "@pulumi/pulumi": "latest", + "@pulumi/random": "^4.2.0" + } +} diff --git a/examples/lambda/tsconfig.json b/examples/lambda/tsconfig.json new file mode 100644 index 00000000..2666e28e --- /dev/null +++ b/examples/lambda/tsconfig.json @@ -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" + ] +} \ No newline at end of file