Hi, whats happening? I develop my lambda with `npx...
# help
k
Hi, whats happening? I develop my lambda with
npx sst start
everything works fine with the endpoint. Then I
npx sst deploy --stage prod
and I get an error on the frontend:
d
I don't know enough to help based on what I see here, but ime this is because my auth headers are wrong. What permissions do you have on the lambda?
k
thanks for reply, how do I check my lambda permisions?
d
I suspect you set them in your stack or on the lambda.
k
I don't see anything related to the permissions in these files
d
You likely have code something like...
Copy code
// Create the HTTP API
    const api = new sst.Api(this, "Api", {
      routes: {
        "GET /notes": "src/list.main",
        "GET /notes/{id}": "src/get.main",
        "PUT /notes/{id}": "src/update.main",
      },
    });
May I see what you have that's like that?
k
Copy code
import { LayerVersion } from "aws-cdk-lib/aws-lambda";
import * as sst from "@serverless-stack/resources";

const layerArn =
  "arn:aws:lambda:us-east-1:764866452798:layer:chrome-aws-lambda:22";

export default class MyStack extends sst.Stack {
  constructor(scope: <http://sst.App|sst.App>, id: string, props?: sst.StackProps) {
    super(scope, id, props);

    const layer = LayerVersion.fromLayerVersionArn(this, "Layer", layerArn);

    // Create a HTTP API
    const api = new sst.Api(this, "Api", {
      routes: {
        "POST /": {
          function: {
            handler: "src/lambda.handler",
            // Increase the timeout for generating screenshots
            timeout: 15,
            // Load Chrome in a Layer
            layers: [layer],
            // Exclude bundling it in the Lambda function
            bundle: { externalModules: ["chrome-aws-lambda"] },
          },
        },
      },
    });

    // Show the endpoint in the output
    this.addOutputs({
      ApiEndpoint: api.url,
    });
  }
}
d
I see! Thanks for clarifying. So you’re hitting this with a frontend application? Or just the console?
k
frontend
localhost:3000
d
Lots I don’t know with regard to your setup but the two things I’d check first are:
Copy code
// allow cors
new Api(this, "Api", {
  cors: {
    allowMethods: [CorsHttpMethod.GET],
  },
or auth type
Copy code
"POST     /cool-route": {
          authorizationType: sst.ApiAuthorizationType.NONE,
          function: "src/coolFunction.main" 
        },
Perhaps you’ll be able to find what you need here as well https://docs.serverless-stack.com/constructs/Api#configuring-cors
k
Thanks, I'm looking into this
Copy code
const api = new sst.Api(this, "Api", {
      cors: {
        allowMethods: [<http://CorsHttpMethod.POST|CorsHttpMethod.POST>],
        allowOrigins: ["<http://localhost:3000>"],
      },
any ideas?
d
You can allow auth using
Copy code
authorizationType: sst.ApiAuthorizationType.NONE,
from
Copy code
import * as sst from "@serverless-stack/resources";
That should help a touch
k
Thanks. Still same error 😞
Copy code
from origin '<http://localhost:3000>' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
tried with ngrok, but same issue
Copy code
from origin '<https://4d76-78-58-239-79.ngrok.io>' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Copy code
cors: {
        allowHeaders: ["*"],
        allowOrigins: ["*"],
        allowMethods: [CorsHttpMethod.ANY],
      },
I don't know what else todo
d
I've never tried to hit prod with a front end on local host. So I'm sorry I can't be of further help
k
thats ok, you helped a lot
k
try
Copy code
allowOrigins: [<http://localhost:3000>],
without quotes
this is how i do cors import * as apig from "@aws-cdk/aws-apigatewayv2-alpha" cors: { allowOrigins: [process.env.ORIGINS], allowHeaders: ["*"], allowMethods: [apig.CorsHttpMethod.POST, apig.CorsHttpMethod.GET, apig.CorsHttpMethod.PUT, apig.CorsHttpMethod.DELETE], }, and in my .env.local i have ORIGINS=http://localhost:3000
k
OK. found an issue in
stacks/index.js
Copy code
app.setDefaultFunctionProps({
    runtime: "nodejs14.x", // change to "nodejs12.x"
});