Aws-sdk-js: Import CognitoIdentityCredentials without loading all of AWS

Created on 7 Nov 2016  路  10Comments  路  Source: aws/aws-sdk-js

Hello,

I granularly import the pieces of the sdk I need, to keep files sizes down. For example:

import Config from 'aws-sdk/clients/configservice';

I would like to do the same for AWS.CognitoIdentityCredentials, but I have only been able to do it by loading the entire sdk:

import AWS from 'aws-sdk';

This ballons the build from 3mb to 8.5mb.

Is there a way to surgically load AWS.CognitoIdentityCredentials?

guidance

Most helpful comment

@thinkloop
AWS.ConfigService is different from AWS.Config. The former is a service you'd need to import explicitly, the latter controls the SDK configuration settings. In your example, it looks like you're trying to update the SDK's credentials, so you can do something like this:

import AWS from 'aws-sdk/global';

AWS.config.credentials = new AWS.CognitoIdentityCredentials({
  /** params **/
});

All 10 comments

@thinkloop
There isn't a good way of requiring just the AWS.CognitoIdentityCredentials. You can import aws-sdk/global to give you the AWS namespace with all the core classes (including credentials) loaded. Services aren't added to AWS when importing from aws-sdk/global unless you explicitly import them elsewhere.

This should result in a smaller build, since you won't be importing every single service.

Thanks! That worked. Do I still need the separate Config or does it also come with global:

import Config from 'aws-sdk/clients/configservice';
import AWS from 'aws-sdk/global';

Config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: 'us-east-1_xxxx',
    Logins : {
        'cognito-idp.us-east-1.amazonaws.com/us-east-xxxx': result.getIdToken().getJwtToken()
    },
    Paranoia : 7
});

@thinkloop
AWS.ConfigService is different from AWS.Config. The former is a service you'd need to import explicitly, the latter controls the SDK configuration settings. In your example, it looks like you're trying to update the SDK's credentials, so you can do something like this:

import AWS from 'aws-sdk/global';

AWS.config.credentials = new AWS.CognitoIdentityCredentials({
  /** params **/
});

+1, thanks again!

Sorry I'm using Angular typescript, I have an error that aws-sdk/global doesn't has a default export. How can I config global credential after retrieved it from CognitoIdentityCredentials ?

@sandangel
in typescript use * as
import * as AWS from "aws-sdk/global";

I found a more efficient way.

import {config as AWSConfig} from 'aws-sdk/global';

its still 17k lines and 1.4MB. Is there no way to expose this as a module so it can be used effectively in the front end? At the moment is still only realistic for backend.

EDIT:

Just read:

By default this provider gets credentials using the AWS.CognitoIdentity.getCredentialsForIdentity()

So looks like you may be able to implement what you need just using this service.

I am running into this issue today as well, is there a work around for a small build? I do not see CognitoIdentityCredentials in clients, only cognitoidentity and cognitoidentityserviceprovider.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

Was this page helpful?
0 / 5 - 0 ratings