dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Other
1.74k stars 167 forks source link

Accessing context fields from API Gateway #49

Closed mfolivas closed 6 years ago

mfolivas commented 6 years ago

First, thanks for this library! I need to access some attributes from the API Gateway: the context.requestId and context. requestTime. I was able to fetch the get the requestId by using:

module.exports.handler` = serverless(app, {
  request: (req, event, context) => {
    req.context = event.requestContext;
    req.awsRequestId = context.awsRequestId;
    req.eventAws = event;
    req.contextAws = context;
  }
});

However, I am not seeing the dates. I show al the context, and I got the following:

{
    "resource": "/boosts",
    "path": "/boosts",
    "httpMethod": "POST",
    "headers": {},
    "queryStringParameters": null,
    "pathParameters": null,
    "stageVariables": null,
    "requestContext": {
        "path": "/boosts",
        "accountId": "xxx",
        "resourceId": "xxx",
        "stage": "test-invoke-stage",
        "requestId": "test-invoke-request",
        "identity": {
            "cognitoIdentityPoolId": null,
            "cognitoIdentityId": null,
            "apiKey": "test-invoke-api-key",
            "cognitoAuthenticationType": null,
            "userArn": "arn:aws:iam::xxx:user/Marcelo_Olivas",
            "apiKeyId": "test-invoke-api-key-id",
            "userAgent": "userAgent",
            "accountId": "xxx",
            "caller": "xxx",
            "sourceIp": "sourceIp",
            "accessKey": "xxx",
            "cognitoAuthenticationProvider": null,
            "user": "xxx"
        },
        "resourcePath": "/boosts",
        "httpMethod": "POST",
        "extendedRequestId": "test-invoke-extendedRequestId",
        "apiId": "xxx"
    },
    "body": "{\n\t\"attribute\": \"another.attribute\", \n\t\"boost\" : 1, \n\t\"status\" : \"analysis\"\n}",
    "isBase64Encoded": false
}

Now for the events, I was able to get:

{
    "callbackWaitsForEmptyEventLoop": false,
    "logGroupName": "/aws/lambda/three-eyed-raven-dev-createBoost",
    "logStreamName": "2018/05/11/[$LATEST]9be2c7eadced41b487b87d1a1885d761",
    "functionName": "three-eyed-raven-dev-createBoost",
    "memoryLimitInMB": "1024",
    "functionVersion": "$LATEST",
    "invokeid": "xxx-yyy-11e8-aaa6-aaa",
    "awsRequestId": "xxx-yyy-zzz",
    "invokedFunctionArn": "arn:aws:lambda:us-east-1:aabbvv:function:three-eyed-raven-dev-createBoost"
}

Thanks for the help, and again...thanks for the library!

dougmoscrop commented 6 years ago

Hey! Sorry, I'm not sure I understand your issue.

What happens when you just log context by itself (without using this library)? I can't think of anything that would change this.

dougmoscrop commented 6 years ago

This library doesn't manipulate the request context at all, so if context.requestTime is missing, that is a Lambda/APIg issue. Please re-open if I can somehow help more on this but I think it's out of my control.