Issue sending message to SQS when authenticating with cognito - amazon-web-services

When attempting to send a message to sqs I get a missing config credentials warning. If switch to just displaying my accesskey and password I can send the message to sqs just fine. I've included the code I'm using and the errors I get from the browser below.
Code below:
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'xxxxxx',
});
var params = {
MessageBody: 'some random message',
QueueUrl: 'xxxxxx'
};
AWS.config.credentials.get(function(err) {
if (err) console.log(err);
else console.log(AWS.config.credentials);
});
var sqs = new AWS.SQS();
sqs.sendMessage(params, function (err, data) {
if (!err) {
console.log('Message sent.');
} else {
console.log(err);
}
});
Errors from console.log:
Error: Missing region in config
at null. (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:5:10470)
at r.SequentialExecutor.r.util.inherit.callListeners (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:6:27635)
at r.SequentialExecutor.r.util.inherit.emit (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:6:27431)
at n.Request.o.emitEvent (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:6:15837)
at e (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:6:12148)
at r.runTo (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:7:23197)
at n.Request.o.runTo (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:6:13657)
at n.Request.o.send (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:6:13550)
at t.Service.i.makeUnauthenticatedRequest (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:6:30572)
at t.util.update.getId (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:7:2224)
index.html:57 Error: Missing credentials in config
at null. (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:5:10470)
at r.SequentialExecutor.r.util.inherit.callListeners (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:6:27635)
at r.SequentialExecutor.r.util.inherit.emit (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:6:27431)
at n.Request.o.emitEvent (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:6:15837)
at e (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:6:12148)
at r.runTo (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:7:23197)
at n.Request.o.runTo (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:6:13657)
at n.Request.o.send (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:6:13550)
at t.Service.i.makeUnauthenticatedRequest (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:6:30572)
at t.util.update.getId (https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js:7:2224)
Take note that I've tried adding region various different ways.

I don't see anything in your posted code that sets the region.
[Following copied from the Configuring the SDK in Node.js documentation]
The AWS SDK for Node.js doesn't select the region by default. You can choose a region similarly to setting credentials by either loading from disk or using AWS.config.update():
AWS.config.update({region: 'us-west-1'});

Related

Getting 'The security token included in the request is invalid.' when trying to get a pre signed url for s3

Basically I'm trying to get a pre-signed URL for the putObject method in S3. I send this link back to my frontend, which then uses it to upload the file directly from the client.
Here's my code :
const AWS = require("aws-sdk");
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_IAM_ACCESS,
secretAccessKey: process.env.AWS_IAM_SECRET,
region: 'ap-southeast-1',
});
const getPreSignedUrlS3 = async (data) => {
try {
//DO SOMETHING HERE TO GENERATE KEY
const options = {
Bucket: process.env.AWS_USER_CDN,
ContentType: data.type,
Key: key,
Expires: 5 * 60
};
return new Promise((resolve, reject) => {
s3.getSignedUrl(
"putObject", options,
(err, url) => {
if (err) {
reject(err);
}
else resolve({ url, key });
}
);
});
} catch (err) {
console.log(err)
return {
status: 500,
msg: 'Failed to sync with CDN, Please try again later!',
}
}
}
I'm getting the following error from the aws sdk : The security token included in the request is invalid.
Things I have tried :
Double check the permissions from my IAM user. Even made bucket access public for testing. My IAM user is given full s3 access policy.
Tried using my root user security key and access details. Still got the same error.
Regenerated new security credentials for my IAM user. I don't have any MFA turned on.
I'm following this documentation.
SDK Version : 2.756.0
I've been stuck on this for a while now. Any help is appreciated. Thank you.
Pre-signed URLs are created locally in the SDK so there's no need to use the asynchronous calls.
Instead, use a synchronous call to simplify your code, something like this:
const getPreSignedUrlS3 = (Bucket, Key, ContentType, Expires = 5 * 60) => {
const params = {
Bucket,
ContentType,
Key,
Expires
};
return s3.getSignedUrl("putObject", params);
}

AWS Connect API CORS issue

I am new to AWS and been struggling to find a solution for my problem. I am trying to create a custom chat widget for our website using Amazon Connect and I am using this API to start a chat contact.
I am following this article to create a custom chat widget for our website.
The problem I am facing is the CORS error when I try to start a chat session. I am following this documentation from AWS.
This is what I have so far
<script type="text/javascript">
AWS.config.credentials = new AWS.Credentials({
accessKeyId: "keyId",
secretAccessKey: "secret"
});
AWS.config.region = "us-east-1";
var connect = new AWS.Connect({
apiVersion: '2017-08-08',
region: 'us-east-1',
});
new AWS.Connect(options = {});
var params = {
ContactFlowId: '67324a76-5b54-48a3c1-a0da-c64e26700af9', /* required */
InstanceId: '54b0f2c2-dd64-4ce3a4-973f3f-c3fea84f12c1E', /* required */
ParticipantDetails: { /* required */
DisplayName: 'test_customer' /* required */
},
ClientToken: '',
InitialMessage: {
Content: 'test', /* required */
ContentType: 'string' /* required */
}
};
connect.startChatContact(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
</script>
and this is the error that I get
I really don't have any idea if what I am doing is correct. Please help.
Try going to the AWS Connect in AWS (The actual AWS console not the Connect console).
The click on "Application Integrations" and then "+add origin" and add the url to your custom chat widget.

AWS - adminGetUser returns unexpected error

I have a problem when trying to connect to my AWS cognito database.
Here is my code:
AWS.config.region = 'eu-west-2'; // Region
AWS.config.accessKeyId = 'XXXXXXXXXX';
AWS.config.secretAccessKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'eu-west-2:XXXX-XXXXXX-XXXXXXXXXXXXX',
});
var params = {
UserPoolId: 'eu-west-2_xxxxxxxxxx',
Username: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
};
var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
cognitoidentityserviceprovider.adminGetUser(params, function(err, data) {
if (err) console.log(err); // an error occurred
else console.log(data); // successful response
})
Here is a breakdown -
At first I am configure the AWS.config with the region, accessKeyID, secretAccessKey and the credentials with AWS.CognitoIdentityCredentials().
I then simply set a new AWS.CognitoIdentityServiceProvider() variable and call the adminGetUSer function with the UserPoolID and Username parameters (the Username is the username of the user I want to get information of)
However, I am getting a very weird error when deploying it:
"Cannot read property 'byteLength' of undefined..
When looking at the http request:
And the response is a little weird looking:
Thank you for your time reading this! Hope someone would be able to help me understand what is the problem with my code.
Thank you!
can you please post the statement that the stack trace originates from?
Also, are you using the 'aws-sdk' version provided by aws-amplify?
The response definitely looks incorrect and does not match the Response specified by the AdminGetUser API. I think you're looking at the wrong entry in your network tab.

read data from AWS S3 bucket using JavaScript

i'm trying to read data from AWS S3 Bucket using JavaScript but getting
error :
"Error: Missing credentials in config"
AWS.config.update({
"region": "eu-west-1"
});
var params = { Bucket: <BucketName>, Key: "data.json" };
new AWS.S3().getObject(params, function (err, json_data) {
if (!err) {
var json = JSON.parse(new Buffer(json_data.Body).toString("utf8"));
console.log(json);
}
else
console.log(err);
});
even if i tried without AWS.config.update i'm getting this error.
any idea?
AFAIK if you wish to access a bucket which is not public then you will need to supply your AWS credentials along with the request. Here's the SDK page for building an AWS.credentials object that you put into your options when making the AWS.S3 object.
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Credentials.html
No example because I am not a JS dev and can't write it out of memory, sorry!

Is there a way to create buckets and set CORS only using Javascript(in browser)?

From here I know that I can set bucket CORS via REST API, but these API calls themselves would meet cross-domain problems if sent from browser. So I wonder if there's a method to manage s3 buckets using javascript only(in browser, without a proxy server)?
Tried and affirmed, test code and result picture below:
AWS.config.region = 'us-east-1';
AWS.config.accessKeyId = 'YTBGSLU96NCNWPKEY11E';
AWS.config.secretAccessKey = 'XXXXXXXXXXXXXXXXXXX'; // not that silly to tell you
var s3 = new AWS.S3();
s3.createBucket({
Bucket: 'test',
ACL: 'public-read-write'
}, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
})