cognito_getcreds.js - AWS Code Sample


/* Copyright, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3), which is pending release. The preview version of the SDK is available at This example is in the 'AWS SDK for JavaScript v3 Developer Guide' at Purpose: cognito_getcreds.ts generates or retrieves an Amazon Cognito ID. For more information, see Inputs (into command line): - IDENTITY_POOL_ID: Your Cognito identity pool ID - AWS_ACCOUNT_ID: Your AWS account ID - REGION Running the code: 1. Install webpack by entering 'yarn add webpack' at a command prompt. 2. Run the following command to generate 'main.js' which is used in cognito_getcreds.html. webpack cognito_getcreds.js --mode development --target web --devtool false -o main.ts 3. Open cognito_getcreds.html in your browser. */ // Load the required clients and packages const { CognitoIdentityClient, GetIdCommand } = require("@aws-sdk/client-cognito-identity"); const { fromCognitoIdentityPool, } = require("@aws-sdk/credential-provider-cognito-identity"); // Set the paramerter const IDENTITY_POOL_ID = "IDENTITY_POOL_ID"; const ACCOUNT_ID = "ACCOUNT_ID"; // Initialize the Amazon Cognito credentials provider const cognitoidentity = new CognitoIdentityClient({ region: "REGION", credentials: fromCognitoIdentityPool({ client: new CognitoIdentityClient({ region: "REGION" }), identityPoolId: IDENTITY_POOL_ID }), }); const getIdParams = { IdentityPoolId: IDENTITY_POOL_ID, AccountId: ACCOUNT_ID }; const showId = async () => { try { const data = await cognitoidentity.send(new GetIdCommand(getIdParams)); const results = "Cognito Identity ID is " + data.IdentityId; document.getElementById("results").innerHTML = results; } catch (err) { const errorResults = "Error" + err; document.getElementById("results").innerHTML = results; } }; // Make function available to browser window.showId = showId; module.exports = {showId}; //for unit tests only