https://pulumi.com logo
Title
c

colossal-battery-24701

06/06/2021, 9:14 AM
Can someone help me with passing env variable for lambda created through AWS APIGateway crosswalk
import * 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?
m

mysterious-belgium-44686

06/06/2021, 3:56 PM
There's a
CallbackFunction
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-route
m

miniature-musician-31262

06/06/2021, 4:36 PM
You’ll need to fix the code to use backticks (so the variable is interpolated), but assuming you’ve done that, you’re probably seeing that the value is empty or undefined when the Lambda runs. That’s because the Lambda runtime environment (which runs the function) doesn’t know anything about that variable. If you pull that
const 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.
The nice thing about setting them with
environment
is that you’re able to see them in the AWS console as well:
c

colossal-battery-24701

06/08/2021, 5:44 PM
This worked like a charm. Thank you