Lambda works locally but not when deployed

0 votes

I'm using serverless framework and my endpoint work locally with sls offline, but when I sls deploy it to AWS I get 502 Bad Gateway in postman enter image description here

and if I go to AWS Lambda console and click test event to see what comes up I get

{
  "errorType": "Runtime.UserCodeSyntaxError",
  "errorMessage": "SyntaxError: Unexpected token '??='",
  "trace": [
    "Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token '??='",
    "    at _loadUserApp (/var/runtime/UserFunction.js:222:13)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:300:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:34)",
    "    at Module._compile (internal/modules/cjs/loader.js:1085:14)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)",
    "    at Module.load (internal/modules/cjs/loader.js:950:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:790:12)",
    "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)",
    "    at internal/main/run_main_module.js:17:47"
  ]
}

There's nothing in my code that has ??=' in it so I made my endpoint super simple as a process of elimination and returned a response instantly ie

router.get('/my-api', async (req, res: Response) => {
    return res.status(200).json({"message": "success"});
});

but I still get the same error. serverless.yaml

service: my-service

plugins:
    - serverless-webpack
    - serverless-offline

custom:
    env:
        default: Sandbox
        prod: Production
        AWS_REGION: us-east-1

    apigwBinary:
        types: #list of mime-types
            - '*/*'

    contentCompression: 14000

    webpack:
        webpackConfig: ./webpack.config.js
        packager: 'npm'
        includeModules:
            forceExclude:
                - aws-sdk
                - dotenv

provider:
    name: aws
    versionFunctions: false
    region: ${opt:region, 'us-east-1'}
    stage: ${opt:stage, 'staging'}
    environment:
        STAGE: ${self:provider.stage}
functions:
    myApi:
        handler: src/handler.service
        events:
            - http:
                  path: route/my-api
                  method: GET
                  cors: true

handler.ts

import { APIGatewayProxyEvent, Context, ProxyResult } from 'aws-lambda';
import awsServerlessExpress from 'aws-serverless-express';
import app from './app';

const server = awsServerlessExpress.createServer(app);

export function service(event: APIGatewayProxyEvent, context: Context) {
    return awsServerlessExpress.proxy(server, event, context);
}

So as a recap, it works locally, but not after sls deploy in AWS in postman or aws console lambda test event. So if it works locally, why isn't it working in aws. I appreciate any help!

Feb 16, 2023 in AWS by Ashwini
• 5,430 points
603 views

1 answer to this question.

0 votes

The error message you received when testing your function in the AWS Lambda console indicates a syntax error in your code. The error message specifically points to the line containing the "??=" operator, which is the nullish coalescing operator. This operator is only supported in newer versions of Node.js, so it's possible that the version of Node.js on AWS Lambda does not support this operator.

To fix this issue, you can try updating your version of Node.js or replacing the nullish coalescing operator with an alternative solution that is compatible with the version of Node.js running on AWS Lambda. For example, you could use a ternary operator or an if statement to check for nullish values.

In addition to that, I noticed that you are using serverless-webpack to package your code. It's possible that the webpack configuration is causing issues with your code. You can try disabling serverless-webpack by removing it from the plugins list in your serverless.yaml file and deploying your code again to see if the issue is resolved.

Lastly, make sure that your AWS credentials are properly configured and that your IAM role has the necessary permissions to run your Lambda function and access other AWS services.

Ready to level up your coding game? Uncover the realm of microservices with our cutting-edge Microservices Developer Certification!

answered Feb 17, 2023 by sarit
• 1,830 points

Related Questions In AWS

+1 vote
10 answers
+2 votes
3 answers
+3 votes
3 answers
+1 vote
3 answers

Not able to pass params POST to AWS Lambda from Amazon API Gateway

For this template : application/x-www-form-urlencoded  This line below will ...READ MORE

answered Jun 13, 2018 in AWS by Cloud gunner
• 4,670 points
8,321 views
+1 vote
1 answer

Redirect working from www.xyz.com to https://xyz.com but not from https://www.xyz.com

Make sure you have created the certificate ...READ MORE

answered Aug 28, 2018 in AWS by Priyaj
• 58,090 points
1,396 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Xcode not found in the PATH and expo cannot run iOS simulator

It sounds like you've encountered a few ...READ MORE

answered Feb 17, 2023 in AWS by sarit
• 1,830 points
455 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP