The AWS SDK for JavaScript v3 is a rewrite of v2 with some great new features. As with version 2, it enables you to easily work with Amazon Web Services, but has a modular architecture with a separate package for each service. It also includes many frequently requested features, such as a first-class TypeScript support and a new middleware stack. For more details, visit blog post on general availability of Modular AWS SDK for JavaScript.
To get started with JavaScript SDK version 3, visit our Developer Guide or API Reference.
If you are starting a new project with AWS SDK for JavaScript v3, then you can refer aws-sdk-js-notes-app which shows examples of calling multiple AWS Services in a note taking application. If you are migrating from v2 to v3, then you can visit our self-guided workshop which builds as basic version of note taking application using AWS SDK for JavaScript v2 and provides step-by-step migration instructions to v3.
To test your universal JavaScript code in Node.js, browser and react-native environments, visit our code samples repo.
Performance is crucial for the AWS SDK for JavaScript because it directly impacts the user experience. Please refer to Performance section to know more.
Let’s walk through setting up a project that depends on DynamoDB from the SDK and makes a simple service call. The following steps use yarn as an example. These steps assume you have Node.js and yarn already installed.
Create a new Node.js project.
Inside of the project, run: yarn add @aws-sdk/client-dynamodb
. Adding packages results in update in lock file, yarn.lock or package-lock.json. You should commit your lock file along with your code to avoid potential breaking changes.
Create a new file called index.js, create a DynamoDB service client and send a request.
const { DynamoDBClient, ListTablesCommand } = require("@aws-sdk/client-dynamodb"); (async () => { const client = new DynamoDBClient({ region: "us-west-2" }); const command = new ListTablesCommand({}); try { const results = await client.send(command); console.log(results.TableNames.join("\n")); } catch (err) { console.error(err); } })();
If you want to use non-modular (v2-like) interfaces, you can import client with only the service name (e.g DynamoDB), and call the operation name directly from the client:
const { DynamoDB } = require("@aws-sdk/client-dynamodb"); (async () => { const client = new DynamoDB({ region: "us-west-2" }); try { const results = await client.listTables({}); console.log(results.TableNames.join("\n")); } catch (err) { console.error(err); } })();
If you use tree shaking to reduce bundle size, using non-modular interface will increase the bundle size as compared to using modular interface.
<!-- Uncomment when numbers are available for gamma clients In our workshop code, a lambda with DynamoDBClient and a command takes ~18kB while DynamoDB takes ~26 kB ([details](https://github.com/aws-samples/aws-sdk-js-v3-workshop/blob/dc3ad778b04dfe3f8f277dca67162da79c937eca/Exercise1/backend/README.md#reduce-bundle-size-by-just-importing-dynamodb)) -->If you are consuming modular AWS SDK for JavaScript on react-native environments, you will need to add and import following polyfills in your react-native application:
import "react-native-get-random-values"; import "react-native-url-polyfill/auto"; import "web-streams-polyfill/dist/polyfill"; import { DynamoDB } from "@aws-sdk/client-dynamodb";
Specifically Metro bundler used by react-native, enable Package Exports Support:
The SDK is now split up across multiple packages. The 2.x version of the SDK contained support for every service. This made it very easy to use multiple services in a project. Due to the limitations around reducing the size of the SDK when only using a handful of services or operations, many customers requested having separate packages for each service client. We have also split up the core parts of the SDK so that service clients only pull in what they need. For example, a service sends responses in JSON will no longer need to also have an XML parser as a dependency.
For those that were already importing services as sub-modules from the v2 SDK, the import statement doesn’t look too different. Here’s an example of importing the AWS Lambda service in v2 of the SDK, and the v3 SDK:
// import the Lambda client constructor in v2 of the SDK const Lambda = require("aws-sdk/clients/lambda"); // import the Lambda client constructor in v3 SDK const { Lambda } = require("@aws-sdk/client-lambda");
It is also possible to import both versions of the Lambda client by changing the variable name the Lambda constructor is stored in.
We’ve made several public API changes to improve consistency, make the SDK easier to use, and remove deprecated or confusing APIs. The following are some of the big changes included in the new AWS SDK for JavaScript v3.
In version 2.x of the SDK, service configuration could be passed to individual client constructors.
However, these configurations would first be merged automatically into a copy of the global SDK configuration: AWS.config
.
Also, calling AWS.config.update({/* params */})
only updated configuration for service clients instantiated after the update call was made, not any existing clients.
This behavior was a frequent source of confusion, and made it difficult to add configuration to the global object that only affects a subset of service clients in a forward-compatible way. In v3, there is no longer a global configuration managed by the SDK. Configuration must be passed to each service client that is instantiated. It is still possible to share the same configuration across multiple clients but that configuration will not be automatically merged with a global state.
Version 2.x of the SDK allows modifying a request throughout multiple stages of a request’s lifecycle by attaching event listeners to a request. Some feedback we received frequently was that it can be difficult to debug what went wrong during a request’s lifecycle. We’ve switched to using a middleware stack to control the lifecycle of an operation call now. This gives us a few benefits. Each middleware in the stack calls the next middleware after making any changes to the request object. This also makes debugging issues in the stack much easier since you can see exactly which middleware have been called leading up to an error. Here’s an example of logging requests using middleware:
const client = new DynamoDB({ region: "us-west-2" }); client.middlewareStack.add( (next, context) => async (args) => { console.log("AWS SDK context", context.clientName, context.commandName); console.log("AWS SDK request input", args.input); const result = await next(args); console.log("AWS SDK request output:", result.output); return result; }, { name: "MyMiddleware", step: "build", override: true, } ); await client.listTables({});
In the above example, we’re adding a middleware to our DynamoDB client’s middleware stack. The first argument is a function that accepts next, the next middleware in the stack to call, and context, an object that contains some information about the operation being called. It returns a function that accepts args, an object that contains the parameters passed to the operation and the request, and returns the result from calling the next middleware with args.
If you are looking for a breakdown of the API changes from AWS SDK for JavaScript v2 to v3, we have them listed in UPGRADING.md.
The Lambda provided AWS SDK is set to a specific minor version, and NOT the latest version. To check the minor version used by Lambda, please refer to Lambda runtimes doc page. If you wish to use the latest / different version of the SDK from the one provided by lambda, we recommend that you bundle and minify your project, or upload it as a Lambda layer.
The performance of the AWS SDK for JavaScript v3 on node 18 has improved from v2 as seen in the performance benchmarking
When using Lambda we should use a single SDK client per service, per region, and initialize it outside of the handler's codepath. This is done to optimize for Lambda's container reuse.
The API calls themselves should be made from within the handler's codepath. This is done to ensure that API calls are signed at the very last step of Lambda's execution cycle, after the Lambda is "hot" to avoid signing time skew.
Example:
import { STSClient, GetCallerIdentityCommand } from "@aws-sdk/client-sts"; const client = new STSClient({}); // SDK Client initialized outside the handler export const handler = async (event) => { const response = { statusCode: 200, headers: { "Content-Type": "application/json", }, }; try { const results = await client.send(new GetCallerIdentityCommand({})); // API operation made from within the handler const responseBody = { userId: results.UserId, }; response.body = JSON.stringify(responseBody); } catch (err) { console.log("Error:", err); response.statusCode = 500; response.body = JSON.stringify({ message: "Internal Server Error", }); } return response; };
Please refer to supplemental docs on performance to know more.
All clients have been published to NPM and can be installed as described above. If you want to play with latest clients, you can build from source as follows:
Clone this repository to local by:
git clone https://github.com/aws/aws-sdk-js-v3.git
Under the repository root directory, run following command to link and build the whole library, the process may take several minutes:
yarn && yarn test:all
For more information, please refer to contributing guide.
After the repository is successfully built, change directory to the client that you want to install, for example:
cd clients/client-dynamodb
Pack the client:
yarn pack .
yarn pack
will create an archive file in the client package folder, e.g. aws-sdk-client-dynamodb-v3.0.0.tgz
.
Change directory to the project you are working on and move the archive to the location to store the vendor packages:
mv path/to/aws-sdk-js-v3/clients/client-dynamodb/aws-sdk-client-dynamodb-v3.0.0.tgz ./path/to/vendors/folder
Install the package to your project:
yarn add ./path/to/vendors/folder/aws-sdk-client-dynamodb-v3.0.0.tgz
You can provide feedback to us in several ways. Both positive and negative feedback is appreciated. If you do, please feel free to open an issue on our GitHub repository. Our GitHub issues page also includes work we know still needs to be done to reach full feature parity with v2 SDK.
GitHub issues. Customers who are comfortable giving public feedback can open a GitHub issue in the new repository. This is the preferred mechanism to give feedback so that other customers can engage in the conversation, +1 issues, etc. Issues you open will be evaluated, and included in our roadmap for the GA launch.
Gitter channel. For informal discussion or general feedback, you may join the Gitter chat. The Gitter channel is also a great place to get help with v3 from other developers. JS SDK team doesn't track the discussion daily, so feel free to open a GitHub issue if your question is not answered there.
You can open pull requests for fixes or additions to the new AWS SDK for JavaScript v3. All pull requests must be submitted under the Apache 2.0 license and will be reviewed by an SDK team member prior to merging. Accompanying unit tests are appreciated. See Contributing for more information.
This is an introduction to some of the high level concepts behind AWS SDK for JavaScript (v3) which are shared between services and might make your life easier. Please consult the user guide and API reference for service specific details.
Bare-bones clients/commands: This refers to a modular way of consuming individual operations on JS SDK clients. It results in less code being imported and thus more performant. It is otherwise equivalent to the aggregated clients/commands.
// this imports a bare-bones version of S3 that exposes the .send operation import { S3Client } from "@aws-sdk/client-s3" // this imports just the getObject operation from S3 import { GetObjectCommand } from "@aws-sdk/client-s3" //usage const bareBonesS3 = new S3Client({...}); await bareBonesS3.send(new GetObjectCommand({...}));
Aggregated clients/commands: This refers to a way of consuming clients that contain all operations on them. Under the hood this calls the bare-bones commands. This imports all commands on a particular client and results in more code being imported and thus
最强AI数据分析助手
小浣熊家族Raccoon,您的AI智能助手,致力于通过先进的人工智能技术,为用户提供高效、便捷的智能服务。无论是日常咨询还是专业问题解答,小浣熊都能以快速、准确的响应满足您的需求,让您的生活更加智能便捷。
像人一样思考的AI智能体
imini 是一款超级AI智能体,能根据人类指令,自主思考、自主完成、并且交付结果的AI智能体。
AI数字人视频创作平台
Keevx 一款开箱即用的AI数字人视频创作平台,广泛适用于电商广告、企业培训与社媒宣传,让全球企业与个人创作者无需拍摄剪辑,就能快速生成多语言、高质量的专业视频。
一站式AI创作平台
提供 AI 驱动的图片、视频生成及数字人等功能,助力创意创作
AI办公助手,复杂任务高效处理
AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!
AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。
AI小说写作助手,一站式润色、改写、扩写
蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。
全能AI智能助手,随时解答生活与工作的多样问题
问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。
实时语音翻译/同声传译工具
Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。
一键生成PPT和Word,让学习生活更轻松
讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。
最新AI工具、AI资讯
独家AI资源、AI项目落地
微信扫一扫关注公众号