moesif-aws-lambdapublic
Moesif AWS Lambda Middleware
Middleware (NodeJS) to automatically log incoming API requests/responses from AWS Lambda functions and send to Moesif for error analysis. Designed for APIs that are hosted on AWS Lambda and using Amazon API Gateway as a trigger.
This middleware expects the Lambda proxy integration type. If you're using AWS Lambda with API Gateway, you are most likely using the proxy integration type.
Express apps
Alternatively, if you're running the Express Framework on AWS Lambda and prefer to use Express middleware, Moesif has Express Middleware also available. The Express Middleware isn't specific to AWS lambda but won't capture AWS specific stuff like Trace Id.
How to install
npm install --save moesif-aws-lambda
How to use
The following shows how import the controllers and use:
1. Import the module:
// Import Modules'use strict'const moesif = ; const moesifOptions = applicationId: 'Your Moesif application_id' { return eventrequestContextidentitycognitoIdentityId }; exports { ;}; exportshandler = ;
2. Enter Moesif Application Id.
You can find your Application Id from Moesif Dashboard -> Top Right Menu -> App Setup
Repo file structure
lib/index.js
the middleware libindex.js
sample AWS Lambda function using the middleware
Configuration options
identifyUser
Type: (event, context) => String
identifyUser is a function that takes AWS lambda event
and context
objects as arguments
and returns a userId. This helps us attribute requests to unique users. Even though Moesif can
automatically retrieve the userId without this, this is highly recommended to ensure accurate attribution.
options.identifyUser = function (event, context) {
// your code here, must return a string
return event.requestContext.identity.cognitoIdentityId
}
getSessionToken
Type: (event, context) => String
getSessionToken a function that takes AWS lambda event
and context
objects as arguments and returns a
session token (i.e. such as an API key).
options { // your code here, must return a string. return eventheaders'Authorization';}
getTags
Type: (event, context) => String
getTags is a function that takes AWS lambda event
and context
objects as arguments and returns a comma-separated string containing a list of tags.
See Moesif documentation for full list of tags.
options { // your code here. must return a comma-separated string. if eventpath && eventhttpMethod == 'GET' return 'user' return 'random_tag_1, random_tag2'}
getApiVersion
Type: (event, context) => String
getApiVersion is a function that takes AWS lambda event
and context
objects as arguments and
returns a string to tag requests with a specific version of your API.
options { // your code here. must return a string. return '1.0.5'}
skip
Type: (event, context) => Boolean
skip is a function that takes AWS lambda event
and context
objects as arguments and returns true
if the event should be skipped (i.e. not logged)
The default is shown below and skips requests to the root path "/".
options { // your code here. must return a boolean. if eventpath === '/' // Skip probes to home page. return true; return false}
maskContent
Type: MoesifEventModel => MoesifEventModel
maskContent is a function that takes the final Moesif event model (rather than the AWS lambda event/context objects) as an
argument before being sent to Moesif. With maskContent, you can make modifications to headers or body such as
removing certain header or body fields.
options { // remove any field that you don't want to be sent to Moesif. return moesifEvent;}
EventModel
format:
For more documentation regarding what fields and meaning, see below or the Moesif Node API Documentation.
Fields | Required | Description |
---|---|---|
request.time | Required | Timestamp for the request in ISO 8601 format |
request.uri | Required | Full uri such as https://api.com/?query=string including host, query string, etc |
request.verb | Required | HTTP method used, i.e. GET , POST |
request.api_version | Optional | API Version you want to tag this request with |
request.ip_address | Optional | IP address of the end user |
request.headers | Required | Headers of the request |
request.body | Optional | Body of the request in JSON format |
response.time | Required | Timestamp for the response in ISO 8601 format |
response.status | Required | HTTP status code such as 200 or 500 |
request.ip_address | Optional | IP address of the responding server |
response.headers | Required | Headers of the response |
response.body | Required | Body of the response in JSON format |
updateUser method
A method is attached to the moesif middleware object to update the users profile or metadata.
'use strict'const moesif = ; const moesifOptions = applicationId: 'Your Moesif application_id' ;var moesifMiddleware = ;var user = userId: 'your user id' // required. metadata: email: 'user@email.com' name: 'George' moesifMiddleware;
The metadata field can be any custom data you want to set on the user. The userId field is required.
Other integrations
To view more more documentation on integration options, please visit the Integration Options Documentation.