colossal-battery-24701
06/06/2021, 9:14 AMimport * as awsx from "@pulumi/awsx";
const api = new awsx.apigateway.API("example", {
routes: [{
path: "/",
method: "GET",
eventHandler: async (event) => {
const DB_NAME = process.env.DB_NAME;
return {
statusCode: 200,
body: "Hello! The database name is: ${DB_NAME}",
};
},
}],
})
export const url = api.url;
What are the ways that I can add DB_NAME
(or any) env variable?mysterious-belgium-44686
06/06/2021, 3:56 PMCallbackFunction
construct apparently, which maybe would allow that, but I can't find documentation on it. Also, it appears that there's (limited) capture semantics for your handler, so you could just access DB_NAME directly.
You can also pass a Function in directly, so you could construct it as you like.
eventHandler: aws.lambda.Function.get("get-handler", "your_lambda_id"),
https://www.pulumi.com/docs/guides/crosswalk/aws/api-gateway/#defining-a-lambda-function-event-handler-routeminiature-musician-31262
06/06/2021, 4:36 PMconst DB_NAME = process.env.DB_NAME;
up out of the body of the callback (between your import
and const api…
statements, say), the value of DB_NAME
will get captured when the Pulumi program runs, and its value will be serialized into the body of the Lambda.
That’s one way to do it. Another way is as Colin suggests — to pass an aws.lambda.CallbackFunction
as the eventHandler
instead, and give it its own `environment`:
const api = new awsx.apigateway.API("example", {
routes: [{
path: "/",
method: "GET",
eventHandler: new aws.lambda.CallbackFunction("eventHandler", {
callback: async () => {
return {
statusCode: 200,
// Evaluated when the Lambda is executed.
body: `Hello! The database name is: ${process.env.DB_NAME}`,
};
},
environment: {
variables: {
// Evaluated when the Pulumi program runs.
DB_NAME: process.env.DB_NAME || "",
}
},
}),
}],
});
➜ export DB_NAME="my-db-name"
➜ pulumi up --yes --skip-preview
➜ curl <https://j3sq2elstf.execute-api.us-west-2.amazonaws.com/stage/>
Hello! The database name is: my-db-name
I’m a little stumped as to where the docs are for this too, though. Seems like they should be here: https://www.pulumi.com/docs/reference/pkg/aws/lambda/ But I don’t see them. I’ll see if I can find out more on this.environment
is that you’re able to see them in the AWS console as well:colossal-battery-24701
06/08/2021, 5:44 PM