AWS.SimpleDB: How to use API to do putAttributes() - amazon-web-services

I'm using Amazon Simple DB (http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SimpleDB.html)
and I'm trying to use putAttributes by writing the following node.js code:
AWS.config = new AWS.Config({accessKeyId: '***', secretAccessKey: '***', region: '***'});
var simpledb = new AWS.SimpleDB();
var params = {
Attributes: [
{
Name: 'firstname',
Value: 'David'
},
],
DomainName: '***', /* required */
ItemName: 'per2', /* required */
};
simpledb.putAttributes(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
Unfortunately, I get the following error in console after reading the line:
var simpledb = new AWS.SimpleDB();
try.html?_ijt=no554cohakgtacn421reh33mpr:22 Uncaught TypeError:
AWS.SimpleDB is not a constructor
Do you have any idea what is the problem?

Related

aws identity pools: What are the differences between these two approaches?

I have recently started to work with the AWS SDK and how a user can authenticate with Cognito in an identity pool.
When querying the credentials I have the following question:
To what extent do the following approaches differ?
approach: getId() and getCredentialsForIdentity():
const cognitoidentity = new AWS.CognitoIdentity()
var params = {
IdentityPoolId: 'STRING_VALUE', /* required */
AccountId: 'STRING_VALUE',
Logins: {
'<IdentityProviderName>': 'STRING_VALUE',
/* '<IdentityProviderName>': ... */
}
};
cognitoidentity.getId(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
var params = {
IdentityId: 'STRING_VALUE', /* required */
CustomRoleArn: 'STRING_VALUE',
Logins: {
'<IdentityProviderName>': 'STRING_VALUE',
/* '<IdentityProviderName>': ... */
}
};
cognitoidentity.getCredentialsForIdentity(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // data AccesKeyId,Expiration, SecretKey, SessionToken
});
approach:
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: identityPoolId,
Logins:{
'Provider': jwtToken
},
region: "eu-central-1"
});
AWS.config.credentials.get(function(){
// Credentials will be available when this function is called.
const{ accessKeyId} = AWS.config.credentials
const {secretAccessKey} = AWS.config.credentials
const {sessionToken} = AWS.config.credentials
})
the credentials of the two attempts are different, the SessionToken is the same. What is the difference between the SecretKey obtained in the first attempt and the SecretAccesKey obtained in the second attempt?
What are the differences between the two attempts?
The first attempt : enhanced simpliefied authflow with GetId and GetCredentialsForIdentity
https://docs.aws.amazon.com/cognito/latest/developerguide/authentication-flow.html
second attempt:
https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-browser-credentials-cognito.html
I am grateful for any help :)
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityCredentials.html
According to this it is doing the same thing in the background.The difference in syntax for SecretKey and SecretAccesKey appears to be purely cosmetic. I have noticed the same in boto3. Regardless, you can pass either with accesskey and sessionid to authenticate other AWS services.

"Converting circular structure to JSON" while reading data from MYSQL and putting it in DynamoDB

I am reading data from read replica of Aurora and putting it into the DynamoDB. I getting data from RDS correctly but facing problems while inserting it into DynamoDB. I am sharing my code and error message please guide me....
var AWS = require("aws-sdk");
var mysql = require('mysql');
const client = new AWS.DynamoDB.DocumentClient({region : 'eu-west-1'});
var connection = mysql.createPool({
host : "**********",
user : "****",
password : "*****",
database : "mydb",
port : "3306"
});
exports.handler = (event, context, callback) => {
connection.query('select * from demo where id=2;', function (error, results, fields) {
if (error) {
if (error) throw error;
} else {
var data = {results};
var params = {
Item:{
test_id:data.id,
name:data.name,
result:data.result
},
TableName: 'demo_rds_dynamoDB'
};
connection.end(function (err) {
callback(err, client.put(params, function(error,data){
if(error){
callback(error,null);
}else{
callback(null,data);
}
})
);
});
}
});
};
Following is the data I am getting from Aurora and I want to put in DynamoDB
Response:
[
{
"id": 2,
"name": "Display",
"result": "Pass"
}
]
And the response m getting is as follow and sometimes i get error as Cannot read property "id" undefined,
Response:
{
"errorMessage": "Converting circular structure to JSON",
"errorType": "TypeError",
"stackTrace": []
}
Request ID:
"896a64e3-573e-11e8-bb94-67c4be71eb3d"
Function Logs:
START RequestId: 896a64e3-573e-11e8-bb94-67c4be71eb3d Version: $LATEST
Unable to stringify response body as json: Converting circular structure to JSON: TypeError
at Object.stringify (native)
Item:{
test_id:results[0].id,
name:results[0].name,
result:results[0].result
}
results[0].columnname

AWS getting all Users from aws cognitio with JS

how can I get all Users from aws cognito?
I found this: list Users
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#listUsers-property
var cognito = new AWSCognito.CognitoIdentityServiceProvider;
cognito.listUsers(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
But when I do this I got this error:
No reagon defined ...
also tried this:
AWS.config.region = 'eu-central-1'; // Region
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'eu-central-1:',
Logins: {
'cognito-idp.eu-central-1.amazonaws.com/eu-central-1_': result.getIdToken().getJwtToken()
}
});
here I got result is not defined ...
how can I fetch all Users?
This is an working example:
let params = {
UserPoolId: '<<PoolID>>',
AttributesToGet: [
'email',
'family_name',
'given_name',
'custom:client',
'phone_number'
],
Limit: 0,
};
this.awsSDKAuth().listUsers(params, (err, data) => {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(data.Users);
}
});

Parse Server - Missing Push Configuration?

I've been trying to get push notifications to work with my Parse application. I tried to do so my adding the below code into my Parse server.js file but my server does not start up again when this code is contained inside of the file. I have my p12 file available and linked in the code below (on my actual sever) as well so Im not sure what the issue is.
push: {
android: {
senderId: '...',
apiKey: '...'
},
ios: {
pfx: '/file/path/to/XXX.p12',
passphrase: '', // optional password to your p12/PFX
bundleId: '',
production: false
}
}
My server is running on an Amazon EC2 instance as well.
Are you using AWS SNS to use push notification? If so, you can try setting this in your server code:
function sendPhoneNotification() {
AWS = require('aws-sdk');
AWS.config.update({
accessKeyId: '***',
secretAccessKey: '***',
region: 'ap-southeast-1'
});
var sns = new AWS.SNS();
var promise = new Parse.Promise();
sns.createPlatformEndpoint({
PlatformApplicationArn: '***',
Token: "***"
}, function (err, data) {
if (err) {
console.log("Error in endpoint" + err.stack);
//res.error("error stack 1: " + err.stack);
promise.reject(err.stack);
return promise;
}
var endpointArn = data.EndpointArn;
var payload = {
GCM: {
data: {
title: "YOUR TITLE",
message: "HELLO PUSH NOTIFICATION"
}
}
/* APNS: {
aps: {
alert: 'Hello World',
sound: 'default',
badge: 1
}
}*/
};
// payload.APNS = JSON.stringify(payload.APNS);
payload.GCM = JSON.stringify(payload.GCM);
payload = JSON.stringify(payload);
var result = sns.publish({
Message: payload,
MessageStructure: 'json',
TargetArn: endpointArn
}, function (err, data) {
if (err) {
promise.reject(err.stack);
return promise;
}
res.success("push sent " + JSON.stringify(data));
});
});
return promise;
}

AWS.DynamoDB: KeyConditionExpression is missing

I wanted to make a search in DynamoDB equivalent to the following SQL query:
SELECT *
from db
where attr="abc"
Therefore, I wrote the following code:
function searchFile(carModel, busMapping, from_date, until_date, location, attackTraffic, isTagged) {
AWS.config = new AWS.Config({accessKeyId: '***', secretAccessKey: '***', region: 'us-west-2'});
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
var params = {
"TableName" : 'db',
QueryFilter: {
"attr": {
ComparisonOperator: 'EQ',
AttributeValueList: [
{S: 'abc'}
]
}
}
}
dynamodb.query(params, function(err,data) {
if (err) {
alert(err);
}
})
}
I got the error message:
"Expected params.KeyConditionExpression to be a string"
I looked at the AWS documentation (http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#query-property) and saw that params.KeyConditionExpression must be defined. However, I still didn't understand what is expected to be inside this key. Can you propose something that can make my query work?
To perform a query against DynamoDB you must provide it with the primary/hash/partition key for the table.
KeyConditionExpression: The condition must perform an equality test on a single partition key value. The partition key equality test is required, and must be specified in the following format: partitionKeyName = :partitionkeyval
What you probably want is to use the scan method and provide it with a ScanFilter instead of a QueryFilter:
function searchFile(carModel, busMapping, from_date, until_date, location, attackTraffic, isTagged) {
AWS.config = new AWS.Config({accessKeyId: '***', secretAccessKey: '***', region: 'us-west-2'});
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
var params = {
TableName : 'db',
ScanFilter: {
attr: {
ComparisonOperator: 'EQ',
AttributeValueList: [
{S: 'abc'}
]
}
}
}
dynamodb.scan(params, function(err,data) {
if (err) {
alert(err);
}
})
}