I am trying to publish events from aws lambda but I get the following error:
Process exited before completing request
Here's my code
exports.handler = (event, context, callback) => {
kinesis.PutRecord({
"Data": event,
"PartitionKey" : "1",
"StreamName": "TestStream"
});
context.done();
callback(null, "");
}
You have context.done called before the callback function where both are exit callbacks for the handler code. Remove context.done in your code, and also do the following change.
const AWS = require('aws-sdk');
const kinesis = new AWS.Kinesis({apiVersion: '2013-12-02'});
exports.handler = (event, context, callback) => {
kinesis.putRecord({
"Data": event,
"PartitionKey" : "1",
"StreamName": "TestStream"
},
function(err, data) {
if (err)
console.log(err, err.stack); // an error occurred
else
callback(null, data); // successful response
});
}
Related
I'm trying to build my first lambda function that triggered by an API gateway request, then sends to Queue service where I included down below: /when I send a request through api gateway, it gives status code: 200 but queue doesn't get any messages/
var AWS = require('aws-sdk');
var sqs = new AWS.SQS();
exports.handler = function(event, context, callback) {
var params = {
MessageBody: JSON.stringify(event),
QueueUrl: 'https://sqs.ap-southeast-1.amazonaws.com/********/****.fifo'
};
sqs.sendMessage(params, function(err, data) {
if (err) {
console.log('error:', "Fail Send Message", +err);
context.done('error', "ERROR Put SQS");
} else {
console.log('data:', data.MessageId);
context.done(null, '');
}
});
}
I am trying to scan the Dynamodb table form my following code, can anyone please guide me what is wrong here.
const AWS = require("aws-sdk");
const dynamodb = new AWS.DynamoDB({
region: "eu-west-1",
apiVersion: "2012-08-10"
});
exports.handler = async (event, callback) => {
const params = {
TableName: "job_Status"
};
dynamodb.scan(params, (err, data) => {
if (err) {
console.log(err);
callback(err);
} else {
console.log(data);
callback(null, data);
}
});
};
I have given full dynamodb access role to the function but still it gives me the null response. Any idea what can be wrong here?
Response:
I tried with dynaomClient which not working too.
const AWS = require("aws-sdk");
const db = new AWS.DynamoDB.DocumentClient({
region : 'eu-west-1'
});
exports.handler = async (event, callback) => {
const params = {
TableName: "job_Status"
};
db.scan(params, (err, data) => {
if (err) {
console.log(err);
callback(err);
} else {
console.log(data);
callback(null, data);
}
});
};
Your Lambda function is async but your code uses callbacks. By the time the callback is reached, your function has already been terminated because it ran asynchronously. I'd speculate that the null output you see is the return value from the Lambda function, not your console.log.
Replace your call to scan with the following:
try{
let results = await db.scan(params).promise()
console.log(results);
} catch(err){
console.log(err)
}
For more info, check out the AWS documentation about working with promises.
Consider the following code in Lambda:
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'us-east-2', apiVersion: '2012-08-10'});
exports.fn = (event, context, callback) => {
const params = {
Item: {
"UserId": {
S:"dsafsgdhf"
},
"Age": {
N: "28"
},
"Height": {
N: "72"
},
"Income": {
N: "33"
}
},
TableName: "compare-yourself"
};
dynamodb.putItem(params, function(err, data){
if(err){
console.log(err);
callback(err);
} else {
console.log(data);
callback(null, data);
}
});
When I run it, I get the following error:
Response:
{
"errorMessage": "Handler 'handler' missing on module 'index'"
}
Kindly let me know where I must have gone wrong.
As error states, you are missing handler.
You should change this line of code:
exports.fn = (event, context, callback) => {
to
exports.handler = (event, context, callback) => {
The thing is that Lambda function looks for handler as entry point, so you can't just rename that function.
Also, from the code you posted here, you are missing parenthesis at the end ( } ) to close the function definition.
You should use the Handler portion shown in the capture below. You should choose whether to use the Handler as index.fn or exports.hander in your code.
I am trying to create a lambda function to create a user whenever there is an update in one of the tables of dynamo db.
I have created a dynamodb table with a trigger. I get "adminCreateUser not a function" error when i run the below code in lambda.
lambda function:
exports.handler = function(event, context) {
// TODO implement
cognitoidentityserviceprovider.adminCreateUser(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
context.done(null, 'Hello from Lambda');
};
It should be just the code below. Maybe make sure adminCreateUser is in your main AWS SDK, it was only introduced pretty recently.
const AWS = require('aws-sdk');
exports.handler = (event, context, callback) => {
AWS.config.apiVersions = {
cognitoidentityserviceprovider: '2016-04-18'
};
AWS.config.update({accessKeyId: 'akid', secretAccessKey: 'secret'});
var cognito = new AWS.CognitoIdentityServiceProvider({
region: 'us-east-1'
});
params = {};
cognito.adminCreateUser(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else
console.log(data); // successful response
});
context.done(null, 'Hello from Lambda');
};
I've got problem with getting data from database via AWS lambda. I've got code in Node.js:
'use strict';
console.log('Loading function');
let doc = require('dynamodb-doc');
let dynamo = new doc.DynamoDB();
exports.handler = (event, context, callback) => {
//console.log('Received event:', JSON.stringify(event, null, 2));
if (event.tableName) {
event.id.TableName = event.tableName;
}
dynamo.getItem(event.id, callback);
callback(null, event.id); // Echo back the first key value
// callback('Something went wrong');
};
And testing it with such a JSON code:
{
"tableName": "Events",
"payload": "dbf943c3-aaac-473d-9c6a-7651280e0f90"
}
I get an error:
{
"errorMessage": "Process exited before completing request"
}
Does anyone know what is wrong?
PS. My Database is called Events and id of the record which I want to get is: dbf943c3-aaac-473d-9c6a-7651280e0f90