AWS Lambda async/await boilerplate - async-await

Is there an example how to completely use async/await in aws lambda functions? Most lambda example starts with:
module.exports.handler = (event, context, callback) => {
Now I tried to use:
module.exports.hello = async (event, context) => {
but now I have to call a lambda-function in this lambda function.
Can I just write:
'use strict';
const AWS = require('aws-sdk');
const lambda = new AWS.Lambda({
region: 'my-region'
});
let params = {
FunctionName: process.env.lambdafunc, /* required */
Payload: "",
InvocationType: "Event"
};
module.exports.hello = async (event, context) => {
/** HERE COMES SOME CODE AND BUSINESS LOGIC
* ...
* ...
*/
params.Payload = new Buffer(JSON.stringify(MYJSONDATA));
data = await lambda.invokeAsync(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
return {
statusCode: 200,
body: JSON.stringify({
message: 'Go Serverless v1.0! Your function executed successfully!',
data: data
}),
};
};

Related

Mock Lambda.invoke wrapped but not being called

I'm having an issue while trying to mock lambda.invoke which I'm calling from within another lambda function.
The function is wrapped (I can't use sinon after, it will tell me it's already wrapped).
The test keeps calling the lambda function on AWS instead of calling the mock.
It does the same if I use sinon instead of aws-sdk-mock.
test.js
const { handler1 } = require('../handler');
const sinon = require('sinon');
const AWSMock = require('aws-sdk-mock');
describe('invoke', () => {
beforeEach(() => {
invokeMock = jest.fn();
AWSMock.mock('Lambda', 'invoke', invokeMock);
// const mLambda = { invoke: sinon.stub().returnsThis(), promise: sinon.stub() };
// sinon.stub(AWS, 'Lambda').callsFake(() => mLambda);
});
afterEach(() => {
AWSMock.restore();
sinon.restore();
});
test('test1', async () => {
const event = { test: 'ok'};
const handler = await handler1(event);
expect(handler.statusCode).toBe(204);
});
});
and my lambda function is:
handler.js
const AWS = require('aws-sdk');
module.exports.handler1 = (event) => {
// The initialisation bellow has to be in the handler not outside.
const lambda = new AWS.Lambda({
region: 'us-west-2' //change to your region
});
let params = {
InvocationType: 'Event',
LogType: 'Tail',
FunctionName: 'handler2', // the lambda function we are going to invoke
Payload: JSON.stringify(event)
};
return new Promise((resolve, reject) => {
lambda.invoke(params, function(error, data) {
if(error) return reject(error);
const payload = JSON.parse(data.Payload);
if(!payload.success){
return resolve({ statusCode: 400});
}
return resolve({ statusCode: 204});
});
});
};
EDIT: So the issue I had was because I had my lambda initialisation (const lambda = new AWS.Lambda({})) outside the handler instead on inside. Thanks to stijndepestel's answer.
It is not entirely clear from the code you have shared, but presumably, you have a reference to lambda in your handler.js before you have wrapped the function in your test. Could you add the const lambda = new AWS.Lamda({}) line inside your handler function instead of outside of it?

Codepipeline lambda action never complete

Well, my lambda function work's well according to the log's, but it never get completed in the codepipeline stage, I have already set permission to role for allow notificate pipeline ("codepipeline:PutJobSuccessResult",
"codepipeline:PutJobFailureResult") and even set maximun time to 20sec but still not working (it actually ends at 800ms).
const axios = require('axios')
const AWS = require('aws-sdk');
const url = 'www.exampleurl.com'
exports.handler = async (event, context) => {
const codepipeline = new AWS.CodePipeline();
const jobId = event["CodePipeline.job"].id;
const stage = event["CodePipeline.job"].data.actionConfiguration.configuration.UserParameters;
const putJobSuccess = function(message) {
var params = {
jobId: jobId
};
codepipeline.putJobSuccessResult(params, function(err, data) {
if (err) {context.fail(err); }
else {context.succeed(message);}
});
};
const putJobFailure = function(message) {
var params = {
jobId: jobId,
failureDetails: {
message: JSON.stringify(message),
type: 'JobFailed',
externalExecutionId: context.invokeid
}
};
codepipeline.putJobFailureResult(params, function(err, data) {
if (err) console.log(err)
context.fail(message);
});
};
try {
await axios.post(url, { content: stage})
putJobSuccess('all fine')
} catch (e) {
putJobFailure(e)
}
};
The root issue
Because nodeJS runs everything async by default, codepipeline.putJobSuccessResult is being run async. The issue seems to be that the Lambda function is finishing it's execution before codepipeline.putJobSuccessResult has a chance to complete.
The solution
Run codepipeline.putJobSuccessResult synchronously so that it is forced to complete before the response is returned to Lambda for the lambdaHandler.
const putJobSuccess = function(id) {
//await sleep(60);
console.log("Telling Codepipeline test passed for job: " + id)
var params = {
jobId: id
};
return codepipeline.putJobSuccessResult(params, function(err, data) {
if(err) {
console.error(err)
} else {
console.log(data)
}
}).promise()
};
exports.lambdaHandler = async (event, context) => {
...
await putJobSuccess( jobId )
return response
};
Whenever I see this issue, most of the time it is due to 'PutJobSuccessResult' never being invoked. The best way to check this is to go to CloudTrail > 'Event History' and look for 'Event name' = 'PutJobSuccessResult' during the time range you expect the Lambda function calling this API. Probably you will not find the 'PutJobSuccessResult', then please have a look at the code again and the Lambda execution logs in CloudWatch.

Async Lambda function returning null on calling DynamoDB

I am new to async functions and promises. I have written a Lambda function which queries a DynamoDB table and returns the result. The code is executing inside the callback success block and I am able to see the response in the log from the console.log(res) line. However the Lambda response is always showing as null, i.e. the response object below is not returned at all. I was able to make this work using a Synchronous Lambda function using a callback to return the data. Can you please suggest what I may be doing incorrectly.
const doc = require('dynamodb-doc');
var dynamodbclient;
const tablename = process.env.TABLE_NAME;
exports.handler = async(event) => {
if (!dynamodbclient) {
dynamodbclient = new doc.DynamoDB();
}
let id = event.params.id;
let queryparams = {
TableName: 'table-name',
Key: { id: id }
};[![enter image description here][1]][1]
var getItemsCallback = (err, res) => {
console.log('inside');
if (err) {
}
else {
console.log('success');
console.log(res);
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
}
};
var item = await dynamodbclient.getItem(queryparams, getItemsCallback).promise();
};
Your callback is still executing after the promise resolves, so the lambda will terminate and your callback will not finish.
Try:
try {
const item = await dynamodbclient.getItem(queryparams).promise();
} catch (err) {}
console.log('success');
console.log(item);
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;

Apollo Server: How can I send a response based on a callback?

I am currently trying to validate iOS receipts for in app purchases using this package: https://github.com/Wizcorp/node-iap
This is my incomplete resolver:
export default {
Query: {
isSubscribed: combineResolvers(
isAuthenticated,
async (parent, args, { models, currentUser }) => {
const subscription = await models.Subscription.find({ user: currentUser.id });
const payment = {
...
};
iap.verifyPayment(subscription.platform, payment, (error, response) => {
/* How do I return a response here if it is async and I don't have the response object? */
});
}
),
},
};
How do I return a response here if it is async and I don't have the response object? Normally, I'm just used to returning whatever the model returns. However, this time I'm using node-iap and it's callback based.
You can use a Promise:
const response = await new Promise((resolve, reject) => {
iap.verifyPayment(subscription.platform, payment, (error, response) => {
if(error){
reject(error);
}else{
resolve(response);
}
});
});

Netlify Lambda Functions

I am having an issue getting a 502 error back when I call my Netlify function. Is there something I am doing wrong in my Axios call or does the "error" sent in the callback need to be an actual Error object?
Below is the example of my function:
const axios = require('axios')
require('dotenv').config()
const https = require('https')
const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type'
}
exports.handler = function (event, context, callback) {
// your server-side functionality
axios
.post(
`https://us18.api.mailchimp.com/3.0/lists/${
process.env.LIST_ID
}/members/`, {
email_address: 'deuce3608#gmail.com',
status: 'subscribed'
}, {
auth: {
username: 'admin',
password: process.env.MAILCHIMP_API_KEY
}
}
)
.then(response => {
callback(null, {
statusCode: 200,
headers,
body: response.data
})
})
.catch(err => {
callback(JSON.stringify(err.response.data))
})
}
Netlify announced in April (2018) that Node.js 8.10 would be the default in Netlify functions.
Using Callback Parameter:
When you need to return an error in Lambda functions on Netlify using the Callback Parameter, it will be the same format as the Lambda functions for AWS.
You will need to return an Error in the first parameter of the callback as you can see in the AWS documentation
callback(Error error, Object result);
The error is used if not null and the result will be ignored.
Using Async Handler:
You also have the option to return your error in the response with an error status code like the example function below.
import fetch from "node-fetch";
const API_ENDPOINT =
"https://08ad1pao69.execute-api.us-east-1.amazonaws.com/dev/random_joke";
exports.handler = async (event, context) => {
return fetch(API_ENDPOINT)
.then(response => response.json())
.then(data => ({
statusCode: 200,
body: `${data.setup} ${data.punchline} *BA DUM TSSS*`
}))
.catch(error => ({ statusCode: 422, body: String(error) }));
};
Showing simple tests
Error
exports.handler = function(event, context, callback) {
const err = new Error("this is an error")
callback(err);
}
Response (response status code 502):
{"errorMessage":"this is an error","errorType":"Error","stackTrace":["48.exports.handler (/var/task/showerror.js:75:13)"]}
Object
exports.handler = function(event, context, callback) {
const err = {statusCode: 422, body: "this is an error"}
callback(err);
}
Response (response status code 502):
{"errorMessage":"[object Object]"}
String
exports.handler = function(event, context, callback) {
const err = "this is an error"
callback(err);
}
Response (response status code 502):
{"errorMessage":"this is an error"}
NOTE:
If you want to use callback and have the error status code in the response, you would just pass it in an object to the response.
exports.handler = function(event, context, callback) {
const err = {statusCode: 422, body: "this is an error"}
callback(null, err);
}
Response (response status code 422):
this is an error
You could arrange the response like so:
var response = {
statusCode: 4xx
body: ''
}
and then pass it to the callback to return the error
response.body = 'some error text here';
callback(response);
They are using AWS Lambda, so if you can do it in the console you should in theory be able to call things the same way since they are taking your code and deploying it via Cloudformation in their infrastructure.

Resources