I am looking for guidance on how to use Redwood to deploy an API-only application on AWS Lambda, while still supporting local development.
I have a basic example working using the documented example for deploying with serverless.com, but the provided examples only speak to simple paths that don’t include path parameters such as /things/{id}
.
API defined in serverless.yml:
events:
- httpApi:
authorizer:
name: myApiKeyAuthorizer
path: /things
method: GET
- httpApi:
authorizer:
name: myApiKeyAuthorizer
path: /things/{id}
method: GET
AWS documentation/tutorials such as: Tutorial: Build a CRUD API with Lambda and DynamoDB - Amazon API Gateway
recommend using the 2.0 payload/event format that includes an ‘event.routeKey
’, whose values end up looking like ‘GET /things/{id}
’.
Based on this, here is my basic function making use of event.routeKey
to identify what underlying service function to call.
export const handler = async (event, context) => {
logger.info('Invoked things function')
let body
let statusCode = 200
const headers = { 'Content-Type': 'application/json' }
try {
switch (event.routeKey) {
case 'GET /things/{id}':
body = await thing({ id: event.pathParameters.id })
break
case 'GET /things':
body = await things()
break
default:
throw new Error(`Unsupported route: '${event.routeKey}'`)
}
} catch (err) {
statusCode = 400
body = err.message
} finally {
body = JSON.stringify(body)
}
return {
statusCode,
headers,
body,
}
}
The problem is, while this works when the function is deployed on AWS, when invoking the API locally in the dev environment, the event payload does not include the routeKey (or pathParameters).
Event payload on localhost
{
"event": {
"httpMethod": "GET",
"headers": {
"host": "localhost:8911",
"user-agent": "curl/7.79.1",
"accept": "*/*"
},
"path": "/things/1",
"queryStringParameters": {},
"requestContext": {
"requestId": "req-1",
"identity": {
"sourceIp": "::ffff:127.0.0.1"
}
},
"body": "",
"isBase64Encoded": false
}
}
Note that this payload is also lacking the ‘pathParameters
’ object, so extracting the ‘id
’ using ‘event.pathParameters.id
’ is also not possible.
Is there a place in Redwood to configure these API route paths, such that the API system will map the request to a route key and populate it along with the path parameters in the event payload?