AWS SDK for JavaScript

Developer Guide for SDK Version 3

AWS SDK for JavaScript

Copyright ? 2024 Amazon Web Services, Inc. and/or its affiliates. All rights reserved.

AWS SDK for JavaScript

Developer Guide for SDK Version 3

AWS SDK for JavaScript: Developer Guide for SDK Version 3

Copyright ? 2024 Amazon Web Services, Inc. and/or its affiliates. All rights reserved.

Amazon's trademarks and trade dress may not be used in connection with any product or service that is not Amazon's, in any manner that is likely to cause confusion among customers, or in any manner that disparages or discredits Amazon. All other trademarks not owned by Amazon are the property of their respective owners, who may or may not be affiliated with, connected to, or sponsored by Amazon.

AWS SDK for JavaScript

Table of Contents

Developer Guide for SDK Version 3

......................................................................................................................................................... xi What's the AWS SDK for JavaScript? ............................................................................................. 1

Get started with the SDK ........................................................................................................................... 1 Maintenance and support for SDK major versions ................................................................................ 2 Using the SDK with Node.js ....................................................................................................................... 2 Using the SDK with AWS Cloud9 .............................................................................................................. 2 Using the SDK with AWS Amplify ............................................................................................................ 2 Using the SDK with web browsers ........................................................................................................... 3

Using browsers in V3 ............................................................................................................................. 3 Common use cases ................................................................................................................................. 3 About the examples ............................................................................................................................... 4 Resources ................................................................................................................................................... 4 Get started ....................................................................................................................................... 5 SDK authentication with AWS ................................................................................................................... 5 Start an AWS access portal session .................................................................................................... 6 More authentication information ........................................................................................................ 7 Get started with Node.js ............................................................................................................................. 8 The scenario ............................................................................................................................................. 8 Prerequisites ............................................................................................................................................. 8 Step 1: Set up the package structure and installing client packages ........................................... 8 Step 2: Add necessary imports and SDK code .................................................................................. 9 Step 3: Run the example .................................................................................................................... 11 Get started in the browser ....................................................................................................................... 12 The Scenario .......................................................................................................................................... 12 Step 1: Create an Amazon Cognito identity pool and IAM role .................................................. 13 Step 2: Add a policy to the created IAM role ................................................................................. 14 Step 3: Add a Amazon S3 bucket and object ................................................................................. 14 Step 4: Set up the browser code ....................................................................................................... 15 Step 5: Run the Example .................................................................................................................... 16 Cleanup ................................................................................................................................................... 17 Set up the SDK for JavaScript ...................................................................................................... 18 Prerequisites ................................................................................................................................................ 18 Set up an AWS Node.js environment ............................................................................................... 18 Supported web browsers .................................................................................................................... 19

iii

AWS SDK for JavaScript

Developer Guide for SDK Version 3

Install the SDK ............................................................................................................................................ 20 Load the SDK .............................................................................................................................................. 21 Configure the SDK for JavaScript ................................................................................................. 22 Configuration per service ......................................................................................................................... 22

Set configuration per service ............................................................................................................. 23 Set the AWS Region .................................................................................................................................. 23

In a client class constructor ................................................................................................................ 24 Use an environment variable ............................................................................................................. 24 Use a shared config file ....................................................................................................................... 24 Order of precedence for setting the Region ................................................................................... 24 Set credentials ............................................................................................................................................ 25 Best practices for credentials ............................................................................................................. 25 Set credentials in Node.js ................................................................................................................... 26 Set credentials in a web browser ...................................................................................................... 29 Node.js considerations .............................................................................................................................. 33 Use built-in Node.js modules ............................................................................................................. 33 Use npm packages ................................................................................................................................ 33 Configure maxSockets in Node.js ...................................................................................................... 34 Reuse connections with keep-alive in Node.js ................................................................................ 35 Configure proxies for Node.js ............................................................................................................. 35 Register certificate bundles in Node.js ............................................................................................. 36 Browser Script Considerations ................................................................................................................. 37 Build the SDK for Browsers ................................................................................................................ 37 Cross-origin resource sharing (CORS) ............................................................................................... 38 Bundle with webpack ........................................................................................................................... 42 Work with AWS services ............................................................................................................... 46 Create and call service objects ................................................................................................................ 46 Specify service object parameters .................................................................................................... 47 Call services asynchronously .................................................................................................................... 47 Manage asynchronous calls ................................................................................................................ 48 Use async/await .................................................................................................................................... 49 Use promises .......................................................................................................................................... 50 Use a callback function ....................................................................................................................... 51 Create service client requests .................................................................................................................. 52 Handle service client responses .............................................................................................................. 53 Access data returned in the response .............................................................................................. 53

iv

AWS SDK for JavaScript

Developer Guide for SDK Version 3

Access error information ..................................................................................................................... 54 Work with JSON ......................................................................................................................................... 54

JSON as service object parameters ................................................................................................... 55 Code examples subset with guidance .................................................................................................... 56

JavaScript ES6/CommonJS syntax .................................................................................................... 57 Amazon DynamoDB examples ........................................................................................................... 60 AWS Elemental MediaConvert examples ......................................................................................... 84 AWS Lambda examples ..................................................................................................................... 106 Amazon Lex examples ....................................................................................................................... 106 Amazon Polly examples .................................................................................................................... 107 Amazon Redshift examples .............................................................................................................. 110 Amazon SES examples ...................................................................................................................... 118 Amazon SNS Examples ...................................................................................................................... 145 Amazon Transcribe examples ........................................................................................................... 180 Cross-service: Setting up Node.js on an Amazon EC2 instance ................................................ 191 Cross-service: App to submit data .................................................................................................. 193 Cross-service: Amazon API Gateway and Lambda ....................................................................... 201 Cross-service: Scheduled Lambda events ...................................................................................... 216 Cross-service: Amazon Lex example ............................................................................................... 228 Use AWS Cloud9 with the SDK for JavaScript ........................................................................... 243 Step 1: Set up your AWS account to use AWS Cloud9 .................................................................... 243 Step 2: Set up your AWS Cloud9 development environment ......................................................... 243 Step 3: Set up the SDK for JavaScript ................................................................................................ 244 To set up the SDK for JavaScript for Node.js ............................................................................... 244 To set up the SDK for JavaScript in the browser ......................................................................... 245 Step 4: Download example code .......................................................................................................... 245 Step 5: Run and debug example code ................................................................................................ 245 Code examples ............................................................................................................................. 247 API Gateway .............................................................................................................................................. 249 Scenarios ............................................................................................................................................... 249 Aurora ......................................................................................................................................................... 250 Scenarios ............................................................................................................................................... 249 Auto Scaling .............................................................................................................................................. 251 Actions .................................................................................................................................................. 252 Scenarios ............................................................................................................................................... 249 Amazon Bedrock ...................................................................................................................................... 294

v

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download