question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Environment variables in CDK construct

See original GitHub issue

Issue Summary

I struggle to pass CDK values into the builder for Next to use them as environment variables.

Actual behavior

Values come across as CDK tokens:

env: {
  REGION: '${Token[AWS.Region.6]}'
}

Expected behavior

CDK values to be resolved and embedded into Next env correctly.

Steps to reproduce

Pass values from other CDK constructs (e.g. Function ARN or region) around as props and then pass them to the Builder env options.

Additional Context

I’ve read the discussion in https://github.com/serverless-nextjs/serverless-next.js/issues/184, but it seems not applicable to the CDK construct. It seems a CDK specific issue.

Checklist

  • You have reviewed the README and FAQs, which answers several common questions.
  • You have reviewed our DEBUGGING wiki and have tried your best to include complete information and reproduction steps (including your configuration) as is possible. As there is only one maintainer (who maintains this in his free time) and thus very limited resources, if you have time, please try to debug the issue a bit yourself if possible.
  • You have first tried using the most recent latest or alpha @sls-next/serverless-component release version, which may have already fixed your issue or implemented the feature you are trying to use. Note that the old serverless-next.js component and the serverless-next.js plugin are deprecated and no longer maintained.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

2reactions
sladgcommented, Jul 18, 2022

It does not work for Lambda@Edge as env variables are not supported. Soooo slsNext.defaultNextLambda.addEnvironment is not gonna cut it.

For now, what works for me is going for printenv > .env during build time. This sets processEnv vars in output/api-lambda/pages/*.

Downside it’s populated for all functions, so in theory, it might be accessible from outside the API, possibly revealing secrets (have not figure out how to pen test it tho).

Hope it helps.

PS. I’m kinda getting pissed at this Edge limitations and deployment speeds. It really feels like having normal Lambda would be easier to grasp.

2reactions
musou1500commented, Jun 9, 2022

@shishkin

NextJSLambdaEdge has some member variables that holds lambda function construct. https://github.com/serverless-nextjs/serverless-next.js/blob/master/packages/serverless-components/nextjs-cdk-construct/src/index.ts#L56-L60

so, for example…

const slsNext = new NextJSLambdaEdge(this, "NextJsApp", {
  serverlessBuildOutDir: "./build"
});
slsNext.defaultNextLambda.addEnvironment("ENV_VAR_KEY", "ENV_VAR_VALUE");
Read more comments on GitHub >

github_iconTop Results From Across the Web

Environments - AWS Cloud Development Kit (AWS CDK) v2
In an environment-agnostic stack, any constructs that use Availability Zones ... your stack can use two environment variables provided by the AWS CDK...
Read more >
CDK context and Environment | Keep bragging - Technologies
CDK context and Environment. In CDK deployment, it is important to retrieve context and environment variables such as stage, region, user, account, etc....
Read more >
How to use Environment Variables in AWS CDK | bobbyhadz
In order to use environment variables in AWS CDK we have to first set them using the CDK CLI and then access them...
Read more >
Adding environment variables to the Lambda function using ...
In this blog, we will learn how to add environment variables to the Lambda function using CDK. We have learned how to create...
Read more >
How to set environment variable for Lambda using CDK
In the previous post, I show how to make a Lambda function using CDK. In this post, I will show we can set...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found