AWS SDK for JavaScript

AWS SDK for JavaScript

Developer Guide for SDK Version 3

AWS SDK for JavaScript Developer Guide for SDK Version 3

AWS SDK for JavaScript: Developer Guide for SDK Version 3

Copyright ? 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 Developer Guide for SDK Version 3

Table of Contents

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

Maintenance and support for SDK major versions .......................................................................... 1 What's new in Version 3 ............................................................................................................. 1

Modularized packages ......................................................................................................... 1 New middleware stack ........................................................................................................ 5 Using the SDK with Node.js ......................................................................................................... 5 Using the SDK with AWS Cloud9 .................................................................................................. 5 Using the SDK with AWS Amplify ................................................................................................. 5 Using the SDK with web browsers ................................................................................................ 6 Using browsers in V3 .......................................................................................................... 6 Common use cases ............................................................................................................. 6 About the examples ........................................................................................................... 7 Resources .......................................................................................................................... 7 Getting started .................................................................................................................................. 8 Getting started in a browser script ............................................................................................... 8 The Scenario ..................................................................................................................... 8 Step 1: Create an Amazon Cognito Identity Pool .................................................................... 9 Step 2: Add a Policy to the Created IAM Role ...................................................................... 10 Step 3: Create a project environment .................................................................................. 10 Step 4: Create the HTML Page ........................................................................................... 10 Step 5: Write the JavaScript .............................................................................................. 11 Step 6: Run the Example ................................................................................................... 12 Possible Enhancements ..................................................................................................... 12 Getting started in Node.js ......................................................................................................... 12 The scenario .................................................................................................................... 13 Prerequisite tasks ............................................................................................................. 13 Step 1: Install the Amazon S3 package and dependencies ...................................................... 13 Step 2: Write the Node.js code ........................................................................................... 14 Step 3: Run the example ................................................................................................... 15 Getting started in React Native .................................................................................................. 15 The Scenario .................................................................................................................... 15 Setup for this tutorial ....................................................................................................... 16 Step 1: Create an Amazon Cognito Identity Pool .................................................................. 17 Step 2: Add a Policy to the Created IAM Role ...................................................................... 17 Step 3: Create app using create-react-native-app ................................................................. 18 Step 4: Install the Amazon S3 package and other dependencies ............................................. 18 Step 5: Write the React Native code ................................................................................... 18 Step 6: Run the Example ................................................................................................... 20 Possible Enhancements ..................................................................................................... 21 Setting up the SDK for JavaScript ...................................................................................................... 23 Prerequisites ............................................................................................................................ 23 Setting up an AWS Node.js environment ............................................................................. 23 Supported web browsers ................................................................................................... 23 Installing the SDK .................................................................................................................... 24 Loading the SDK ...................................................................................................................... 24 Migrating to V3 ....................................................................................................................... 24 Path 1 example ................................................................................................................ 25 Path 2 example ................................................................................................................ 26 Path 3 examples .............................................................................................................. 26 Configuring the SDK for JavaScript ..................................................................................................... 28 Configuration per service .......................................................................................................... 28 Setting configuration per service ........................................................................................ 28 Setting the AWS Region ............................................................................................................ 29

iii

AWS SDK for JavaScript Developer Guide for SDK Version 3

In a client class constructor ............................................................................................... 29 Using an environment variable ........................................................................................... 29 Using a shared config file .................................................................................................. 29 Order of precedence for setting the Region ......................................................................... 30 Getting your credentials ............................................................................................................ 30 Setting credentials ................................................................................................................... 31 Best practices for credentials ............................................................................................. 31 Setting credentials in Node.js ............................................................................................. 31 Setting credentials in a web browser .................................................................................. 34 Node.js considerations .............................................................................................................. 36 Using built-in Node.js modules .......................................................................................... 36 Using npm packages ......................................................................................................... 37 Configuring maxSockets in Node.js ..................................................................................... 37 Reusing connections with keep-alive in Node.js .................................................................... 37 Configuring proxies for Node.js .......................................................................................... 38 Registering certificate bundles in Node.js ............................................................................ 38 Browser Script Considerations .................................................................................................... 39 Building the SDK for Browsers ........................................................................................... 39 Cross-origin resource sharing (CORS) .................................................................................. 39 Bundling with webpack ..................................................................................................... 42 Working with services ....................................................................................................................... 46 Creating and calling service objects ............................................................................................ 46 Specifying service object parameters .................................................................................. 47 Calling services asychronously .................................................................................................... 47 Managing asychronous calls ............................................................................................... 48 Using async/await ............................................................................................................ 48 Using promises ................................................................................................................ 49 Using a callback function .................................................................................................. 50 Creating service client requests .................................................................................................. 50 Handling service client responses ............................................................................................... 51 Accessing data returned in the response .............................................................................. 51 Accessing error information ............................................................................................... 51 Working with JSON .................................................................................................................. 51 JSON as service object parameters ..................................................................................... 52 Using AWS Cloud9 with the SDK for JavaScript .................................................................................... 54 Step 1: Set up your AWS account to use AWS Cloud9 ................................................................... 54 Step 2: Set up your AWS Cloud9 development environment .......................................................... 54 Step 3: Set up the SDK for JavaScript ......................................................................................... 55 To set up the SDK for JavaScript for Node.js ........................................................................ 55 To set up the SDK for JavaScript in the browser ................................................................... 55 Step 4: Download example code ................................................................................................ 55 Step 5: Run and debug example code ......................................................................................... 56 Code examples ................................................................................................................................. 57 JavaScript ES6/CommonJS syntax .............................................................................................. 57 Amazon CloudWatch examples .................................................................................................. 59 Creating alarms in Amazon CloudWatch .............................................................................. 60 Using alarm actions in Amazon CloudWatch ........................................................................ 64 Getting metrics from Amazon CloudWatch .......................................................................... 67 Sending events to Amazon CloudWatch Events .................................................................... 70 Using subscription filters in Amazon CloudWatch Logs .......................................................... 75 Amazon DynamoDB examples .................................................................................................... 79 Creating and using tables in DynamoDB .............................................................................. 79 Reading and writing a single item in DynamoDB .................................................................. 84 Reading and writing items in batch in DynamoDB ................................................................ 88 Querying and scanning a DynamoDB table .......................................................................... 91 Using the DynamoDB Document Client ............................................................................... 94 Amazon EC2 examples ............................................................................................................ 103

iv

AWS SDK for JavaScript Developer Guide for SDK Version 3

Creating an Amazon EC2 instance ..................................................................................... 104 Managing Amazon EC2 instances ...................................................................................... 106 Working with Amazon EC2 key pairs ................................................................................. 110 Using Regions and Availability Zones with Amazon EC2 ....................................................... 114 Working with security groups in Amazon EC2 ..................................................................... 115 Using elastic IP addresses in Amazon EC2 .......................................................................... 119 MediaConvert examples ........................................................................................................... 122 Getting your account-specific endpoint ............................................................................. 123 Creating and managing jobs ............................................................................................ 124 Using job templates ........................................................................................................ 130 Amazon S3 Glacier examples ................................................................................................... 137 Creating a S3 Glacier vault .............................................................................................. 138 Uploading an archive to S3 Glacier ................................................................................... 139 AWS Identity and Access Management examples ........................................................................ 140 Managing IAM users ........................................................................................................ 141 Working with IAM policies ............................................................................................... 145 Managing IAM access keys ............................................................................................... 151 Working with IAM server certificates ................................................................................. 156 Managing IAM account aliases .......................................................................................... 160 Amazon Kinesis Examples ........................................................................................................ 163 Capturing Webpage Scroll Progress with Amazon Kinesis ..................................................... 163 AWS Lambda examples ........................................................................................................... 170 Amazon Lex examples ............................................................................................................. 171 Amazon Polly examples ........................................................................................................... 171 The scenario .................................................................................................................. 171 Prerequisite tasks ........................................................................................................... 171 Upload audio recorded using Amazon Polly to Amazon S3 ................................................... 172 Amazon S3 examples .............................................................................................................. 173 Amazon S3 browser examples .......................................................................................... 173 Amazon S3 Node.js examples ........................................................................................... 197 Amazon SES examples ............................................................................................................ 223 Managing identities ........................................................................................................ 224 Working with email templates ......................................................................................... 229 Sending email using Amazon SES ..................................................................................... 235 Using IP address filters .................................................................................................... 240 Using receipt rules .......................................................................................................... 244 Amazon SNS Examples ............................................................................................................ 248 Managing Topics ............................................................................................................. 248 Publishing Messages to a Topic ........................................................................................ 254 Managing Subscriptions ................................................................................................... 255 Sending SMS Messages ................................................................................................... 262 Amazon SQS examples ............................................................................................................ 268 Using queues in Amazon SQS .......................................................................................... 268 Sending and receiving messages in Amazon SQS ................................................................ 273 Managing visibility timeout in Amazon SQS ....................................................................... 276 Enabling long polling in Amazon SQS ............................................................................... 278 Using dead-letter queues in Amazon SQS .......................................................................... 282 Amazon Transcribe examples ................................................................................................... 284 Amazon Transcribe examples ........................................................................................... 284 Amazon Transcribe medical examples ............................................................................... 288 Amazon Redshift examples ...................................................................................................... 291 Amazon Redshift examples .............................................................................................. 292 Cross-service examples .................................................................................................................... 297 Setting up Node.js on an Amazon EC2 instance .......................................................................... 297 Prerequisites .................................................................................................................. 297 Procedure ...................................................................................................................... 297 Creating an Amazon Machine Image (AMI) ......................................................................... 298

v

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

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

Google Online Preview   Download