Reputation: 9251
Wanting to build a microservice within Lambda.
I want it to be accessible via an HTTP request so I believe I need to configure it with API gateway. I am unsure if I should use API Gateway Proxy Request Event
or API Gateway Proxy Response Event
.
Also in terms of authenticating the user, what is the best way?
I am thinking using Auth0 so a user will essentially just send a JWT with the HTTP request.
Thanks
Upvotes: 0
Views: 551
Reputation: 1951
I use API Gateway Proxy Request. I configure the Integration Request
to use Use Lambda Proxy integration
which will result in Integration Response
updating to Proxy integrations cannot be configured to transform responses.
I then handle responses within the lambda function itself. For example, I will have the following for a successful response:
context.succeed({
statusCode: 200,
headers: { 'Content-Type': 'Application/json'},
body
});
And for an example of an expected fail, I have:
context.succeed({
statusCode: 404,
headers: { 'Content-Type': 'Application/json'},
body: JSON.stringify({'Error': error})
});
NOTE: It is important to note I use context.succeed
on error handling and not context.fail
.
I developed my lambda locally in Node and used a combination of lambda-local
and lambda-tester
for debugging/testing functionality. For example, I would run the following command to pass in the lambda and the event:
lambda-local -f ~/Desktop/lambdaNode/myNewAPI.js -e ~/Desktop/lambdaEvents/testEvent.json
An example event will look like this:
{
"queryStringParameters":
{
"param1": "1234567890",
"param2": "0987654321",
}
}
For my unit tests, I used lambda-tester
along with mocha as follows:
const LambdaTester = require( 'lambda-tester' );
describe('handle my new lambda: ', () => {
it('should do exactly what I expect', () => {
return LambdaTester( myNewAPI.handler )
.event( testEvents.newLambdaEvent )
.expectSucceed( ( result ) => {
expect( result.statusCode ).to.equal(200);
});
});
});
Test the above with npm test
if you're setup for that, otherwise just mocha testFile
For my use case, authentication is handled differently as it is an internal API. For your case, I think JWT is the best way forward there, although I am sorry I cannot provide more info in that regard.
Upvotes: 2