Skip to content

Commit

Permalink
Upgrade CallbackFunction default runtime to Node 20.x (#4348)
Browse files Browse the repository at this point in the history
This is a breaking change for users of Node automatically provisioned
lambdas, moving them from the Node 16.x to Node 20.x runtime without an
ability to opt out.
 
Prior to this change, Lambda functions provisioned in Node with
CallbackFunction and helper methods such as Bucket.onObjectRemoved,
Bucket.onObjectCreated or sns.Topic.onEvent used Node 16.x runtime by
default. Per
[lambda-runtimes.html](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html#runtimes-supported)
this is now deprecated by AWS. After the change, the new default runtime
is Node 20.x.

Fixes #4134 

The change is supported by strengthening a test to utilize `fetch()` API
that was made available in 18.x and exercise the callback function at
runtime.
  • Loading branch information
t0yv0 authored Aug 8, 2024
1 parent 7332d83 commit 2a3dd78
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 20 deletions.
46 changes: 44 additions & 2 deletions examples/examples_nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,52 @@ func TestAccMinimal(t *testing.T) {
}

func TestAccExpress(t *testing.T) {
// This example is reused to further validate that provisioned CallbackFunctions in Node are working at runtime
// as expected, in particular their default runtime is not deprecated and they can utilize new APIs like the
// fetch() API that is new in the Node 18 runtime.
validate := func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
lambdaRuntime := stack.Outputs["lambdaRuntime"].(string)
t.Logf("Picked the following runtime by default: %v", lambdaRuntime)

lambdaARN := stack.Outputs["lambdaARN"].(string)

// Invoke a given Lambda function using Go SDK v2
sess := getAwsSession(t)
lambdaClient := lambda.New(sess)
result, err := lambdaClient.Invoke(&lambda.InvokeInput{
FunctionName: aws.String(lambdaARN),
Payload: []byte("{}"),
})
require.NoError(t, err)

t.Logf("Raw payload returned by the Lambda: %s", result.Payload)

type data struct {
StatusCode int `json:"statusCode"`
Body string `json:"body"`
}
var payload data
err = json.Unmarshal(result.Payload, &payload)
require.NoError(t, err)

require.Equal(t, 200, payload.StatusCode)

type inner struct {
Message string `json:"message"`
FetchStatus int `json:"fetchStatus"`
}

var response inner
err = json.Unmarshal([]byte(payload.Body), &response)
require.NoError(t, err)

assert.Contains(t, response.Message, "Hello, world!")
assert.Equal(t, 200, response.FetchStatus)
}
test := getJSBaseOptions(t).
With(integration.ProgramTestOptions{
Dir: filepath.Join(getCwd(t), "express"),
RunUpdateTest: true,
Dir: filepath.Join(getCwd(t), "express"),
ExtraRuntimeValidation: validate,
})
skipRefresh(&test)
integration.ProgramTest(t, &test)
Expand Down
19 changes: 16 additions & 3 deletions examples/express/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
// Copyright 2016-2024, Pulumi Corporation. All rights reserved.

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
Expand All @@ -19,7 +19,17 @@ const lambda = new aws.lambda.CallbackFunction<any, any>("mylambda", {

app.get("/", (req, res) => {
console.log("Invoked url: " + req.url);
res.json({ message: hello + "\n\nSucceeded with " + ctx.getRemainingTimeInMillis() + "ms remaining.\n" });

// Test fetch.
// Per https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html
// fetch is available in Node.js 18 and later runtimes
fetch('https://www.pulumi.com/robots.txt').then(resp => {
res.json({
message: hello + "\n\nSucceeded with " + ctx.getRemainingTimeInMillis() + "ms remaining.",
fetchStatus: resp.status,
fetched: resp.text(),
});
});
});

const server = serverlessExpress.createServer(app);
Expand All @@ -32,4 +42,7 @@ const lambda = new aws.lambda.CallbackFunction<any, any>("mylambda", {
serverlessExpress.proxy(server, event, <any>context);
}
}
}, providerOpts);
}, providerOpts);

export const lambdaARN = lambda.arn;
export const lambdaRuntime = lambda.runtime;
9 changes: 5 additions & 4 deletions provider/cmd/pulumi-resource-aws/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -88828,10 +88828,6 @@
"name": "Java8AL2",
"value": "java8.al2"
},
{
"name": "NodeJS16dX",
"value": "nodejs16.x"
},
{
"name": "NodeJS18dX",
"value": "nodejs18.x"
Expand Down Expand Up @@ -88912,6 +88908,11 @@
"value": "nodejs14.x",
"deprecationMessage": "This runtime is now deprecated"
},
{
"name": "NodeJS16dX",
"value": "nodejs16.x",
"deprecationMessage": "This runtime is now deprecated"
},
{
"name": "Custom",
"value": "provided",
Expand Down
2 changes: 1 addition & 1 deletion provider/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -4538,7 +4538,6 @@ compatibility shim in favor of the new "name" field.`)
{Value: "java17", Name: "Java17"},
{Value: "java21", Name: "Java21"},
{Value: "java8.al2", Name: "Java8AL2"},
{Value: "nodejs16.x", Name: "NodeJS16dX"},
{Value: "nodejs18.x", Name: "NodeJS18dX"},
{Value: "nodejs20.x", Name: "NodeJS20dX"},
{Value: "provided.al2", Name: "CustomAL2"},
Expand All @@ -4558,6 +4557,7 @@ compatibility shim in favor of the new "name" field.`)
deprecateRuntime("nodejs10.x", "NodeJS10dX"),
deprecateRuntime("nodejs12.x", "NodeJS12dX"),
deprecateRuntime("nodejs14.x", "NodeJS14dX"),
deprecateRuntime("nodejs16.x", "NodeJS16dX"),
deprecateRuntime("provided", "Custom"),
deprecateRuntime("python2.7", "Python2d7"),
deprecateRuntime("python3.6", "Python3d6"),
Expand Down
3 changes: 2 additions & 1 deletion sdk/dotnet/Lambda/Enums.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions sdk/go/aws/lambda/pulumiEnums.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions sdk/nodejs/lambda/lambdaMixins.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion sdk/nodejs/types/enums/lambda/index.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/python/pulumi_aws/lambda_/_enums.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2a3dd78

Please sign in to comment.