Skip to content

How do I setup the credentials for the SDK? #34

@jonsmirl

Description

@jonsmirl

I put together a test function for lambda, but the credentials are not set into the SDK.
-- Error: Missing credentials in config
Am I using the same copy of the SDK as amplify?
Had amplify set the credentials into the SDK?
I also noted that AWS.config.region is not set either.

Previously I did setup like this:
AWS.config.update({region: 'us-east-1'});
AWS.config.credentials = new AWS.Credentials({
accessKeyId: sessionStorage.getItem('accessKeyId'),
secretAccessKey: sessionStorage.getItem('secretAccessKey'),
sessionToken: sessionStorage.getItem('sessionToken'),
});
But I had expected aws-amplify to set this up for me.

import React, { Component } from 'react';
import AWS from 'aws-sdk'

var Lambda = new AWS.Lambda({region: 'us-east-1', apiVersion: '2015-03-31'});

export default class Page2 extends Component {

  build_table () {
    // create JSON object for parameters for invoking Lambda function
    var pullParams = {
      FunctionName : 'digi-dev-getEvents',
      InvocationType : 'RequestResponse',
      LogType : 'None'
    };
    // create variable to hold data returned by the Lambda function
    var events;

    Lambda.invoke(pullParams, function(error, data) {
      if (error) {
        prompt(error);
      } else {
        events = JSON.parse(data.Payload);
        var t = "<table border=1 padding=3>";
        events.forEach(function(r) {
          t += "<tr>";
          var x = new Date( r["timestamp"] );
          var formatted = x.toLocaleString();
          t += "<td>" + formatted + "</td>";
          t += "<td>" + r["thing"] + "</td>";
          t += "<td>" + r["type"] + "</td>";
          t += "</tr>";
        });
        t += "</table>";
        console.log(t);
      }
    });
  }

Do I need to do something like this?

_ensureCredentials() {
    if (this._options.credentials) { return Promise.resolve(true); }

    return Auth.currentCredentials()
        .then(credentials => {
            const cred = Auth.essentialCredentials(credentials);
            logger.debug('set credentials for storage', cred);
            this._options.credentials = cred;

            return true;
        })
        .catch(err => {
            logger.warn('ensure credentials error', err)
            return false;
        });
}

I just expected amplify to supply me with a global AWS handle that always has valid credentials in it (ie refresh is taken care of).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions