AWS SDK for JavaScript
Developer Guide for SDK v2

Prepare the Browser Script

This topic is part of a larger tutorial about using the AWS SDK for JavaScript with AWS Lambda functions. To start at the beginning of the tutorial, see Tutorial: Creating and Using Lambda Functions.

In this task, you will focus on creating an Amazon Cognito identity pool used to authenticate your browser script code, and then editing the browser script accordingly.


            Preparing the browser JavaScript

Prepare an Amazon Cognito Identity Pool

The JavaScript code in the browser script needs authentication to access AWS services. Within webpages, you typically use Amazon Cognito Identity to do this authentication. First, create an Amazon Cognito identity pool.

To create and prepare an Amazon Cognito identity pool for the browser script

  1. Open the Amazon Cognito console, choose Manage Federated Identities, and then choose Create new identity pool.

  2. Enter a name for your identity pool, choose enable access to unauthenticated identities, and then choose Create Pool.

  3. Choose View Details to display details on both the authenticated and unauthenticated IAM roles created for this identity pool.

  4. In the summary for the unauthenticated role, choose View Policy Document to display the current role policy.

  5. Choose Edit to change the role policy, and then choose Ok.

  6. In the text box, edit the policy to insert this "lambda:InvokeFunction" action, so the full policy becomes the following.

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "lambda:InvokeFunction", "mobileanalytics:PutEvents", "cognito-sync:*" ], "Resource": [ "*" ] } ] }
  7. Choose Allow.

  8. Choose Sample code in the side menu. Make a note of the identity pool ID, shown in red text in the console.

    
                        Preparing an Amazon Cognito identity pool for the browser
                            script

Edit the Browser Script

Next, update the browser script to include the Amazon Cognito identity pool ID created for this application.

To prepare the browser script in the webpage

  1. Open the slotassets.zip archive file that you downloaded from the code example archive on GitHub.

  2. Open index.html in a text editor.

  3. Find this line of code in the browser script.

    AWS.config.credentials = new AWS.CognitoIdentityCredentials({IdentityPoolId: 'IDENTITY_POOL_ID'});

  4. Replace IDENTITY_POOL_ID with the identity pool ID you obtained previously.

  5. Save index.html.

Next Step

Return to the full Tutorial Steps.