aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.43k stars 2.13k forks source link

UnhandledPromiseRejectionWarning: Error: No credentials, applicationId or region #7934

Closed sky4git closed 3 years ago

sky4git commented 3 years ago

Before opening, please confirm:

Is this a production issue?

JavaScript Framework

React

Amplify APIs

Authentication, Analytics, REST API, GraphQL API

Amplify Categories

auth, function, api, analytics

Environment information

System:
    OS: Windows 10 10.0.19041
    CPU: (8) x64 Intel(R) Core(TM) i5-1035G7 CPU @ 1.20GHz
    Memory: 787.99 MB / 7.60 GB
  Binaries:
    Node: 14.15.4 - C:\Program Files\nodejs\node.EXE
    npm: 7.5.6 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 88.0.4324.190
    Edge: Spartan (44.19041.423.0), Chromium (88.0.705.50)
    Internet Explorer: 11.0.19041.1
  npmPackages:
    @aws-amplify/ui-react: ^0.2.34 => 0.2.38
    @babel/cli: ^7.12.10 => 7.13.10
    @babel/core: ^7.12.10 => 7.13.10
    @babel/plugin-transform-typescript: ^7.12.1 => 7.13.0
    @babel/preset-env: ^7.12.11 => 7.13.10
    @fortawesome/fontawesome-svg-core: ^1.2.32 => 1.2.34
    @fortawesome/free-brands-svg-icons: ^5.15.1 => 5.15.2
    @fortawesome/free-solid-svg-icons: ^5.15.1 => 5.15.2
    @fortawesome/react-fontawesome: ^0.1.14 => 0.1.14
    @material-ui/core: ^4.11.2 => 4.11.3
    @material-ui/icons: ^4.11.2 => 4.11.2
    @material-ui/lab: ^4.0.0-alpha.57 => 4.0.0-alpha.57
    @material-ui/styles: ^4.11.2 => 4.11.3
    @types/node: ^14.14.20 => 14.14.34
    @types/react: ^16.14.2 => 16.14.5
    aws-amplify: ^3.3.14 => 3.3.24
    aws-sdk: ^2.824.0 => 2.863.0
    axios: ^0.21.1 => 0.21.1
    braintree-web: ^3.70.0 => 3.74.0
    cross-fetch: ^3.0.6 => 3.1.0
    eslint-plugin-react-hooks: ^4.2.0 => 4.2.0
    graphql: ^15.4.0 => 15.5.0
    graphql-tag: ^2.11.0 => 2.11.0
    nanoid: ^3.1.20 => 3.1.21
    next: ^10.0.5 => 10.0.8
    qrcode.react: ^1.0.1 => 1.0.1
    react: 16.13.1 => 16.13.1
    react-dom: 16.13.1 => 16.13.1
    react-google-recaptcha: ^2.1.0 => 2.1.0
    sass: ^1.32.2 => 1.32.8
    typescript: ^3.9.7 => 3.9.9
    zen-observable: ^0.8.15 => 0.8.15
  npmGlobalPackages:
    @aws-amplify/cli: 4.45.2
    amplify-cli: 1.0.0
    aws-cdk: 1.88.0
    npm-check-updates: 9.1.0
    npm: 7.5.6

Describe the bug

Suddenly started appearing in dev environment and subsequently in production environment.

Expected behavior

Not sure why this error is coming in first place. I am using Analytics Pinpoint and recording various event through out the app. The bug description doesn't help to understand where and what is going wrong.

Reproduction steps

Its happening on home and all pages of my app. so just visit localhost:3000 to see the error.

Code Snippet

// Put your code below this line.

Log output

Uncaught (in promise) Error: No credentials, applicationId or region
    at AWSPinpointProvider.<anonymous> (_app.js:1101)
    at step (_app.js:980)
    at Object.next (_app.js:961)
    at fulfilled (_app.js:952)

aws-exports.js

{
    "aws_project_region": "ap-southeast-2",
    "aws_cognito_identity_pool_id": "ap-southeast-2:XXXXXXX",
    "aws_cognito_region": "ap-southeast-2",
    "aws_user_pools_id": "ap-southeast-2_XXXXX",
    "aws_user_pools_web_client_id": "XXXXXX",
    "oauth": {
        "domain": "XXXXX.auth.ap-southeast-2.amazoncognito.com",
        "scope": [
            "phone",
            "email",
            "openid",
            "profile",
            "aws.cognito.signin.user.admin"
        ],
        "redirectSignIn": "https://example.com/",
        "redirectSignOut": "https://example.com/",
        "responseType": "code"
    },
    "federationTarget": "COGNITO_USER_AND_IDENTITY_POOLS",
    "aws_mobile_analytics_app_id": "XXXXXXXXX",
    "aws_mobile_analytics_app_region": "us-west-2",
    "aws_appsync_graphqlEndpoint": "https://xxxxxxxxxxx.appsync-api.ap-southeast-2.amazonaws.com/graphql",
    "aws_appsync_region": "ap-southeast-2",
    "aws_appsync_authenticationType": "AMAZON_COGNITO_USER_POOLS",
    "aws_cloud_logic_custom": [
        {
            "name": "ExampleAPI",
            "endpoint": "https://xxxxxxxxx.execute-api.ap-southeast-2.amazonaws.com/local",
            "region": "ap-southeast-2"
        }
    ]
}

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

pyrossh commented 3 years ago

Facing the same issue here.

pyrossh commented 3 years ago

I'm not getting this anymore it seems that I needed to configure the IdentityPool with an UnauthRole and it works now.

eryon commented 3 years ago

I was getting this issue too. Amplify Analytics had previously been working but has suddenly stopped. The identity pool has authenticated and unauthenticated roles with analytics permissions.

I had originally been using these deps, but after upgrading, it appears to be working again:

diff --git a/package.json b/package.json
index adbc753d..3b296a65 100644
--- a/package.json
+++ b/package.json
@@ -13,15 +13,15 @@
   "dependencies": {
-    "@aws-amplify/analytics": "^3.2.7",
-    "@aws-amplify/api": "^3.2.16",
-    "@aws-amplify/auth": "^3.4.16",
-    "@aws-amplify/core": "^3.8.8",
-    "@aws-amplify/storage": "^3.3.16",
-    "@aws-sdk/client-s3": "3.0.0",
-    "@aws-sdk/s3-request-presigner": "3.0.0",
-    "@aws-sdk/util-create-request": "3.0.0",
-    "@aws-sdk/util-format-url": "3.0.0",
+    "@aws-amplify/analytics": "^4.0.16",
+    "@aws-amplify/api": "^3.2.28",
+    "@aws-amplify/auth": "^3.4.28",
+    "@aws-amplify/core": "^3.8.20",
+    "@aws-amplify/storage": "^3.3.28",
+    "@aws-sdk/client-s3": "3.3.0",
+    "@aws-sdk/s3-request-presigner": "3.3.0",
+    "@aws-sdk/util-create-request": "3.6.1",
+    "@aws-sdk/util-format-url": "3.6.1",

Possibly related to #7951 ?

sky4git commented 3 years ago

I'm not getting this anymore it seems that I needed to configure the IdentityPool with an UnauthRole and it works now.

Can you show me howz your permissions in iam role look like? Thank you.

pyrossh commented 3 years ago

Yep you need to upgrade also `"@aws-amplify/analytics": "^4.0.16". These are my roles,

  IdentityPool:
    Type: AWS::Cognito::IdentityPool
    Properties:
      IdentityPoolName: !Ref AWS::StackName
      AllowUnauthenticatedIdentities: true
      CognitoIdentityProviders:
        - ProviderName: !Sub cognito-idp.${AWS::Region}.amazonaws.com/${UserPool}
          ClientId: !Ref UserPoolClient
          ServerSideTokenCheck: true

  AuthenticatedRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Deny
            Principal:
              Service:
                - cognito-identity.amazonaws.com
            Action:
              - sts:AssumeRoleWithWebIdentity
      Policies:
        - PolicyName: !Sub ${AWS::StackName}-authenticated-role
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - mobiletargeting:PutItems
                  - mobiletargeting:UpdateEndpoint
                  - mobiletargeting:PutEvents
                Resource: !Sub
                  - arn:aws:mobiletargeting:*:${AWS::AccountId}:apps/${ApplicationId}*
                  - ApplicationId: !Ref PinPointApp
              - Effect: Allow
                Action:
                  - mobileanalytics:*
                  - cognito-sync:*
                  - cognito-identity:*
                Resource: '*'

  UnauthenticatedRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Deny
            Principal:
              Service:
                - cognito-identity.amazonaws.com
            Action:
              - sts:AssumeRoleWithWebIdentity
      Policies:
        - PolicyName: !Sub ${AWS::StackName}-authenticated-role
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - mobiletargeting:PutItems
                  - mobiletargeting:UpdateEndpoint
                  - mobiletargeting:PutEvents
                Resource: !Sub
                  - arn:aws:mobiletargeting:*:${AWS::AccountId}:apps/${ApplicationId}*
                  - ApplicationId: !Ref PinPointApp
              - Effect: Allow
                Action:
                  - mobileanalytics:*
                  - cognito-sync:*
                  - cognito-identity:*
                Resource: '*'

  IdentityPoolAuthRole:
    Type: AWS::Cognito::IdentityPoolRoleAttachment
    Properties:
      IdentityPoolId: !Ref IdentityPool
      Roles:
        authenticated: !GetAtt AuthenticatedRole.Arn
        unauthenticated: !GetAtt UnAuthenticatedRole.Arn
sky4git commented 3 years ago

@pyros2097 I have almost same permissions, except sts:AssumeRoleWithWebIdentity is allowed.

However, in my case, I think its the issue is on front-end library. Especially with AWSPinpointProvider.js. I have never changed the permissions on unauth or auth role, but the error started appearing after npm update.

I have tried reverting the changes but then the error didnt went away which is really problematic.

sky4git commented 3 years ago

Solution

In my case, in package.json, I had both included @aws-amplify/ui-react: ^0.2.34 aws-amplify: ^3.3.14

However, it is not required. I removed @aws-amplify/ui-react from package.json and ran npm update.

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.