Can someone help me with passing env variable for ...
# general
c
Can someone help me with passing env variable for lambda created through AWS APIGateway crosswalk
Copy code
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
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.
Copy code
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
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`:
Copy code
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 || "",
                }
            },
        }),
    }],
});
Copy code
➜ export DB_NAME="my-db-name"
➜ pulumi up --yes --skip-preview
Copy code
➜ 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
This worked like a charm. Thank you