Hi I am fairly new to pulumi, and just wondering h...
# typescript
g
Hi I am fairly new to pulumi, and just wondering how to get an env variable into a cloudwatch event lambda, here is a snippet of what i am trying to do and also my attempt, I have searched all over looking for info on this but no luck
Copy code
const scheduleHandler = async (
  event: aws.cloudwatch.EventRuleEvent
) => {
  // I want to access MY_ENV_VAR here
  // but it doesnt show up in the logs
  console.log("envs", process.env);
}
const lambdaEnvVars: pulumi.Input<{
  [key: string]: pulumi.Input<string>;
}> = {};

lambdaEnvVars["MY_ENV_VAR"] = "my value";
const scheduler: aws.cloudwatch.EventRuleEventSubscription =
  aws.cloudwatch.onSchedule(
    "schedulePublisher",
    "cron(* * * * *)",
    scheduleHandler,
    {
      environment: {
        variables: {
          MY_ENV_VAR: "my value",
        },
      },
  );
for anyone else who comes across this, as a workaround i did
aws lambda update-function-configuration --function-name my-function \
--environment "Variables={BUCKET=my-bucket,KEY=file.txt}"
which i found from the docs https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html
m
Hi there — in general this should work; I’ve written about this same thing in my book in fact, example here: https://github.com/pulumibook/examples/blob/21040fbe7b4bd2a2038567aa6195c0c41b287ead/chapter4/health-checker/index.ts#L8-L48
The URL (which happens to be a secret) gets read from Pulumi config and passed as an environment variable to the
aws.lambda.CallbackFunction
definition, then picked up at runtime by
process.env
as you have in your example. The main difference I see is that in my example, I’m defining the callback function as its own resource and setting the environment variables on it, then passing that function to the
onSchedule
handler.
g
Thanks @miniature-musician-31262 I will check your example out later today
@miniature-musician-31262 that is exactly what i was after, thanks so much