AWS - Call the declared variable in another lambda - amazon-web-services

So I have 3 lambdas that connects to the routes of the API Gateway. One(connectFunction)for $connect, One(disconnectFunction) for $disconnect, and another one(onMessageFunction) for my added route named onMessage.
My problem is I declared const usernameid=event.queryStringParameters.usernameid in my lambda for $connect and I will use that usernameid in my onMessage route. How can I do this?
Below is my snippet code for connectFunction lambda and onMessageFunction lambda:
connectFunction
exports.handler = (event, context, callback) => {
const connection_Id = event.requestContext.connection_Id;
const usernameid = event.queryStringParameters.usernameid;
addConnection(connection_Id,usernameid).then(() => {
callback(null, {
statusCode: 200,
});
});};
onMessageFunction
let sending = undefined;
function init(event) {
const apigwManagementApi = new AWS.ApiGatewayManagementApi({
apiVersion: '2018-11-29',
endpoint: event.requestContext.domainName + '/' + event.requestContext.stage
});
sending = async (connection_Id, data) => {
await apigwManagementApi.postToConnection({
Connection_Id: connection_Id,
Data: `Echo: ${data}`}).promise();
}}
The code is correct but the output is Echo: data and I want to be the username which I inputted and the data so dev(this is the username):data should be the output

Related

Lambda connect to Dynamodb

I have lambda function like below
// Loads in the AWS SDK
const AWS = require('aws-sdk');
// Creates the document client specifing the region
const ddb = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});
exports.handler = async (event, context, callback) => {
// Handle promise fulfilled/rejected states
await readMessage().then(data => {
data.Items.forEach(function(item) {
console.log(item.message)
});
callback(null, {
// If success return 200, and items
statusCode: 200,
body: data.Items,
headers: {
'Access-Control-Allow-Origin': '*',
},
})
}).catch((err) => {
// If an error occurs write to the console
console.error(err);
})
};
// Function readMessage
// Reads 10 messages from the DynamoDb table Message
// Returns promise
function readMessage() {
const params = {
TableName: 'Message',
Limit: 10
}
return ddb.scan(params).promise();
}
my dynamo db is in region us-east-1 , but above code is not running , it is just timing out.But i am able to run simple hello world inside handler function , and it just works fine.
I have enabled VPC with 6 subnets.. also disabled VPC , not matter what it is timing out without any error logs.. I even increased timeout to 5 mins , it did not do anything.Should I install aws-sdk or something ? Sorry I am completely new to lambda.

Having Issues with external http requests on AWS Lambda

I'm using cities stored in a DynamoDB table to make calls to an external weather API to determine whether it will rain in that city on that day. It then calls a separate Lambda that uses that info to message subscribers of an SNS topic that it will rain.
const AWS = require('aws-sdk');
AWS.config.update({ region: 'eu-west-2' });
const lambda = new AWS.Lambda();
const axios = require('axios');
const tableName = process.env.CITY_TABLE;
const docClient = new AWS.DynamoDB.DocumentClient();
const publishMessageLambda = process.env.PUBLISH_MESSAGE_LAMBDA_NAME;
const weatherApiKey = process.env.WEATHER_API_KEY;
exports.handler = async (event) => {
const scanParams = {
TableName: tableName,
AttributesToGet: ['city'],
};
try {
let citiesArr = await docClient.scan(scanParams).promise();
citiesArr.Items.forEach(async (cityObj) => {
// console.log('cityObj', cityObj);
// console.log('city', cityObj.city);
// console.log('weatherApiKey:', weatherApiKey);
let weatherReport = await axios({
method: 'get',
url: 'http://api.weatherapi.com/v1/forecast.json',
params: {
key: weatherApiKey,
q: cityObj.city,
days: 1,
},
});
// console.log(weatherReport);
let city = cityObj.city;
let dailyChanceOfRain = Number(
weatherReport.data.forecast.forecastday[0].day.daily_chance_of_rain
);
let totalPrecip =
weatherReport.data.forecast.forecastday[0].day.totalprecip_mm;
let lambdaParams = {
FunctionName: publishMessageLambda,
InvocationType: 'RequestResponse',
Payload: JSON.stringify({
body: { dailyChanceOfRain, totalPrecip, city },
}),
};
console.log('dailyChanceOfRain: ', dailyChanceOfRain);
console.log('totalPrecip: ', totalPrecip);
if (dailyChanceOfRain > 50 && totalPrecip > 3) {
let data = await lambda.invoke(lambdaParams).promise();
}
// console.log(
// 'weatherReport: ',
// weatherReport.data.forecast.forecastday[0].day
// );
});
} catch (error) {
console.log(error);
}
};
Running the function locally produces the desired logs on the command line however when running the function on Lambda the logs involving the external http request don't show on the cold start and the logs only show up once the function has been invoked multiple times and often not together.
Any advice would be much appreciated.

AWS - Cannot read "domainName" of undefined

[EDIT]
I am new to AWS and I've been trying to make a real-time chat application. I saw this code, so I might as well try this. I tried to follow everything but it seems there is a problem.
[END]
I got an error with this code stating, Cannot read "domainName" of undefined which got me confused. Is this because of my API Gateway? I already connected this to another route named "Message" and I already have the integration request of my connect and disconnect.
Below is my code:
const AWS = require('aws-sdk');
const db = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});
require('./index.js');
let send = undefined;
function init(event) {
console.log(event);
const apigwManagementApi = new AWS.ApiGatewayManagementApi({
apiVersion: '2018-11-29',
endpoint: event.requestContext.domainName + '/' + event.requestContext.stage
});
send = async (connectionId, data) => {
await apigwManagementApi.postToConnection({
ConnectionId: connectionId,
Data: `Echo: ${data}` }).promise();
}}
exports.handler = (event, context, callback) => {
init(event);
let message = JSON.parse(event.body).message;
getConnections().then((data) => {
console.log(data.Items);
callback(null, {
statusCode: 200,
});
data.Items.forEach(function(connection) {
console.log("Connection " +connection.connection_id);
send(connection.connection_id, message);
});
});
return {};
};
function getConnections(){
const params = {
TableName: 'GroupChat',
}
return db.scan(params).promise();
}

Why isn't Dynamodb.put working in Lambda Function? Returns null. How to debug?

Debugging lambda is frustrating.
I've got a very simple lambda function:
const AWS = require('aws-sdk')
const dynamodb = new AWS.DynamoDB.DocumentClient({region: 'ap-southeast-2'});
exports.handler = async (event, context, callback) => {
const params = {
TableName: 'people-dev',
Item: {
id: '1',
name: 'person',
email: 'person#example.com'
}
};
dynamodb.put(params, (err, data) => {
if(err) {
callback(err, null)
} else{
callback(null, data)
}
});
};
The test response is:
Response:
null
Request ID:
"3d7e9329-3843-4760-917d-4b4d4781dbd7"
Function Logs:
START RequestId: 3d7e9329-3843-4760-917d-4b4d4781dbd7 Version: $LATEST
END RequestId: 3d7e9329-3843-4760-917d-4b4d4781dbd7
REPORT RequestId: 3d7e9329-3843-4760-917d-4b4d4781dbd7 Duration: 243.13 ms Billed Duration: 300 ms Memory Size: 128 MB Max Memory Used: 29 MB
Nothing is being written to Dynamo.
Nothing useful is logged in cloudwatch.
Yes, the function has full permissions to DynamoDB.
Put async back to its place as using callbacks is outdated and much more error prone. Use the built-in promise() methods available on the node.js aws-sdk and just await on these promises. If you want to deal with errors, just surround your code with a try/catch block.
const AWS = require('aws-sdk')
const dynamodb = new AWS.DynamoDB.DocumentClient({region: 'ap-southeast-2'});
exports.handler = async (event) => {
const params = {
TableName: 'people-dev',
Item: {
id: '1',
name: 'person',
email: 'person#example.com'
}
};
await dynamodb.put(params).promise()
return {
statusCode: 200,
body: JSON.stringify({message: 'Success'})
}
};
More on async/await
The issue was that the handler was executing asynchronously.
exports.handler = async (event, context, ....
Changing it to the following, fixed the problem:
exports.handler = function (event, context, ....

How to access the aws parameter store from a lambda using node.js and aws-sdk

I've created a lambda and cloud formation template which grants a lambda access to the parameter store and secrets manager. When I test the lambda I have the following functions outside of the export.handler function:
function getParameterFromStore(param){
let promise = new Promise(function(resolve, reject){
console.log('++ ' + param.Path);
servmgr.getParametersByPath(param, function(err, data){
if(err){
reject(console.log('Error getting parameter: ' + err, err.stack));
} else {
resolve(data);
}
});
});
let parameterResult = promise.then(function(result){
console.log('---- result: '+ JSON.stringify(result));
return result;
});
return parameterResult;
};
servmgr is instantiated as var servmgr = new AWS.SSM();
When I call this function from the export.handler function I do so as:
myFirstParam = { Path : '/myPath/Service/servicesEndpoint'};
let endpointResult = getParameterFromStore(myFirstParam);
In the lambda I have the function retrieve the parameter defined outside of the export.handler function bt wrapped in a promise.
When I run/test this lambda the object returned is always undefined... I get Parameters[] back but no values.
2019-02-20T21:42:41.340Z 2684fe88-d552-4560-a477-6761f2de6717 ++ /myPath/Service/serviceEndpoint
2019-02-20T21:42:41.452Z 2684fe88-d552-4560-a477-6761f2de6717 ---- result: {"Parameters":[]}
How do you get parameter values returned back to a lambda at run time?
update
based upon the suggestion/answer from Thales I've simplified the lambda to just this:
const getParameterFromStoreAsync = (param) => {
return new Promise((resolve, reject) => {
servmgr.getParametersByPath(param, (err, data) => {
if(err){
reject(console.log('Error getting parameter: ' + err, err.stack));
}
return resolve(data);
});
});
};
exports.handler = async(event, ctx, callback) => {
console.log('INFO[lambda]: Event: [' + JSON.stringify(event, null, 2) + ']');
console.log('this is the event' + JSON.stringify(event));
sfdcEndPointParam = { Path : '/PartnerBanking/Service/SfdcEndpoint'};
let myendpoint = await getParameterFromStoreAsync(sfdcEndPointParam);
console.log('### endpoint path: ' + JSON.stringify(myendpoint));
done = ()=>{}
callback(null, done());
};
I am still seeing an empty array being returned in my tests:
### endpoint path: {"Parameters":[]}
I've also moved the function into the callback as
exports.handler = (event,ctx, callback){
done = async()=>{
console.log('this is the event' + JSON.stringify(event));
sfdcEndPointParam = { Path : '/PartnerBanking/Service/SfdcEndpoint'};
let myendpoint = await getParameterFromStoreAsync(sfdcEndPointParam);
console.log('### endpoint path: ' + JSON.stringify(myendpoint));}
}
callback(null, done());
Same result ... empty array. Any additional things to try?
This is because your getParameterFromStore returns before your then() code is executed, thus parameterResult is undefined. If you don't want to change your code too much, I would return the Promise you create, like this:
function getParameterFromStore(param){
return new Promise(function(resolve, reject){
console.log('++ ' + param.Path);
servmgr.getParametersByPath(param, function(err, data){
if(err){
reject(console.log('Error getting parameter: ' + err, err.stack));
} else {
resolve(data);
}
});
});
};
And finally, on your function's client, you can get the result like this:
const myFirstParam = { Path : '/myPath/Service/servicesEndpoint'}
getParameterFromStore(myFirstParam).then(console.log)
When coding in NodeJS, however, I highly recommend you use async/await instead, so you'll be able to escape the Promise Hell (chaninig Promise after Promise in order to achieve something "synchronously")
When using async/await, you can design your code as though it was synchronous. Here's a refactored version of your example, using async/await as well as arrow functions:
const getParameterFromStore = param => {
return new Promise((resolve, reject) => {
console.log('++ ' + param.Path);
servmgr.getParametersByPath(param, (err, data) => {
if (err) {
console.log('Error getting parameter: ' + err, err.stack)
return reject(err);
}
return resolve(data);
});
})
}
exports.handler = async (event) => {
const endpointResult = await getParameterFromStore(event.someAttributeFromTheEventThatYouWantToUse)
console.log(endpointResult)
};
EDIT: After the OP fixed the first issue, I created a working example on my own. It turned out that the way the OP was invoking the API was incorrect.
Here's the full working example:
'use strict';
const AWS = require('aws-sdk')
AWS.config.update({
region: 'us-east-1'
})
const parameterStore = new AWS.SSM()
const getParam = param => {
return new Promise((res, rej) => {
parameterStore.getParameter({
Name: param
}, (err, data) => {
if (err) {
return rej(err)
}
return res(data)
})
})
}
module.exports.get = async (event, context) => {
const param = await getParam('MyTestParameter')
console.log(param);
return {
statusCode: 200,
body: JSON.stringify(param)
};
};
Mind the Name attribute which must be provided as part of the API call to the ServiceManager.getAttribute method.
This attribute is stated in the official docs
I have run this myself and here's the output in CloudWatch Logs:
As you can see, the value was returned successfully.
Hope this helps!
If your lambda is deployed on VPC, make sure that Security Group is attached to it and outbound traffic is allowed. It will be able to access parameter store automatically.
https://aws.amazon.com/premiumsupport/knowledge-center/lambda-vpc-parameter-store/
A simpler solution would be:
const getParameterFromStore = (params) => servmgr.getParametersByPath(params).promise();
const myFirstParam = { Path : '/myPath/Service'};
getParameterFromStore(myFirstParam).then(console.log);
As you can see, the SDK itself provides utility functinality that you can use depending on your needs to use in an async or syncronious fashion.
Hope it helps.