dherault / serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project
MIT License
5.19k stars 795 forks source link

How to run lambda integration mode in serverless locally #1724

Open karthikpower opened 1 year ago

karthikpower commented 1 year ago

I want to run serverless application locally earlier this was working using lambda proxy but I want to change it to lambda integration. So I have defined request and response template as follows but when I invoke lambda locally, I see this response,

{
"isBase64Encoded": false,
"statusCode": 200,
"headers": {
    "access-control-allow-origin": "*",
    "content-type": "application/json; charset=utf-8",
    "content-length": "2",
    "etag": "W/\"2-l9Fw4VUO7kr8CvBlt4zaMCqXZ0w\""
},
body

: "{\"error\":\"[{\\"error\\":\\"Invalid query parameters\\",\\"messages\\":[\\"'page' is required\\",\\"'pagesize' is required\\"]}]\"}" } and I expect only body in response this breaks running UI locally , when I run the application in aws I get correct response. Is there any additional configuration to be done in serverless.yml. This is how serverless.yml looks like

service: my-service-api

provider: name: aws runtime: nodejs16.x memorySize: 256 timeout: 10 region: us-west-2 profile: xyz versionFunctions: false endpointType: regional deploymentBucket: name: ps-serverless vpc: xyz apiGateway: restApiId: xyz restApiRootResourceId: xyz restApiResources: /district-settings: xyz plugins:

custom: corsOptions: origin: http://localhost:3000/ headers:

functions: listDistricts: handler: src/districts/district-handler.list events:

Any additional configuration I need to configure to get correct only body in response running locally will be of great help. Also I have GET request with query params that is not passed.

Please note I am using serverless-offline@8.8.1, node version 16. I am running app locally using sls offline start.

this is the lambda code

async (req: Request, res: Response, next: NextFunction): Promise => { try { const districtListQueryParam: DistrictListQueryParam = (req.query as unknown) as DistrictListQueryParam;

  const districtList = <<some district service>>
  res.status(HttpStatusCodes.OK).send(districtList);
} catch (err) {
  logger.error(`Error while listing the districts ${err.message}`);
  next(err);
}

} This is the error message in terminal from catch ,

{"filename":"http-error-middleware.js","level":"error","message":"Error: [{\"error\":\"Invalid query parameters\",\"messages\":[\"'page' is required\". .... Replies:1 comment

karthikpower 2 days ago Author [offline] REQUEST TEMPLATE PROCESSING [offline] Velocity rendered: { "body": "{}", "httpMethod": "GET", "headers": { "Host" : "localhost:3000" , "Connection" : "keep-alive" , "sec-ch-ua" : ""Not/A)Brand";v="99", "Google Chrome";v="115", "Chromium";v="115"" , "Accept" : "application/json, text/plain, /" , "sec-ch-ua-mobile" : "?0" , "User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" , "sec-ch-ua-platform" : ""macOS"" , "Origin" : "http://localhost:8000/" , "Sec-Fetch-Site" : "same-site" , "Sec-Fetch-Mode" : "cors" , "Sec-Fetch-Dest" : "empty" , "Referer" : "http://localhost:8000/" , "Accept-Encoding" : "gzip, deflate, br" , "Accept-Language" : "en-GB,en-US;q=0.9,en;q=0.8" },

"queryStringParameters": { "page" : '1' , "pagesize" : '12' }, ....

[offline] event: {}

This is what I see in terminal : though request template is processed but event is printed as empty. Though I have serverless-offline plugin