Running other AWS services from the app - issue with credentials


#1

Hi jayair, all,
This is great tutorial and I learned a lot from it - thank you!

I’m trying to run other AWS services as a logged user from the web app - in this example Polly:

texttoSpeech() {

AWS.config.region = ‘us-east-1’; // Region
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: config.cognito.IDENTITY_POOL_ID,
});

var polly = new AWS.Polly();

var params = {
OutputFormat: “mp3”,
SampleRate: “8000”,
Text: “All Gaul is divided into three parts”,
TextType: “text”,
VoiceId: “Joanna”
};

polly.synthesizeSpeech(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
}

This code is not able to run the service due to missing credentials, below is the fragment of error output:

message: “Missing credentials in config”
​name: “CredentialsError”
​originalError: Object { message: “Could not load credentials from CognitoIdentityCredentials”, code: “CredentialsError”, requestId: “244cc2be-6cde-11e8-98bc-cb953548fd85”, … }

Any idea what I’m doing wrong?


#2

Hmmm is this code running on the frontend or backend?


#3

Frontend, I added new page as other pages in the tutorial.


#4

It looks like all AWS settings are not inherited - when I remove line with AWS region, the error says:
"Missing region in config"


#5

If you are using a different AWS service on the frontend then you need to pass in the authentication info like this example here:

import Route53 from 'aws-sdk/clients/route53';

Auth.currentCredentials()
  .then(credentials => {
    const route53 = new Route53({
      apiVersion: '2013-04-01',
      credentials: Auth.essentialCredentials(credentials)
    });

    // more code working with route53 object
    // route53.changeResourceRecordSets();
  })

https://aws.github.io/aws-amplify/media/authentication_guide#working-with-aws-service-objects


#6

Hey jayair!
This is working great. Thanks a lot!
I hope it will be helpful for others as it gives opportunity to use the app to work with other AWS services.


#7

Great! Yeah thanks for reporting back.


#8

I’m experiencing a weird issue and I’m hoping someone could help me. Within the try …catch block on Home.js (following the example from the tutorial), below const jobs = await this.jobs(); I added a call for an additional service, and it’s wrapped in the same Auth.currentCredentials() method - but when I land on the homepage with the Job lists, I get the ‘Error: Missing region in config’ BUT when I click on one of the jobs, and then return back to the homepage, this time the request goes through successfully and I receive a proper response.

What could be causing this issue? I’ve been banging my head for a couple of days now. Any help would be appreciated. Thanks.


#9

Hmm can you post some of the code maybe?