0

I have a lambda function that is supposed to return an HTML page.

This page needs a number of dynamic variables as well as custom stylesheets to function properly.

I have tried the regular way of returning HTML as a string, setting the correct content-type and substituting the dynamic variables I need within that string. This works.

However, I want to use a templating engine rather than writing out strings of HTML as Javascript.

I've tried using nunjucks but it's unable to find my HTML file since the build folder only has an app.js in it.

7
  • 1
    You need to add all modules to the lamda: docs.aws.amazon.com/lambda/latest/dg/nodejs-package.html Commented Jan 9, 2023 at 22:08
  • I'm actually using cdk deploy to deploy my stack. I'm not really directly updating my aws function anywhere. To be honest, the CDK bit is kind of a black box to me as well. Commented Jan 9, 2023 at 22:15
  • Not sure what "Lambda function spun up on the fly using the CDK" means. Are you saying you are just giving it a single nodejs script file to deploy? You probably need to be bundling your Lambda function into a deployment .zip file, and configuring the CDK to deploy that instead. Commented Jan 9, 2023 at 22:26
  • I'm afraid you need to learn CDK if you intend to use it, or hire someone to write the code if you prefer to keep this black box closed. This is the first search result of how to use CDK to deploy npm dependencies github.com/AntoniusGolly/cdk-lambda-typescript-boilerplate Vivisect it, learn and apply to your usecase. It still does the same thing as in the article - installs dependencies, zips them, and pushes to aws. Commented Jan 9, 2023 at 22:37
  • 1
    So, it turns out the file zipping and uploading to AWS was already happening. I just didn't know. What I had to end up doing is adding a copy command to the build command and actually copy my html files to the build output folder. Probably not a long-term solution but it fixed my issue. Commented Jan 10, 2023 at 0:24

1 Answer 1

1

Assuming all your files are part of the bundle being uploaded to AWS, here's how I fixed the issue of the HTML file not being found:

The output of my esbuild is just a single app.js file and this is what was originally being pushed to AWS. I had to add a cp command to my build phase which copies all my .html files from my working directory to the build directory.

Once this is done, you can simply refer to your .html files using a combination of path and __dirname and Nunjucks (or any other templating engine) should be able to find your files without any issues.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.