Skip to main content

Set up Lambda

1) Create a new lambda#

2) Link lambda layer with the lambda function#

  • Scroll to the bottom and look for the Layers tab. Click on Add a layer

    Link Lambda function with the Lambda layer
  • Select Custom Layer and then select the layer we created in the first step:

Link custome layer with Lambda function Node

3) Create a backend config file#

Using the editor provided by AWS, create a new config file and write the following code:

Your app's name:*
Information about the question
This is the name of your application
API Domain:*
Information about the question
This is the URL of your app's API server.
API Base Path:
Information about the question
SuperTokens will expose it's APIs scoped by this base API path.
Website Domain:*
Information about the question
This is the URL of your website.
Website Base Path:
Information about the question
The path where the login UI will be rendered
Submit form
How do you want to identify your users?
Only phone numberOnly emailEmail or phone number
Which authentication type will you use?
OTPMagic linksOTP and Magic link
important

In the above code, notice the extra config of apiGatewayPath that was added to the appInfo object. The value of this should be whatever you have set as the value of your AWS stage which scopes your API endpoints. For example, you may have a stage name per dev environment:

  • One for development (/dev).
  • One for testing (/test).
  • One for prod (/prod).

So the value of apiGatewayPath should be set according to the above based on the env it's running under.

You also need to change the apiBasePath on the frontend config to append the stage to the path. For example, if the frontend is query the development stage and the value of apiBasePath is /auth, you should change it to /dev/auth.

note

You may edit the apiBasePath and apiGatewayPath value later if you haven't setup the API Gateway yet.

4) Add the SuperTokens auth middleware#

Using the editor provided by AWS, create/replace the handler file contents with the following code:

index.mjs
import supertokens from "supertokens-node";
import { middleware } from "supertokens-node/framework/awsLambda";
import { getBackendConfig } from "./config.mjs";
import middy from "@middy/core";
import cors from "@middy/http-cors";

supertokens.init(getBackendConfig());

export const handler = middy(
middleware((event) => {
// SuperTokens middleware didn't handle the route, return your custom response
return {
body: JSON.stringify({
msg: "Hello!",
}),
statusCode: 200,
};
})
)
.use(
cors({
origin: getBackendConfig().appInfo.websiteDomain,
credentials: true,
headers: ["Content-Type", ...supertokens.getAllCORSHeaders()].join(", "),
methods: "OPTIONS,POST,GET,PUT,DELETE",
})
)
.onError((request) => {
throw request.error;
});
Add Supertokens auth middleware UI
important

Since, we are using esm imports, we will need to set NODE_OPTIONS="--experimental-specifier-resolution=node" flag in the lambda environment variables. See the Node.js documentation for more information.

Configuring env variables UI
Looking for older versions of the documentation?
Which UI do you use?
Custom UI
Pre built UI