From dcc71ca7ddbc72fc78f4019ae62e6a184312c5ee Mon Sep 17 00:00:00 2001 From: Colby Haskell Date: Mon, 13 Nov 2023 06:38:56 -0500 Subject: [PATCH 01/12] Javascript: Fix typo in sign up log message in Cognito Identity Provider (#5642) --- .../scenarios/basic/command-handlers/sign-up-handler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javascriptv3/example_code/cognito-identity-provider/scenarios/basic/command-handlers/sign-up-handler.js b/javascriptv3/example_code/cognito-identity-provider/scenarios/basic/command-handlers/sign-up-handler.js index 4c5f0608122..f0c868b7dbb 100644 --- a/javascriptv3/example_code/cognito-identity-provider/scenarios/basic/command-handlers/sign-up-handler.js +++ b/javascriptv3/example_code/cognito-identity-provider/scenarios/basic/command-handlers/sign-up-handler.js @@ -38,7 +38,7 @@ const signUpHandler = async (commands) => { validateClient(clientId); log(`Signing up.`); await signUp({ clientId, username, password, email }); - log(`Signed up. An confirmation email has been sent to: ${email}.`); + log(`Signed up. A confirmation email has been sent to: ${email}.`); log(`Run 'confirm-sign-up ${username} ' to confirm your account.`); } catch (err) { log(err); From dc499393b5a734ce7c9861a4e506546043f4c500 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 13 Nov 2023 09:24:38 -0500 Subject: [PATCH 02/12] JavaScript v3: Dependabot Bump axios from 0.27.2 to 1.6.0 in /resources/clients/react/item-tracker (#5638) Bump axios from 0.27.2 to 1.6.0 in /resources/clients/react/item-tracker Bumps [axios](https://github.com/axios/axios) from 0.27.2 to 1.6.0. - [Release notes](https://github.com/axios/axios/releases) - [Changelog](https://github.com/axios/axios/blob/v1.x/CHANGELOG.md) - [Commits](https://github.com/axios/axios/compare/v0.27.2...v1.6.0) --- updated-dependencies: - dependency-name: axios dependency-type: direct:production ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .../react/item-tracker/package-lock.json | 34 +++++++++++++------ .../clients/react/item-tracker/package.json | 2 +- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/resources/clients/react/item-tracker/package-lock.json b/resources/clients/react/item-tracker/package-lock.json index d04bc518ff0..f2f603a5cbd 100644 --- a/resources/clients/react/item-tracker/package-lock.json +++ b/resources/clients/react/item-tracker/package-lock.json @@ -8,7 +8,7 @@ "name": "item-tracker", "version": "0.1.0", "dependencies": { - "axios": "^0.27.2", + "axios": "^1.6.0", "react": "^18.1.0", "react-bootstrap": "^2.4.0", "react-dom": "^18.1.0", @@ -5748,12 +5748,13 @@ } }, "node_modules/axios": { - "version": "0.27.2", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz", - "integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==", + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.0.tgz", + "integrity": "sha512-EZ1DYihju9pwVB+jg67ogm+Tmqc6JmhamRN6I4Zt8DfZu5lbcQGw3ozH9lFejSJgs/ibaef3A9PMXPLeefFGJg==", "dependencies": { - "follow-redirects": "^1.14.9", - "form-data": "^4.0.0" + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" } }, "node_modules/axios/node_modules/form-data": { @@ -16541,6 +16542,11 @@ "node": ">= 0.10" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/psl": { "version": "1.8.0", "resolved": "https://registry.npmjs.org/psl/-/psl-1.8.0.tgz", @@ -24496,12 +24502,13 @@ "integrity": "sha512-LVAaGp/wkkgYJcjmHsoKx4juT1aQvJyPcW09MLCjVTh3V2cc6PnyempiLMNH5iMdfIX/zdbjUx2KDjMLCTdPeA==" }, "axios": { - "version": "0.27.2", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz", - "integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==", + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.0.tgz", + "integrity": "sha512-EZ1DYihju9pwVB+jg67ogm+Tmqc6JmhamRN6I4Zt8DfZu5lbcQGw3ozH9lFejSJgs/ibaef3A9PMXPLeefFGJg==", "requires": { - "follow-redirects": "^1.14.9", - "form-data": "^4.0.0" + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" }, "dependencies": { "form-data": { @@ -32355,6 +32362,11 @@ } } }, + "proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "psl": { "version": "1.8.0", "resolved": "https://registry.npmjs.org/psl/-/psl-1.8.0.tgz", diff --git a/resources/clients/react/item-tracker/package.json b/resources/clients/react/item-tracker/package.json index 3b12df62e72..d3fc81bf3ed 100644 --- a/resources/clients/react/item-tracker/package.json +++ b/resources/clients/react/item-tracker/package.json @@ -3,7 +3,7 @@ "version": "0.1.0", "private": true, "dependencies": { - "axios": "^0.27.2", + "axios": "^1.6.0", "react": "^18.1.0", "react-bootstrap": "^2.4.0", "react-dom": "^18.1.0", From f0af2a86c8ce499210831335d9ffdc1a67a89cf7 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 13 Nov 2023 09:28:51 -0500 Subject: [PATCH 03/12] JavaScript v3: Dependabot Bump axios from 1.3.6 to 1.6.0 in /resources/clients/react/elwing/src/plugins/sqs-message (#5639) Bump axios in /resources/clients/react/elwing/src/plugins/sqs-message Bumps [axios](https://github.com/axios/axios) from 1.3.6 to 1.6.0. - [Release notes](https://github.com/axios/axios/releases) - [Changelog](https://github.com/axios/axios/blob/v1.x/CHANGELOG.md) - [Commits](https://github.com/axios/axios/compare/v1.3.6...v1.6.0) --- updated-dependencies: - dependency-name: axios dependency-type: direct:production ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .../src/plugins/sqs-message/package-lock.json | 14 +++++++------- .../elwing/src/plugins/sqs-message/package.json | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/resources/clients/react/elwing/src/plugins/sqs-message/package-lock.json b/resources/clients/react/elwing/src/plugins/sqs-message/package-lock.json index 9cfc0030a6c..3d26a4a08c4 100644 --- a/resources/clients/react/elwing/src/plugins/sqs-message/package-lock.json +++ b/resources/clients/react/elwing/src/plugins/sqs-message/package-lock.json @@ -9,7 +9,7 @@ "version": "1.0.0", "license": "Apache-2.0", "dependencies": { - "axios": "^1.3.6" + "axios": "^1.6.0" }, "peerDependencies": { "@cloudscape-design/components": "^3.0.251", @@ -191,9 +191,9 @@ "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" }, "node_modules/axios": { - "version": "1.3.6", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.3.6.tgz", - "integrity": "sha512-PEcdkk7JcdPiMDkvM4K6ZBRYq9keuVJsToxm2zQIM70Qqo2WHTdJZMXcG9X+RmRp2VPNUQC8W1RAGbgt6b1yMg==", + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.0.tgz", + "integrity": "sha512-EZ1DYihju9pwVB+jg67ogm+Tmqc6JmhamRN6I4Zt8DfZu5lbcQGw3ozH9lFejSJgs/ibaef3A9PMXPLeefFGJg==", "dependencies": { "follow-redirects": "^1.15.0", "form-data": "^4.0.0", @@ -768,9 +768,9 @@ "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" }, "axios": { - "version": "1.3.6", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.3.6.tgz", - "integrity": "sha512-PEcdkk7JcdPiMDkvM4K6ZBRYq9keuVJsToxm2zQIM70Qqo2WHTdJZMXcG9X+RmRp2VPNUQC8W1RAGbgt6b1yMg==", + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.0.tgz", + "integrity": "sha512-EZ1DYihju9pwVB+jg67ogm+Tmqc6JmhamRN6I4Zt8DfZu5lbcQGw3ozH9lFejSJgs/ibaef3A9PMXPLeefFGJg==", "requires": { "follow-redirects": "^1.15.0", "form-data": "^4.0.0", diff --git a/resources/clients/react/elwing/src/plugins/sqs-message/package.json b/resources/clients/react/elwing/src/plugins/sqs-message/package.json index a48a15f749c..aa66d1337c3 100644 --- a/resources/clients/react/elwing/src/plugins/sqs-message/package.json +++ b/resources/clients/react/elwing/src/plugins/sqs-message/package.json @@ -5,7 +5,7 @@ "author": "corepyle@amazon.com", "license": "Apache-2.0", "dependencies": { - "axios": "^0.27.2" + "axios": "^1.6.0" }, "peerDependencies": { "@cloudscape-design/components": "^3.0.251", From 9c52e8174b5530d539d8b976e28398b6231d9350 Mon Sep 17 00:00:00 2001 From: David Souther Date: Mon, 13 Nov 2023 10:39:43 -0500 Subject: [PATCH 04/12] Meta: Ignore *.iml (#5646) --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 4902540858e..f6912104c3d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ .DS_Store ._* .idea +*.iml .metadata .phpunit.result.cache .recommenders From d36a8241fc11b5ab3f1e95a7f1e79507c613770a Mon Sep 17 00:00:00 2001 From: Steven Meyer <108885656+meyertst-aws@users.noreply.github.com> Date: Mon, 13 Nov 2023 12:58:16 -0500 Subject: [PATCH 05/12] C++: remove unintended #if (#5648) remove the #if 0 --- .../autoscaling/tests/gtest_groups_and_instances_scenario.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp/example_code/autoscaling/tests/gtest_groups_and_instances_scenario.cpp b/cpp/example_code/autoscaling/tests/gtest_groups_and_instances_scenario.cpp index ecdb538e39f..01ebbd16d56 100644 --- a/cpp/example_code/autoscaling/tests/gtest_groups_and_instances_scenario.cpp +++ b/cpp/example_code/autoscaling/tests/gtest_groups_and_instances_scenario.cpp @@ -20,7 +20,7 @@ namespace AwsDocTest { extern const std::vector RESPONSES; bool addHttpResponses(MockHTTP &mockHttp); -#if 0 + // Only run the un-mocked test in special cases because of its long execution time. // NOLINTNEXTLINE (readability-named-parameter) TEST_F(AutoScaling_GTests, groups_and_instances_scenario_2L_) { @@ -29,7 +29,7 @@ namespace AwsDocTest { bool result = AwsDoc::AutoScaling::groupsAndInstancesScenario(*s_clientConfig); ASSERT_TRUE(result); } -#endif + // NOLINTNEXTLINE (readability-named-parameter) TEST_F(AutoScaling_GTests, groups_and_instances_scenario_3_) { AddCommandLineResponses(RESPONSES); From 39376612bb6a5f3cd0fcf5d1e183971efabaef90 Mon Sep 17 00:00:00 2001 From: Dennis Traub Date: Mon, 13 Nov 2023 20:11:29 +0100 Subject: [PATCH 06/12] Add Bedrock examples for Go, Java, and Python (#5649) Bedrock examples include: * Bedrock: List Foundation Models * Bedrock Runtime: Invoke Text Models (Claude v2) * Bedrock Runtime: Invoke Image Models (Stable Diffusion) --------- Co-authored-by: Dennis Traub --- .../metadata/bedrock-runtime_metadata.yaml | 101 ++++++ .doc_gen/metadata/bedrock_metadata.yaml | 51 +++ gov2/bedrock/README.md | 97 ++++++ gov2/bedrock/actions/foundation_model.go | 44 +++ gov2/bedrock/actions/foundation_model_test.go | 56 ++++ gov2/bedrock/go.mod | 26 ++ gov2/bedrock/go.sum | 30 ++ gov2/bedrock/hello/hello.go | 42 +++ gov2/bedrock/stubs/foundation_model_stubs.go | 22 ++ gov2/bedrock/stubs/go.mod | 5 + .../example_code/bedrock-runtime/.gitignore | 1 + javav2/example_code/bedrock-runtime/Readme.md | 80 +++++ javav2/example_code/bedrock-runtime/pom.xml | 60 ++++ .../example/bedrockruntime/InvokeModel.java | 294 +++++++++++++++++ .../InvokeModelWithResponseStream.java | 99 ++++++ .../src/test/java/BedrockRuntimeTest.java | 109 +++++++ javav2/example_code/bedrock/Readme.md | 77 +++++ javav2/example_code/bedrock/pom.xml | 60 ++++ .../example/bedrock/ListFoundationModels.java | 65 ++++ .../bedrock/src/test/java/BedrockTest.java | 36 +++ .../example_code/bedrock-runtime/.gitignore | 1 + python/example_code/bedrock-runtime/README.md | 90 ++++++ .../bedrock_runtime_wrapper.py | 305 ++++++++++++++++++ .../bedrock-runtime/requirements.txt | 13 + .../bedrock-runtime/test/conftest.py | 12 + .../test/test_bedrock_runtime_wrapper.py | 102 ++++++ python/example_code/bedrock/README.md | 87 +++++ .../example_code/bedrock/bedrock_wrapper.py | 91 ++++++ python/example_code/bedrock/requirements.txt | 4 + python/example_code/bedrock/test/conftest.py | 12 + .../bedrock/test/test_bedrock_wrapper.py | 42 +++ python/test_tools/bedrock_runtime_stubber.py | 118 +++++++ python/test_tools/bedrock_stubber.py | 32 ++ python/test_tools/stubber_factory.py | 6 + 34 files changed, 2270 insertions(+) create mode 100644 .doc_gen/metadata/bedrock_metadata.yaml create mode 100644 gov2/bedrock/README.md create mode 100644 gov2/bedrock/actions/foundation_model.go create mode 100644 gov2/bedrock/actions/foundation_model_test.go create mode 100644 gov2/bedrock/go.mod create mode 100644 gov2/bedrock/go.sum create mode 100644 gov2/bedrock/hello/hello.go create mode 100644 gov2/bedrock/stubs/foundation_model_stubs.go create mode 100644 gov2/bedrock/stubs/go.mod create mode 100644 javav2/example_code/bedrock-runtime/.gitignore create mode 100644 javav2/example_code/bedrock-runtime/Readme.md create mode 100644 javav2/example_code/bedrock-runtime/pom.xml create mode 100644 javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/InvokeModel.java create mode 100644 javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/InvokeModelWithResponseStream.java create mode 100644 javav2/example_code/bedrock-runtime/src/test/java/BedrockRuntimeTest.java create mode 100644 javav2/example_code/bedrock/Readme.md create mode 100644 javav2/example_code/bedrock/pom.xml create mode 100644 javav2/example_code/bedrock/src/main/java/com/example/bedrock/ListFoundationModels.java create mode 100644 javav2/example_code/bedrock/src/test/java/BedrockTest.java create mode 100644 python/example_code/bedrock-runtime/.gitignore create mode 100644 python/example_code/bedrock-runtime/README.md create mode 100644 python/example_code/bedrock-runtime/bedrock_runtime_wrapper.py create mode 100644 python/example_code/bedrock-runtime/requirements.txt create mode 100644 python/example_code/bedrock-runtime/test/conftest.py create mode 100644 python/example_code/bedrock-runtime/test/test_bedrock_runtime_wrapper.py create mode 100644 python/example_code/bedrock/README.md create mode 100644 python/example_code/bedrock/bedrock_wrapper.py create mode 100644 python/example_code/bedrock/requirements.txt create mode 100644 python/example_code/bedrock/test/conftest.py create mode 100644 python/example_code/bedrock/test/test_bedrock_wrapper.py create mode 100644 python/test_tools/bedrock_runtime_stubber.py create mode 100644 python/test_tools/bedrock_stubber.py diff --git a/.doc_gen/metadata/bedrock-runtime_metadata.yaml b/.doc_gen/metadata/bedrock-runtime_metadata.yaml index 96041b64390..58dbd69cb95 100644 --- a/.doc_gen/metadata/bedrock-runtime_metadata.yaml +++ b/.doc_gen/metadata/bedrock-runtime_metadata.yaml @@ -18,3 +18,104 @@ bedrock-runtime_InvokeModel: - bdr.abapv1.invokemodel_stable_diffusion services: bedrock-runtime: {InvokeModel} + +bedrock-runtime_InvokeClaude: + title: Invoke the Anthropic Claude model on &BR; to run an inference + title_abbrev: Invoke Anthropic Claude 2 on &BR; + synopsis: invoke the anthropic claude model on &BR; to run inference. + category: + languages: + Java: + versions: + - sdk_version: 2 + github: javav2/example_code/bedrock-runtime + excerpts: + - description: Invoke the Anthropic Claude 2 foundation model. + snippet_tags: + - bedrock-runtime.java2.invoke_claude.main + Python: + versions: + - sdk_version: 3 + github: python/example_code/bedrock-runtime + excerpts: + - description: Invoke the Anthropic Claude 2 foundation model. + snippet_tags: + - python.example_code.bedrock-runtime.InvokeAnthropicClaude + services: + bedrock-runtime: {InvokeModel} + +bedrock-runtime_InvokeJurassic2: + title: Invoke the AI21 Labs Jurassic-2 model on &BR; to run an inference + title_abbrev: Invoke AI21 Labs Jurassic-2 on &BR; + synopsis: invoke the ai21 labs jurassic-2 model on &BR; to run inference. + category: + languages: + Java: + versions: + - sdk_version: 2 + github: javav2/example_code/bedrock-runtime + excerpts: + - description: Invoke the AI21 Labs Jurassic-2 foundation model. + snippet_tags: + - bedrock-runtime.java2.invoke_jurassic2.main + Python: + versions: + - sdk_version: 3 + github: python/example_code/bedrockruntime + sdkguide: + excerpts: + - description: Invoke the AI 21 Labs Jurassic-2 foundation model. + snippet_tags: + - python.example_code.bedrock-runtime.InvokeAi21Jurassic2 + services: + bedrock-runtime: {InvokeModel} + +bedrock-runtime_InvokeStableDiffusion: + title: Invoke the Stability.ai Stable Diffusion XL model on &BR; to run an inference + title_abbrev: Invoke Stability.ai Stable Diffusion XL on &BR; + synopsis: invoke the stability.ai stable diffusion xl on &BR; model to run inference. + category: + languages: + Java: + versions: + - sdk_version: 2 + github: javav2/example_code/bedrock-runtime + excerpts: + - description: Invoke the Stability.ai Stable Diffusion XL foundation model. + snippet_tags: + - bedrock-runtime.java2.invoke_stable_diffusion.main + Python: + versions: + - sdk_version: 3 + github: python/example_code/bedrock-runtime + excerpts: + - description: Invoke the Stability.ai Stable Diffusion foundation model. + snippet_tags: + - python.example_code.bedrock-runtime.InvokeStableDiffusion + services: + bedrock-runtime: {InvokeModel} + +bedrock-runtime_InvokeModelWithResponseStream: + title: Invoke the specified model on &BR; to run an inference with a response stream + title_abbrev: Invoke a model on &BR; with a response stream + synopsis: invoke an model on &BR; to run an inference with a response stream. + category: + languages: + Java: + versions: + - sdk_version: 2 + github: javav2/example_code/bedrock-runtime + excerpts: + - description: Invoke a model on &BR; and process the response stream. + snippet_tags: + - bedrock-runtime.java2.invoke_model_with_response_stream.main + Python: + versions: + - sdk_version: 3 + github: python/example_code/bedrock-runtime + excerpts: + - description: Invoke a model on &BR; and process the response stream. + snippet_tags: + - python.example_code.bedrock-runtime.InvokeModelWithResponseStream + services: + bedrock-runtime: {InvokeModelWithResponseStream} diff --git a/.doc_gen/metadata/bedrock_metadata.yaml b/.doc_gen/metadata/bedrock_metadata.yaml new file mode 100644 index 00000000000..fbc9652f580 --- /dev/null +++ b/.doc_gen/metadata/bedrock_metadata.yaml @@ -0,0 +1,51 @@ +# zexi 0.4.0 +bedrock_Hello: + title: Hello &BR; + title_abbrev: Hello &BR; + synopsis: get started using &BR;. + category: Hello + languages: + Go: + versions: + - sdk_version: 2 + github: gov2/bedrock + excerpts: + - description: + snippet_tags: + - gov2.bedrock.Hello + services: + bedrock: {ListFoundationModels} + +bedrock_ListFoundationModels: + title: List the available &BRlong; foundation models using an &AWS; SDK + title_abbrev: List available &BR; foundation models + synopsis: list available &BR; foundation models. + category: + languages: + Go: + versions: + - sdk_version: 2 + github: gov2/bedrock + excerpts: + - description: List the available Bedrock foundation models. + snippet_tags: + - gov2.bedrock.FoundationModelWrapper.struct + - gov2.bedrock.ListFoundationModels + Java: + versions: + - sdk_version: 2 + github: javav2/example_code/bedrock + excerpts: + - description: List the available &BRlong; foundation models. + snippet_tags: + - bedrock.java2.list_foundation_models.main + Python: + versions: + - sdk_version: 3 + github: python/example_code/bedrock + excerpts: + - description: List the available &BRlong; foundation models. + snippet_tags: + - python.example_code.bedrock.ListFoundationModels + services: + bedrock: {ListFoundationModels} diff --git a/gov2/bedrock/README.md b/gov2/bedrock/README.md new file mode 100644 index 00000000000..8f30512064b --- /dev/null +++ b/gov2/bedrock/README.md @@ -0,0 +1,97 @@ + +# Amazon Bedrock code examples for the SDK for Go V2 + +## Overview + +Shows how to use the AWS SDK for Go V2 to work with Amazon Bedrock. + + + + +*Amazon Bedrock enables you to build and scale generative AI applications with foundation models.* + +## ⚠ Important + +* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/?aws-products-pricing.sort-by=item.additionalFields.productNameLowercase&aws-products-pricing.sort-order=asc&awsf.Free%20Tier%20Type=*all&awsf.tech-category=*all) and [Free Tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all). +* Running the tests might result in charges to your AWS account. +* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). +* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). + + + + +## Code examples + +### Prerequisites + +For prerequisites, see the [README](../README.md#Prerequisites) in the `gov2` folder. + + + +> ⚠ You must request access to a model before you can use it. If you try to use the model (with the API or console) before you have requested access to it, you will receive an error message. For more information, see [Model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html). + + + +### Get started + +* [Hello Amazon Bedrock](hello/hello.go#L4) (`ListFoundationModels`) + +### Single actions + +Code excerpts that show you how to call individual service functions. + +* [List available Amazon Bedrock foundation models](actions/foundation_model.go#L25) (`ListFoundationModels`) + +## Run the examples + +### Instructions + + + + + + +#### Hello Amazon Bedrock + +This example shows you how to get started using Amazon Bedrock. + +``` +go run ./hello +``` + +#### Run a scenario + +All scenarios can be run with the `cmd` runner. To get a list of scenarios +and to get help for running a scenario, use the following command: + +``` +go run ./cmd -h +``` + +### Tests + +⚠ Running tests might result in charges to your AWS account. + + +To find instructions for running these tests, see the [README](../README.md#Tests) +in the `gov2` folder. + + + + + + +## Additional resources + +* [Amazon Bedrock User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html) +* [Amazon Bedrock API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html) +* [SDK for Go V2 Amazon Bedrock reference](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/bedrock) + + + + +--- + +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 \ No newline at end of file diff --git a/gov2/bedrock/actions/foundation_model.go b/gov2/bedrock/actions/foundation_model.go new file mode 100644 index 00000000000..7357b9f183b --- /dev/null +++ b/gov2/bedrock/actions/foundation_model.go @@ -0,0 +1,44 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package actions + +import ( + "context" + "log" + + "github.com/aws/aws-sdk-go-v2/service/bedrock" + "github.com/aws/aws-sdk-go-v2/service/bedrock/types" +) + +// snippet-start:[gov2.bedrock.FoundationModelWrapper.complete] +// snippet-start:[gov2.bedrock.FoundationModelWrapper.struct] + +// PolicyWrapper encapsulates Amazon Bedrock actions used in the examples. +// It contains a Bedrock service client that is used to perform foundation model actions. +type FoundationModelWrapper struct { + BedrockClient *bedrock.Client +} + +// snippet-end:[gov2.bedrock.FoundationModelWrapper.struct] + +// snippet-start:[gov2.bedrock.ListFoundationModels] + +// ListPolicies lists Bedrock foundation models that you can use. +func (wrapper FoundationModelWrapper) ListFoundationModels() ([]types.FoundationModelSummary, error) { + + var models []types.FoundationModelSummary + + result, err := wrapper.BedrockClient.ListFoundationModels(context.TODO(), &bedrock.ListFoundationModelsInput{}) + + if err != nil { + log.Printf("Couldn't list foundation models. Here's why: %v\n", err) + } else { + models = result.ModelSummaries + } + return models, err +} + +// snippet-end:[gov2.bedrock.ListFoundationModels] + +// snippet-end:[gov2.bedrock.FoundationModelWrapper.complete] \ No newline at end of file diff --git a/gov2/bedrock/actions/foundation_model_test.go b/gov2/bedrock/actions/foundation_model_test.go new file mode 100644 index 00000000000..e15e0e0bc2f --- /dev/null +++ b/gov2/bedrock/actions/foundation_model_test.go @@ -0,0 +1,56 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// Unit tests for the foundation model actions. + +package actions + +import ( + "testing" + "log" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/bedrock" + "github.com/awsdocs/aws-doc-sdk-examples/gov2/bedrock/stubs" + "github.com/awsdocs/aws-doc-sdk-examples/gov2/testtools" +) + +const region ="us-east-1" + +func CallFoundationModelActions(sdkConfig aws.Config, ) { + defer func() { + if r := recover(); r != nil { + log.Println(r) + } + }() + + bedrockClient := bedrock.NewFromConfig(sdkConfig) + foundationModelWrapper := FoundationModelWrapper{bedrockClient} + + models, err := foundationModelWrapper.ListFoundationModels() + if err != nil {panic(err)} + for _, model := range models { + log.Println(*model.ModelId) + } + + log.Printf("Thanks for watching!") +} + +func TestCallFoundationModelActions(t *testing.T) { + scenTest := FoundationModelActionsTest{} + testtools.RunScenarioTests(&scenTest, t) +} + +type FoundationModelActionsTest struct {} + +func (scenTest *FoundationModelActionsTest) SetupDataAndStubs() []testtools.Stub { + var stubList []testtools.Stub + stubList = append(stubList, stubs.StubListFoundationModels(nil)) + return stubList +} + +func (scenTest *FoundationModelActionsTest) RunSubTest(stubber *testtools.AwsmStubber) { + CallFoundationModelActions(*stubber.SdkConfig) +} + +func (scenTest *FoundationModelActionsTest) Cleanup() {} \ No newline at end of file diff --git a/gov2/bedrock/go.mod b/gov2/bedrock/go.mod new file mode 100644 index 00000000000..2b0b16d9ec6 --- /dev/null +++ b/gov2/bedrock/go.mod @@ -0,0 +1,26 @@ +module github.com/awsdocs/aws-doc-sdk-examples/gov2/bedrock + +go 1.21 + +require ( + github.com/aws/aws-sdk-go-v2/config v1.23.0 + github.com/aws/aws-sdk-go-v2/service/bedrock v1.3.1 +) + +require ( + github.com/aws/aws-sdk-go-v2 v1.22.2 // indirect + github.com/aws/aws-sdk-go-v2/credentials v1.15.2 // indirect + github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.3 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.2 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.2 // indirect + github.com/aws/aws-sdk-go-v2/internal/ini v1.6.0 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.2 // indirect + github.com/aws/aws-sdk-go-v2/service/sso v1.17.1 // indirect + github.com/aws/aws-sdk-go-v2/service/ssooidc v1.19.1 // indirect + github.com/aws/aws-sdk-go-v2/service/sts v1.25.1 // indirect + github.com/aws/smithy-go v1.16.0 // indirect + github.com/awsdocs/aws-doc-sdk-examples/gov2/bedrock/stubs v0.0.0-00010101000000-000000000000 // indirect + github.com/awsdocs/aws-doc-sdk-examples/gov2/testtools v0.0.0-20231110170957-c3edee1e8c41 // indirect +) + +replace github.com/awsdocs/aws-doc-sdk-examples/gov2/bedrock/stubs => ./stubs diff --git a/gov2/bedrock/go.sum b/gov2/bedrock/go.sum new file mode 100644 index 00000000000..2a6071ce5c6 --- /dev/null +++ b/gov2/bedrock/go.sum @@ -0,0 +1,30 @@ +github.com/aws/aws-sdk-go-v2 v1.22.2 h1:lV0U8fnhAnPz8YcdmZVV60+tr6CakHzqA6P8T46ExJI= +github.com/aws/aws-sdk-go-v2 v1.22.2/go.mod h1:Kd0OJtkW3Q0M0lUWGszapWjEvrXDzRW+D21JNsroB+c= +github.com/aws/aws-sdk-go-v2/config v1.23.0 h1:kqzEfGGDIrRJpfJckgwuZfFTbU9NB1jZnRcaO9MpOqE= +github.com/aws/aws-sdk-go-v2/config v1.23.0/go.mod h1:p7wbxKXXjS1GGQOss7VXOazVMFF9bjUGq85/4wR/fSw= +github.com/aws/aws-sdk-go-v2/credentials v1.15.2 h1:rKH7khRMxPdD0u3dHecd0Q7NOVw3EUe7AqdkUOkiOGI= +github.com/aws/aws-sdk-go-v2/credentials v1.15.2/go.mod h1:tXM8wmaeAhfC7nZoCxb0FzM/aRaB1m1WQ7x0qlBLq80= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.3 h1:G5KawTAkyHH6WyKQCdHiW4h3PmAXNJpOgwKg3H7sDRE= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.3/go.mod h1:hugKmSFnZB+HgNI1sYGT14BUPZkO6alC/e0AWu+0IAQ= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.2 h1:AaQsr5vvGR7rmeSWBtTCcw16tT9r51mWijuCQhzLnq8= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.2/go.mod h1:o1IiRn7CWocIFTXJjGKJDOwxv1ibL53NpcvcqGWyRBA= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.2 h1:UZx8SXZ0YtzRiALzYAWcjb9Y9hZUR7MBKaBQ5ouOjPs= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.2/go.mod h1:ipuRpcSaklmxR6C39G187TpBAO132gUfleTGccUPs8c= +github.com/aws/aws-sdk-go-v2/internal/ini v1.6.0 h1:hwZB07/beLiCopuRKF0t+dEHmP39iN4YtDh3X5d3hrg= +github.com/aws/aws-sdk-go-v2/internal/ini v1.6.0/go.mod h1:rdAuXeHWhI/zkpYcO5n8WCpaIgY9MUxFyBsuqq3kjyA= +github.com/aws/aws-sdk-go-v2/service/bedrock v1.3.1 h1:lAbJ/wQ96QZPbgMGkwy4MkwYBE0fSkHrsCqpdzKB8FA= +github.com/aws/aws-sdk-go-v2/service/bedrock v1.3.1/go.mod h1:T6sGivDDiUgXY/R3+S9t5oRR0XJiUeKAXyyw3LRdTK4= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.2 h1:h7j73yuAVVjic8pqswh+L/7r2IHP43QwRyOu6zcCDDE= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.2/go.mod h1:H07AHdK5LSy8F7EJUQhoxyiCNkePoHj2D8P2yGTWafo= +github.com/aws/aws-sdk-go-v2/service/sso v1.17.1 h1:km+ZNjtLtpXYf42RdaDZnNHm9s7SYAuDGTafy6nd89A= +github.com/aws/aws-sdk-go-v2/service/sso v1.17.1/go.mod h1:aHBr3pvBSD5MbzOvQtYutyPLLRPbl/y9x86XyJJnUXQ= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.19.1 h1:iRFNqZH4a67IqPvK8xxtyQYnyrlsvwmpHOe9r55ggBA= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.19.1/go.mod h1:pTy5WM+6sNv2tB24JNKFtn6EvciQ5k40ZJ0pq/Iaxj0= +github.com/aws/aws-sdk-go-v2/service/sts v1.25.1 h1:txgVXIXWPXyqdiVn92BV6a/rgtpX31HYdsOYj0sVQQQ= +github.com/aws/aws-sdk-go-v2/service/sts v1.25.1/go.mod h1:VAiJiNaoP1L89STFlEMgmHX1bKixY+FaP+TpRFrmyZ4= +github.com/aws/smithy-go v1.16.0 h1:gJZEH/Fqh+RsvlJ1Zt4tVAtV6bKkp3cC+R6FCZMNzik= +github.com/aws/smithy-go v1.16.0/go.mod h1:NukqUGpCZIILqqiV0NIjeFh24kd/FAa4beRb6nbIUPE= +github.com/awsdocs/aws-doc-sdk-examples/gov2/testtools v0.0.0-20231110170957-c3edee1e8c41 h1:nvpkaKqg/kV+x1kGdAnUiEiFeuzExBlaPCtqxKWQcbs= +github.com/awsdocs/aws-doc-sdk-examples/gov2/testtools v0.0.0-20231110170957-c3edee1e8c41/go.mod h1:qcs782jWmSQW2exwfKW39rOvOJBZ4xzO8dVLoFF62Sc= +github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= diff --git a/gov2/bedrock/hello/hello.go b/gov2/bedrock/hello/hello.go new file mode 100644 index 00000000000..a521fc2e51b --- /dev/null +++ b/gov2/bedrock/hello/hello.go @@ -0,0 +1,42 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// snippet-start:[gov2.bedrock.Hello] + +package main + +import ( + "context" + "fmt" + + "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/service/bedrock" +) + +const region = "us-east-1" + +// main uses the AWS SDK for Go (v2) to create an Amazon Bedrock client and +// list the available foundation models in your account and the chosen region. +// This example uses the default settings specified in your shared credentials +// and config files. +func main() { + sdkConfig, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region)) + if err != nil { + fmt.Println("Couldn't load default configuration. Have you set up your AWS account?") + fmt.Println(err) + return + } + bedrockClient := bedrock.NewFromConfig(sdkConfig) + result, err := bedrockClient.ListFoundationModels(context.TODO(), &bedrock.ListFoundationModelsInput{}) + if err != nil { + fmt.Printf("Couldn't list foundation models. Here's why: %v\n", err) + return + } + if len(result.ModelSummaries) == 0 { + fmt.Println("There are no foundation models.")} + for _, modelSummary := range result.ModelSummaries { + fmt.Println(fmt.Sprintf("%s", *modelSummary.ModelId)) + } +} + +// snippet-end:[gov2.bedrock.Hello] \ No newline at end of file diff --git a/gov2/bedrock/stubs/foundation_model_stubs.go b/gov2/bedrock/stubs/foundation_model_stubs.go new file mode 100644 index 00000000000..cd94db29955 --- /dev/null +++ b/gov2/bedrock/stubs/foundation_model_stubs.go @@ -0,0 +1,22 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// Defines stubs used for unit testing the foundation model actions. + +package stubs + +import ( + "github.com/aws/aws-sdk-go-v2/service/bedrock" + "github.com/aws/aws-sdk-go-v2/service/bedrock/types" + "github.com/awsdocs/aws-doc-sdk-examples/gov2/testtools" +) + +func StubListFoundationModels(raiseErr *testtools.StubError) testtools.Stub { + var modelSummaries []types.FoundationModelSummary + return testtools.Stub{ + OperationName: "ListFoundationModels", + Input: &bedrock.ListFoundationModelsInput{}, + Output: &bedrock.ListFoundationModelsOutput{ModelSummaries: modelSummaries}, + Error: raiseErr, + } +} \ No newline at end of file diff --git a/gov2/bedrock/stubs/go.mod b/gov2/bedrock/stubs/go.mod new file mode 100644 index 00000000000..f31a8df6aea --- /dev/null +++ b/gov2/bedrock/stubs/go.mod @@ -0,0 +1,5 @@ +module github.com/awsdocs/aws-doc-sdk-examples/gov2/bedrock + +go 1.21 + +require () diff --git a/javav2/example_code/bedrock-runtime/.gitignore b/javav2/example_code/bedrock-runtime/.gitignore new file mode 100644 index 00000000000..6caf68aff42 --- /dev/null +++ b/javav2/example_code/bedrock-runtime/.gitignore @@ -0,0 +1 @@ +output \ No newline at end of file diff --git a/javav2/example_code/bedrock-runtime/Readme.md b/javav2/example_code/bedrock-runtime/Readme.md new file mode 100644 index 00000000000..798f7a003f5 --- /dev/null +++ b/javav2/example_code/bedrock-runtime/Readme.md @@ -0,0 +1,80 @@ + +# Amazon Bedrock Runtime code examples for the SDK for Java 2.x + +## Overview + +Shows how to use the AWS SDK for Java 2.x to work with Amazon Bedrock Runtime. + + + + +*Amazon Bedrock Runtime is a fully managed service that makes it easy to use foundation models from third-party providers and Amazon.* + +## ⚠ Important + +* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/?aws-products-pricing.sort-by=item.additionalFields.productNameLowercase&aws-products-pricing.sort-order=asc&awsf.Free%20Tier%20Type=*all&awsf.tech-category=*all) and [Free Tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all). +* Running the tests might result in charges to your AWS account. +* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). +* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). + + + + +## Code examples + +### Prerequisites + +For prerequisites, see the [README](../../README.md#Prerequisites) in the `javav2` folder. + + + +> ⚠ You must request access to a model before you can use it. If you try to use the model (with the API or console) before you have requested access to it, you will receive an error message. For more information, see [Model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html). + + +### Single actions + +Code excerpts that show you how to call individual service functions. + +* [Invoke AI21 Labs Jurassic-2 on Amazon Bedrock](src/main/java/com/example/bedrockruntime/InvokeModel.java#L101) (`InvokeModel`) +* [Invoke Anthropic Claude 2 on Amazon Bedrock](src/main/java/com/example/bedrockruntime/InvokeModel.java#L51) (`InvokeModel`) +* [Invoke Stability.ai Stable Diffusion XL on Amazon Bedrock](src/main/java/com/example/bedrockruntime/InvokeModel.java#L151) (`InvokeModel`) +* [Invoke a model on Amazon Bedrock with a response stream](src/main/java/com/example/bedrockruntime/InvokeModelWithResponseStream.java#L49) (`InvokeModelWithResponseStream`) + +## Run the examples + +### Instructions + + + + + + + + +### Tests + +⚠ Running tests might result in charges to your AWS account. + + +To find instructions for running these tests, see the [README](../../README.md#Tests) +in the `javav2` folder. + + + + + + +## Additional resources + +* [Amazon Bedrock Runtime User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html) +* [Amazon Bedrock Runtime API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html) +* [SDK for Java 2.x Amazon Bedrock Runtime reference](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/bedrock-runtime/package-summary.html) + + + + +--- + +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 \ No newline at end of file diff --git a/javav2/example_code/bedrock-runtime/pom.xml b/javav2/example_code/bedrock-runtime/pom.xml new file mode 100644 index 00000000000..9b1af1a388f --- /dev/null +++ b/javav2/example_code/bedrock-runtime/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + BedrockJ2Project + BedrockJ2Project + 1.0-SNAPSHOT + + UTF-8 + 17 + ${java.version} + ${java.version} + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + ${java.version} + ${java.version} + + + + + + + + software.amazon.awssdk + bom + 2.21.17 + pom + import + + + + + + software.amazon.awssdk + bedrockruntime + + + software.amazon.awssdk + sts + + + org.json + json + 20231013 + + + org.junit.jupiter + junit-jupiter-api + 5.9.2 + test + + + \ No newline at end of file diff --git a/javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/InvokeModel.java b/javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/InvokeModel.java new file mode 100644 index 00000000000..15723434ffc --- /dev/null +++ b/javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/InvokeModel.java @@ -0,0 +1,294 @@ +// snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.] +// snippet-sourcedescription:[InvokeModel.java demonstrates how to invoke a model with Amazon Bedrock.] +// snippet-keyword:[AWS SDK for Java v2] +// snippet-service:[Amazon Bedrock] + +/* + Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + SPDX-License-Identifier: Apache-2.0 +*/ + +package com.example.bedrockruntime; + +// snippet-start:[bedrock-runtime.java2.invoke_model.import] + +import org.json.JSONArray; +import org.json.JSONObject; +import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; +import software.amazon.awssdk.awscore.exception.AwsServiceException; +import software.amazon.awssdk.core.SdkBytes; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient; +import software.amazon.awssdk.services.bedrockruntime.model.BedrockRuntimeException; +import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelRequest; +import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelResponse; + +import java.io.FileOutputStream; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Base64; +import java.util.List; +import java.util.Random; +// snippet-end:[bedrock-runtime.java2.invoke_model.import] + +/** + * Before running this Java V2 code example, set up your development environment, including your credentials. + * + * For more information, see the following documentation topic: + * + * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html + */ +public class InvokeModel { + + private static final Random random = new Random(); + + public static void main(String[] args) { + usageDemo(); + } + + // snippet-start:[bedrock-runtime.java2.invoke_claude.main] + /** + * Invokes the Anthropic Claude 2 model to run an inference based on the provided input. + * + * @param client A Bedrock Runtime client + * @param prompt The prompt that you want Claude to complete. + * @return Inference response from the model. + */ + public static String invokeClaude(BedrockRuntimeClient client, String prompt) { + + try { + + /* + For request/response formats, defaults, and value ranges of Anthropic Claude, see: + https://docs.anthropic.com/claude/reference/complete_post + */ + + // Claude requires you to enclose the prompt as follows: + String enclosedPrompt = "Human: " + prompt + "\n\nAssistant:"; + + JSONObject payload = new JSONObject() + .put("prompt", "Human: " + enclosedPrompt + " Assistant:") + .put("max_tokens_to_sample", 200) + .put("temperature", 0.5) + .put("stop_sequences", List.of("\n\nHuman:")); + + SdkBytes body = SdkBytes.fromUtf8String(payload.toString()); + + InvokeModelRequest request = InvokeModelRequest.builder() + .modelId("anthropic.claude-v2") + .body(body) + .build(); + + InvokeModelResponse response = client.invokeModel(request); + + JSONObject responseBody = new JSONObject(response.body().asUtf8String()); + + String completion = responseBody.getString("completion"); + + return completion; + + } catch (AwsServiceException e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); + } + return null; + + } + // snippet-end:[bedrock-runtime.java2.invoke_claude.main] + + // snippet-start:[bedrock-runtime.java2.invoke_jurassic2.main] + /** + * Invokes the AI21 Labs Jurassic-2 model to run an inference based on the provided input. + * + * @param client A Bedrock Runtime client + * @param prompt The prompt that you want Jurassic to complete. + * @return Inference response from the model. + */ + public static String invokeJurassic2(BedrockRuntimeClient client, String prompt) { + + try { + + /* + For request/response formats, defaults, and value ranges of AI21 Labs Jurassic-2, see: + https://docs.ai21.com/reference/j2-complete-ref + */ + + JSONObject payload = new JSONObject() + .put("prompt", prompt) + .put("maxTokens", 200) + .put("temperature", 0.5); + + SdkBytes body = SdkBytes.fromUtf8String(payload.toString()); + + InvokeModelRequest request = InvokeModelRequest.builder() + .modelId("ai21.j2-mid-v1") + .body(body) + .build(); + + InvokeModelResponse response = client.invokeModel(request); + + JSONObject responseBody = new JSONObject(response.body().asUtf8String()); + + String completion = responseBody + .getJSONArray("completions") + .getJSONObject(0) + .getJSONObject("data") + .getString("text"); + + return completion; + + } catch (AwsServiceException e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); + } + return null; + + } + // snippet-end:[bedrock-runtime.java2.invoke_jurassic2.main] + + // snippet-start:[bedrock-runtime.java2.invoke_stable_diffusion.main] + /** + * Invokes the Stability.ai Stable Diffusion XL model to create an image based on the provided input. + * + * @param client A Bedrock Runtime client + * @param prompt The prompt that guides the Stable Diffusion model. + * @param seed The random noise seed for image generation (use 0 or omit for a random seed). + * @param stylePreset The style preset to guide the image model towards a specific style. + * @return A Base64-encoded string representing the model's inference response as an image. + */ + public static String invokeStableDiffusion(BedrockRuntimeClient client, String prompt, long seed, String stylePreset) { + + try { + + /* + For request/response formats, defaults, and value ranges of Stable Diffusion, see: + https://platform.stability.ai/docs/api-reference#tag/v1generation + */ + + JSONObject payload = new JSONObject() + .put("text_prompts", + new JSONArray() + .put(new JSONObject().put("text", prompt)) + ) + .put("seed", seed); + + if (stylePreset != null && !stylePreset.isEmpty()) { + payload.put("style_preset", stylePreset); + } + + SdkBytes body = SdkBytes.fromUtf8String(payload.toString()); + + InvokeModelRequest request = InvokeModelRequest.builder() + .modelId("stability.stable-diffusion-xl") + .body(body) + .build(); + + InvokeModelResponse response = client.invokeModel(request); + + JSONObject responseBody = new JSONObject(response.body().asUtf8String()); + + String base64ImageData = responseBody + .getJSONArray("artifacts") + .getJSONObject(0) + .getString("base64"); + + return base64ImageData; + + } catch (AwsServiceException e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); + } + return null; + + } + // snippet-end:[bedrock-runtime.java2.invoke_stable_diffusion.main] + + + /** + * Demonstrates the invocation of various large-language and image generation models: + * Anthropic Claude 2, AI21 Labs Jurassic-2, and Stability.ai Stable Diffusion XL. + */ + private static void usageDemo() { + Region region = Region.US_EAST_1; + BedrockRuntimeClient client = BedrockRuntimeClient.builder() + .region(region) + .credentialsProvider(ProfileCredentialsProvider.create()) + .build(); + + System.out.println(String.format("%0" + 88 + "d", 0).replace("0", "-")); + System.out.println("Welcome to the Amazon Bedrock Runtime demo."); + + String textPrompt = "In one sentence, what is a large-language model?"; + invoke(client, "anthropic.claude-v2", textPrompt); + invoke(client, "ai21.j2-mid-v1", textPrompt); + + String imagePrompt = "A sunset over the ocean"; + String stylePreset = "photographic"; + invoke(client, "stability.stable-diffusion-xl", imagePrompt, stylePreset); + } + + private static void invoke(BedrockRuntimeClient client, String modelId, String prompt) { + invoke(client, modelId, prompt, null); + } + + private static void invoke(BedrockRuntimeClient client, String modelId, String prompt, String stylePreset) { + System.out.println(new String(new char[88]).replace("\0", "-")); + System.out.println("Invoking: " + modelId); + System.out.println("Prompt: " + prompt); + + try { + String completion; + if ("anthropic.claude-v2".equals(modelId)) { + completion = invokeClaude(client, prompt); + System.out.println("Completion: " + completion.trim()); + } else if ("ai21.j2-mid-v1".equals(modelId)) { + completion = invokeJurassic2(client, prompt); + System.out.println("Completion: " + completion.trim()); + } else if ("stability.stable-diffusion-xl".equals(modelId)) { + long seed = (random.nextLong() & 0xFFFFFFFFL); + String base64ImageData = invokeStableDiffusion(client, prompt, seed, stylePreset); + String imagePath = saveImage(base64ImageData); + System.out.println("The generated image has been saved to " + imagePath); + } + } catch (BedrockRuntimeException e) { + System.out.println("Couldn't invoke model " + modelId + ": " + e.getMessage()); + throw e; + } + } + + private static String saveImage(String base64ImageData) { + try { + + String directory = "output"; + + URI uri = InvokeModel.class.getProtectionDomain().getCodeSource().getLocation().toURI(); + Path outputPath = Paths.get(uri).getParent().getParent().resolve(directory); + + if (!Files.exists(outputPath)) { + Files.createDirectories(outputPath); + } + + int i = 1; + String fileName; + do { + fileName = String.format("image_%d.png", i); + i++; + } while (Files.exists(outputPath.resolve(fileName))); + + byte[] imageBytes = Base64.getDecoder().decode(base64ImageData); + + Path filePath = outputPath.resolve(fileName); + try (FileOutputStream fileOutputStream = new FileOutputStream(filePath.toFile())) { + fileOutputStream.write(imageBytes); + } + + return filePath.toString(); + } catch (Exception e) { + System.err.println(e.getMessage()); + System.exit(1); + } + return null; + } +} diff --git a/javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/InvokeModelWithResponseStream.java b/javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/InvokeModelWithResponseStream.java new file mode 100644 index 00000000000..9acb0756c5b --- /dev/null +++ b/javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/InvokeModelWithResponseStream.java @@ -0,0 +1,99 @@ +// snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.] +// snippet-sourcedescription:[InvokeModel.java demonstrates how to invoke a model with Amazon Bedrock and process the response stream.] +// snippet-keyword:[AWS SDK for Java v2] +// snippet-service:[Amazon Bedrock] + +/* + Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + SPDX-License-Identifier: Apache-2.0 +*/ + +package com.example.bedrockruntime; + +// snippet-start:[bedrock-runtime.java2.invoke_model_with_response_stream.import] +import org.json.JSONObject; +import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; +import software.amazon.awssdk.core.SdkBytes; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeAsyncClient; +import software.amazon.awssdk.services.bedrockruntime.model.BedrockRuntimeException; +import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelWithResponseStreamRequest; +import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelWithResponseStreamResponseHandler; +import software.amazon.awssdk.services.bedrockruntime.model.ResponseStream; + +import java.util.concurrent.CompletableFuture; +// snippet-end:[bedrock-runtime.java2.invoke_model_with_response_stream.import] + +/** + * Before running this Java V2 code example, set up your development environment, including your credentials. + * + * For more information, see the following documentation topic: + * + * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html + */ +public class InvokeModelWithResponseStream { + + public static void main(String[] args) { + + Region region = Region.US_EAST_1; + BedrockRuntimeAsyncClient bedrockRuntimeAsyncClient = BedrockRuntimeAsyncClient.builder() + .region(region) + .credentialsProvider(ProfileCredentialsProvider.create()) + .build(); + + String prompt = "What is a large-language model?"; + + invokeModel(bedrockRuntimeAsyncClient, prompt); + } + + // snippet-start:[bedrock-runtime.java2.invoke_model_with_response_stream.main] + public static String invokeModel(BedrockRuntimeAsyncClient bedrockRuntimeAsyncClient, String prompt) { + + try { + + double temperature = 0.8; + int maxTokensToSample = 300; + + JSONObject payload = new JSONObject() + .put("prompt", "Human: " + prompt + " Assistant:") + .put("temperature", temperature) + .put("max_tokens_to_sample", maxTokensToSample); + + SdkBytes body = SdkBytes.fromUtf8String(payload.toString()); + + InvokeModelWithResponseStreamRequest request = InvokeModelWithResponseStreamRequest.builder() + .modelId("anthropic.claude-v2") + .contentType("application/json") + .accept("application/json") + .body(body) + .build(); + + InvokeModelWithResponseStreamResponseHandler.Visitor visitor = + InvokeModelWithResponseStreamResponseHandler.Visitor.builder() + .onDefault((event) -> System.out.println("\n\nDefault: " + event.toString())) + .onChunk((chunk) -> { + JSONObject json = new JSONObject(chunk.bytes().asUtf8String()); + System.out.print(json.getString("completion")); + }) + .build(); + + InvokeModelWithResponseStreamResponseHandler responseHandler = + InvokeModelWithResponseStreamResponseHandler.builder() + .onEventStream((stream) -> stream.subscribe((ResponseStream e) -> e.accept(visitor))) + .onComplete(() -> System.out.println("\n\nCompleted streaming response.")) + .onError((e) -> System.out.println("\n\nError: " + e.getMessage())) + .build(); + + CompletableFuture futureResponse = bedrockRuntimeAsyncClient + .invokeModelWithResponseStream(request, responseHandler); + + futureResponse.join(); + + } catch (BedrockRuntimeException e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); + } + return null; + } + // snippet-end:[bedrock-runtime.java2.invoke_model_with_response_stream.main] +} diff --git a/javav2/example_code/bedrock-runtime/src/test/java/BedrockRuntimeTest.java b/javav2/example_code/bedrock-runtime/src/test/java/BedrockRuntimeTest.java new file mode 100644 index 00000000000..85dabbcbf33 --- /dev/null +++ b/javav2/example_code/bedrock-runtime/src/test/java/BedrockRuntimeTest.java @@ -0,0 +1,109 @@ +/* + Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + SPDX-License-Identifier: Apache-2.0 +*/ + +import com.example.bedrockruntime.InvokeModel; +import com.example.bedrockruntime.InvokeModelWithResponseStream; +import org.junit.jupiter.api.*; +import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeAsyncClient; +import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient; + +import java.lang.reflect.Method; + +import static org.junit.jupiter.api.Assertions.*; + +@TestInstance(TestInstance.Lifecycle.PER_METHOD) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class BedrockRuntimeTest { + + @Test + @Order(1) + @Tag("IntegrationTest") + void InvokeClaude() { + + try (BedrockRuntimeClient client = BedrockRuntimeClient.builder() + .region(Region.US_EAST_1) + .credentialsProvider(ProfileCredentialsProvider.create()) + .build()) { + + String prompt = "In one sentence, what is a large-language model?"; + + String completion = InvokeModel.invokeClaude(client, prompt); + + assertNotNull(completion, "The completion is null"); + assertFalse(completion.trim().isEmpty(), "The completion is empty"); + + System.out.printf("Test %d passed.%n", getTestNumber(new Object(){}.getClass().getEnclosingMethod())); + } + } + + @Test + @Order(2) + @Tag("IntegrationTest") + void InvokeJurassic2() { + + try (BedrockRuntimeClient client = BedrockRuntimeClient.builder() + .region(Region.US_EAST_1) + .credentialsProvider(ProfileCredentialsProvider.create()) + .build()) { + + String prompt = "In one sentence, what is a large-language model?"; + + String completion = InvokeModel.invokeJurassic2(client, prompt); + + assertNotNull(completion, "The completion is null"); + assertFalse(completion.trim().isEmpty(), "The completion is empty"); + + System.out.printf("Test %d passed.%n", getTestNumber(new Object(){}.getClass().getEnclosingMethod())); + } + } + + @Test + @Order(3) + @Tag("IntegrationTest") + void InvokeStableDiffusion() { + + try (BedrockRuntimeClient client = BedrockRuntimeClient.builder() + .region(Region.US_EAST_1) + .credentialsProvider(ProfileCredentialsProvider.create()) + .build()) { + + String prompt = "A sunset over the ocean"; + String stylePreset = "cinematic"; + long seed = (0); + + String result = InvokeModel.invokeStableDiffusion(client, prompt, seed, stylePreset); + + assertNotNull(result, "The result is null"); + assertFalse(result.trim().isEmpty(), "The result is empty"); + + System.out.printf("Test %d passed.%n", getTestNumber(new Object(){}.getClass().getEnclosingMethod())); + } + } + + @Test + @Order(4) + @Tag("IntegrationTest") + void InvokeModelWithResponseStream() { + + try (BedrockRuntimeAsyncClient bedrockRuntime = BedrockRuntimeAsyncClient.builder() + .region(Region.US_EAST_1) + .credentialsProvider(ProfileCredentialsProvider.create()) + .build()) { + + String prompt = "In one sentence, what is a large-language model?"; + + assertDoesNotThrow(() -> InvokeModelWithResponseStream.invokeModel(bedrockRuntime, prompt)); + + System.out.println("Test 2 passed."); + } + } + + private int getTestNumber(Method testMethod) { + Order order = testMethod.getAnnotation(Order.class); + return order != null ? order.value() : 0; + } +} diff --git a/javav2/example_code/bedrock/Readme.md b/javav2/example_code/bedrock/Readme.md new file mode 100644 index 00000000000..3ec9407a03c --- /dev/null +++ b/javav2/example_code/bedrock/Readme.md @@ -0,0 +1,77 @@ + +# Amazon Bedrock code examples for the SDK for Java 2.x + +## Overview + +Shows how to use the AWS SDK for Java 2.x to work with Amazon Bedrock. + + + + +*Amazon Bedrock enables you to build and scale generative AI applications with foundation models.* + +## ⚠ Important + +* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/?aws-products-pricing.sort-by=item.additionalFields.productNameLowercase&aws-products-pricing.sort-order=asc&awsf.Free%20Tier%20Type=*all&awsf.tech-category=*all) and [Free Tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all). +* Running the tests might result in charges to your AWS account. +* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). +* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). + + + + +## Code examples + +### Prerequisites + +For prerequisites, see the [README](../../README.md#Prerequisites) in the `javav2` folder. + + + +> ⚠ You must request access to a model before you can use it. If you try to use the model (with the API or console) before you have requested access to it, you will receive an error message. For more information, see [Model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html). + + +### Single actions + +Code excerpts that show you how to call individual service functions. + +* [List available Amazon Bedrock foundation models](src/main/java/com/example/bedrock/ListFoundationModels.java#L42) (`ListFoundationModels`) + +## Run the examples + +### Instructions + + + + + + + + +### Tests + +⚠ Running tests might result in charges to your AWS account. + + +To find instructions for running these tests, see the [README](../../README.md#Tests) +in the `javav2` folder. + + + + + + +## Additional resources + +* [Amazon Bedrock User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html) +* [Amazon Bedrock API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html) +* [SDK for Java 2.x Amazon Bedrock reference](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/bedrock/package-summary.html) + + + + +--- + +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 \ No newline at end of file diff --git a/javav2/example_code/bedrock/pom.xml b/javav2/example_code/bedrock/pom.xml new file mode 100644 index 00000000000..dffc92ee63e --- /dev/null +++ b/javav2/example_code/bedrock/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + BedrockJ2Project + BedrockJ2Project + 1.0-SNAPSHOT + + UTF-8 + 17 + ${java.version} + ${java.version} + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + ${java.version} + ${java.version} + + + + + + + + software.amazon.awssdk + bom + 2.21.17 + pom + import + + + + + + software.amazon.awssdk + sts + + + software.amazon.awssdk + bedrock + + + org.json + json + 20231013 + + + org.junit.jupiter + junit-jupiter-api + 5.9.2 + test + + + \ No newline at end of file diff --git a/javav2/example_code/bedrock/src/main/java/com/example/bedrock/ListFoundationModels.java b/javav2/example_code/bedrock/src/main/java/com/example/bedrock/ListFoundationModels.java new file mode 100644 index 00000000000..2ab239ad066 --- /dev/null +++ b/javav2/example_code/bedrock/src/main/java/com/example/bedrock/ListFoundationModels.java @@ -0,0 +1,65 @@ +// snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.] +// snippet-sourcedescription:[ListFoundationModels.java demonstrates how to obtain a list of the available foundation models.] +// snippet-keyword:[AWS SDK for Java v2] +// snippet-service:[Amazon Bedrock] + +/* + Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + SPDX-License-Identifier: Apache-2.0 +*/ + +package com.example.bedrock; + +// snippet-start:[bedrock.java2.list_foundation_models.import] +import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.bedrock.BedrockClient; +import software.amazon.awssdk.services.bedrock.model.BedrockException; +import software.amazon.awssdk.services.bedrock.model.FoundationModelSummary; +import software.amazon.awssdk.services.bedrock.model.ListFoundationModelsRequest; +import software.amazon.awssdk.services.bedrock.model.ListFoundationModelsResponse; +import java.util.List; +// snippet-end:[bedrock.java2.list_foundation_models.import] + +/** + * Before running this Java V2 code example, set up your development environment, including your credentials. + * + * For more information, see the following documentation topic: + * + * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html + */ +public class ListFoundationModels { + public static void main(String[] args) { + Region region = Region.US_EAST_1; + BedrockClient bedrockClient = BedrockClient.builder() + .region(region) + .credentialsProvider(ProfileCredentialsProvider.create()) + .build(); + + listFoundationModels(bedrockClient); + } + + // snippet-start:[bedrock.java2.list_foundation_models.main] + public static void listFoundationModels(BedrockClient bedrockClient) { + + try { + ListFoundationModelsRequest request = ListFoundationModelsRequest.builder().build(); + + ListFoundationModelsResponse response = bedrockClient.listFoundationModels(request); + + List models = response.modelSummaries(); + + for (FoundationModelSummary model : models) { + System.out.println("Model ID: " + model.modelId()); + System.out.println("Provider: " + model.providerName()); + System.out.println("Name: " + model.modelName()); + System.out.println(); + } + + } catch (BedrockException e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); + } + } + // snippet-end:[bedrock.java2.list_foundation_models.main] +} diff --git a/javav2/example_code/bedrock/src/test/java/BedrockTest.java b/javav2/example_code/bedrock/src/test/java/BedrockTest.java new file mode 100644 index 00000000000..852cab86d56 --- /dev/null +++ b/javav2/example_code/bedrock/src/test/java/BedrockTest.java @@ -0,0 +1,36 @@ +/* + Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + SPDX-License-Identifier: Apache-2.0 +*/ + +import com.example.bedrock.ListFoundationModels; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.bedrock.BedrockClient; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +@TestInstance(TestInstance.Lifecycle.PER_METHOD) +public class BedrockTest { + + private static BedrockClient bedrock; + + @BeforeAll + public static void setUp() { + bedrock = BedrockClient.builder() + .region(Region.US_EAST_1) + .credentialsProvider(ProfileCredentialsProvider.create()) + .build(); + } + + @Test + @Tag("IntegrationTest") + void listFoundationModels() { + assertDoesNotThrow(() -> ListFoundationModels.listFoundationModels(bedrock)); + System.out.println("Test 1 passed."); + } +} diff --git a/python/example_code/bedrock-runtime/.gitignore b/python/example_code/bedrock-runtime/.gitignore new file mode 100644 index 00000000000..7e902e850dc --- /dev/null +++ b/python/example_code/bedrock-runtime/.gitignore @@ -0,0 +1 @@ +output/* \ No newline at end of file diff --git a/python/example_code/bedrock-runtime/README.md b/python/example_code/bedrock-runtime/README.md new file mode 100644 index 00000000000..fc40dbf7d95 --- /dev/null +++ b/python/example_code/bedrock-runtime/README.md @@ -0,0 +1,90 @@ + +# Amazon Bedrock Runtime code examples for the SDK for Python + +## Overview + +Shows how to use the AWS SDK for Python (Boto3) to work with Amazon Bedrock Runtime. + + + + +*Amazon Bedrock Runtime is a fully managed service that makes it easy to use foundation models from third-party providers and Amazon.* + +## ⚠ Important + +* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/?aws-products-pricing.sort-by=item.additionalFields.productNameLowercase&aws-products-pricing.sort-order=asc&awsf.Free%20Tier%20Type=*all&awsf.tech-category=*all) and [Free Tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all). +* Running the tests might result in charges to your AWS account. +* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). +* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). + + + + +## Code examples + +### Prerequisites + +For prerequisites, see the [README](../../README.md#Prerequisites) in the `python` folder. + +Install the packages required by these examples by running the following in a virtual environment: + +``` +python -m pip install -r requirements.txt +``` + + +> ⚠ You must request access to a model before you can use it. If you try to use the model (with the API or console) before you have requested access to it, you will receive an error message. For more information, see [Model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html). + + +### Single actions + +Code excerpts that show you how to call individual service functions. + +* [Invoke AI21 Labs Jurassic-2 on Amazon Bedrock](bedrock_runtime_wrapper.py#L81) (`InvokeModel`) +* [Invoke Anthropic Claude 2 on Amazon Bedrock](bedrock_runtime_wrapper.py#L39) (`InvokeModel`) +* [Invoke Stability.ai Stable Diffusion XL on Amazon Bedrock](bedrock_runtime_wrapper.py#L119) (`InvokeModel`) +* [Invoke a model on Amazon Bedrock with a response stream](bedrock_runtime_wrapper.py#L164) (`InvokeModelWithResponseStream`) + +## Run the examples + +### Instructions + + + + +Run the example by executing the following command inside the `example_code` folder: + +``` +python bedrock_runtime_wrapper.py +``` + + + + +### Tests + +⚠ Running tests might result in charges to your AWS account. + + +To find instructions for running these tests, see the [README](../../README.md#Tests) +in the `python` folder. + + + + + + +## Additional resources + +* [Amazon Bedrock Runtime User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html) +* [Amazon Bedrock Runtime API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html) +* [SDK for Python Amazon Bedrock Runtime reference](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/bedrock-runtime.html) + + + + +--- + +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 \ No newline at end of file diff --git a/python/example_code/bedrock-runtime/bedrock_runtime_wrapper.py b/python/example_code/bedrock-runtime/bedrock_runtime_wrapper.py new file mode 100644 index 00000000000..10d258ba662 --- /dev/null +++ b/python/example_code/bedrock-runtime/bedrock_runtime_wrapper.py @@ -0,0 +1,305 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +""" +Purpose + +Shows how to use the AWS SDK for Python (Boto3) with the Amazon Bedrock Runtime client +to run inferences using Bedrock models. +""" + +import asyncio +import base64 +import json +import logging +import os +import random + +import boto3 +from botocore.exceptions import ClientError + +logger = logging.getLogger(__name__) + + +# snippet-start:[python.example_code.bedrock-runtime.BedrockRuntimeWrapper.class] +# snippet-start:[python.example_code.bedrock-runtime.BedrockRuntimeWrapper.decl] +class BedrockRuntimeWrapper: + """Encapsulates Amazon Bedrock Runtime actions.""" + + def __init__(self, bedrock_runtime_client): + """ + :param bedrock_runtime_client: A low-level client representing Amazon Bedrock Runtime. + Describes the API operations for running inference using + Bedrock models. + """ + self.bedrock_runtime_client = bedrock_runtime_client + + # snippet-end:[python.example_code.bedrock-runtime.BedrockRuntimeWrapper.decl] + + # snippet-start:[python.example_code.bedrock-runtime.InvokeAnthropicClaude] + def invoke_claude(self, prompt): + """ + Invokes the Anthropic Claude 2 model to run an inference using the input + provided in the request body. + + :param prompt: The prompt that you want Claude to complete. + :return: Inference response from the model. + """ + + try: + # The different model providers have individual request and response formats. + # For the format, ranges, and default values for Anthropic Claude, refer to: + # https://docs.anthropic.com/claude/reference/complete_post + + # Claude requires you to enclose the prompt as follows: + enclosed_prompt = "Human: " + prompt + "\n\nAssistant:" + + body = { + "prompt": enclosed_prompt, + "max_tokens_to_sample": 200, + "temperature": 0.5, + "stop_sequences": ["\n\nHuman:"], + } + + response = self.bedrock_runtime_client.invoke_model( + modelId="anthropic.claude-v2", body=json.dumps(body) + ) + + response_body = json.loads(response["body"].read()) + completion = response_body["completion"] + + return completion + + except ClientError: + logger.error("Couldn't invoke Anthropic Claude") + raise + + # snippet-end:[python.example_code.bedrock-runtime.InvokeAnthropicClaude] + + # snippet-start:[python.example_code.bedrock-runtime.InvokeAi21Jurassic2] + def invoke_jurassic2(self, prompt): + """ + Invokes the AI21 Labs Jurassic-2 large-language model to run an inference + using the input provided in the request body. + + :param prompt: The prompt that you want Jurassic-2 to complete. + :return: Inference response from the model. + """ + + try: + # The different model providers have individual request and response formats. + # For the format, ranges, and default values for AI21 Labs Jurassic-2, refer to: + # https://docs.ai21.com/reference/j2-complete-ref + + body = { + "prompt": prompt, + "temperature": 0.5, + "maxTokens": 200, + } + + response = self.bedrock_runtime_client.invoke_model( + modelId="ai21.j2-mid-v1", body=json.dumps(body) + ) + + response_body = json.loads(response["body"].read()) + completion = response_body["completions"][0]["data"]["text"] + + return completion + + except ClientError: + logger.error("Couldn't invoke Anthropic Claude 2") + raise + + # snippet-end:[python.example_code.bedrock-runtime.InvokeAi21Jurassic2] + + # snippet-start:[python.example_code.bedrock-runtime.InvokeStableDiffusion] + def invoke_stable_diffusion(self, prompt, seed, style_preset=None): + """ + Invokes the Stability.ai Stable Diffusion XL model to create an image using + the input provided in the request body. + + :param prompt: The prompt that you want Stable Diffusion to complete. + :param seed: Random noise seed (omit this option or use 0 for a random seed) + :param style_preset: Pass in a style preset to guide the image model towards + a particular style. + :return: Base64-encoded inference response from the model. + """ + + try: + # The different model providers have individual request and response formats. + # For the format, ranges, and available style_presets of Stable Diffusion models refer to: + # https://platform.stability.ai/docs/api-reference#tag/v1generation + + body = { + "text_prompts": [{"text": prompt}], + "seed": seed, + "cfg_scale": 10, + "steps": 30, + } + + if style_preset: + body["style_preset"] = style_preset + + response = self.bedrock_runtime_client.invoke_model( + modelId="stability.stable-diffusion-xl", body=json.dumps(body) + ) + + response_body = json.loads(response["body"].read()) + base64_image_data = response_body["artifacts"][0]["base64"] + + return base64_image_data + + except ClientError: + logger.error("Couldn't invoke Stable Diffusion XL") + raise + + # snippet-end:[python.example_code.bedrock-runtime.InvokeStableDiffusion] + + # snippet-start:[python.example_code.bedrock-runtime.InvokeModelWithResponseStream] + async def invoke_model_with_response_stream(self, prompt): + """ + Invokes the Anthropic Claude 2 model to run an inference and process the response stream. + + :param prompt: The prompt that you want Claude to complete. + :return: Inference response from the model. + """ + + try: + # The different model providers have individual request and response formats. + # For the format, ranges, and default values for Anthropic Claude, refer to: + # https://docs.anthropic.com/claude/reference/complete_post + + # Claude requires you to enclose the prompt as follows: + enclosed_prompt = "Human: " + prompt + "\n\nAssistant:" + + body = { + "prompt": enclosed_prompt, + "max_tokens_to_sample": 1024, + "temperature": 0.5, + "stop_sequences": ["\n\nHuman:"], + } + + response = self.bedrock_runtime_client.invoke_model_with_response_stream( + modelId="anthropic.claude-v2", body=json.dumps(body) + ) + + stream = response.get("body") + + if stream: + for event in stream: + chunk = event.get("chunk") + if chunk: + chunk_obj = json.loads(chunk.get("bytes").decode()) + text = chunk_obj["completion"] + yield text + + except ClientError: + logger.error("Couldn't invoke Anthropic Claude") + raise + + # snippet-end:[python.example_code.bedrock-runtime.InvokeModelWithResponseStream] + + +def save_image(base64_image_data): + directory = "output" + + if not os.path.exists(directory): + os.makedirs(directory) + + i = 1 + while os.path.exists(os.path.join(directory, f"image_{i}.png")): + i += 1 + + image_data = base64.b64decode(base64_image_data) + + file_path = os.path.join(directory, f"image_{i}.png") + with open(file_path, "wb") as file: + file.write(image_data) + + return file_path + + +def invoke(wrapper, model_id, prompt, style_preset=None): + print("-" * 88) + print(f"Invoking: {model_id}") + print("Prompt: " + prompt) + + try: + if model_id == "anthropic.claude-v2": + completion = wrapper.invoke_claude(prompt) + print("Completion: " + completion.strip()) + + elif model_id == "ai21.j2-mid-v1": + completion = wrapper.invoke_jurassic2(prompt) + print("Completion: " + completion.strip()) + + elif model_id == "stability.stable-diffusion-xl": + seed = random.randint(0, 4294967295) + base64_image_data = wrapper.invoke_stable_diffusion( + prompt, seed, style_preset + ) + image_path = save_image(base64_image_data) + print(f"The generated image has been saved to {image_path}") + + except ClientError: + logger.exception("Couldn't invoke model %s", model_id) + raise + + +async def invoke_with_response_stream(wrapper, model_id, prompt): + print("-" * 88) + print(f"Invoking: {model_id} with response stream") + print("Prompt: " + prompt) + print("\nResponse stream:") + + try: + async for completion in wrapper.invoke_model_with_response_stream(prompt): + print(completion, end="") + + except ClientError: + logger.exception("Couldn't invoke model %s", model_id) + raise + + print() + + +def usage_demo(): + """ + Demonstrates the invocation of various large-language and image generation models: + Anthropic Claude 2, AI21 Labs Jurassic-2, and Stability.ai Stable Diffusion XL. + """ + logging.basicConfig(level=logging.INFO) + print("-" * 88) + print("Welcome to the Amazon Bedrock Runtime demo.") + print("-" * 88) + + client = boto3.client(service_name="bedrock-runtime", region_name="us-east-1") + + wrapper = BedrockRuntimeWrapper(client) + + text_generation_prompt = "Hi, write a paragraph about yourself" + image_generation_prompt = "A sunset over the ocean" + image_style_preset = "photographic" + + invoke(wrapper, "anthropic.claude-v2", text_generation_prompt) + + invoke(wrapper, "ai21.j2-mid-v1", text_generation_prompt) + + asyncio.run( + invoke_with_response_stream( + wrapper, "anthropic.claude-v2", text_generation_prompt + ) + ) + + invoke( + wrapper, + "stability.stable-diffusion-xl", + image_generation_prompt, + image_style_preset, + ) + + +if __name__ == "__main__": + usage_demo() + +# snippet-end:[python.example_code.bedrock-runtime.BedrockRuntimeWrapper.class] diff --git a/python/example_code/bedrock-runtime/requirements.txt b/python/example_code/bedrock-runtime/requirements.txt new file mode 100644 index 00000000000..fd7722c4401 --- /dev/null +++ b/python/example_code/bedrock-runtime/requirements.txt @@ -0,0 +1,13 @@ +boto3==1.28.83 +botocore==1.31.83 +colorama==0.4.6 +iniconfig==2.0.0 +jmespath==1.0.1 +packaging==23.2 +pluggy==1.3.0 +pytest==7.4.3 +pytest-asyncio==0.21.1 +python-dateutil==2.8.2 +s3transfer==0.7.0 +six==1.16.0 +urllib3==2.0.7 diff --git a/python/example_code/bedrock-runtime/test/conftest.py b/python/example_code/bedrock-runtime/test/conftest.py new file mode 100644 index 00000000000..615c63b51fa --- /dev/null +++ b/python/example_code/bedrock-runtime/test/conftest.py @@ -0,0 +1,12 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +""" +Contains common test fixtures used to run Amazon Bedrock tests. +""" + +import sys + +# This is needed so Python can find test_tools on the path. +sys.path.append("../..") +from test_tools.fixtures.common import * diff --git a/python/example_code/bedrock-runtime/test/test_bedrock_runtime_wrapper.py b/python/example_code/bedrock-runtime/test/test_bedrock_runtime_wrapper.py new file mode 100644 index 00000000000..68e9da62556 --- /dev/null +++ b/python/example_code/bedrock-runtime/test/test_bedrock_runtime_wrapper.py @@ -0,0 +1,102 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +""" +Unit tests for bedrock_runtime_wrapper.py. +""" + +import pytest +import random + +import boto3 +from botocore.exceptions import ClientError + +from bedrock_runtime_wrapper import BedrockRuntimeWrapper + + +@pytest.mark.parametrize("error_code", [None, "ClientError"]) +def test_invoke_claude(make_stubber, error_code): + bedrock_runtime = boto3.client( + service_name="bedrock-runtime", region_name="us-east-1" + ) + bedrock_runtime_stubber = make_stubber(bedrock_runtime) + wrapper = BedrockRuntimeWrapper(bedrock_runtime) + + prompt = "Hey, how are you?" + + bedrock_runtime_stubber.stub_invoke_claude(prompt, error_code=error_code) + + if error_code is None: + got_completion = wrapper.invoke_claude(prompt) + assert len(got_completion) > 0 + else: + with pytest.raises(ClientError) as exc_info: + wrapper.invoke_claude(prompt) + assert exc_info.value.response["Error"]["Code"] == error_code + + +@pytest.mark.parametrize("error_code", [None, "ClientError"]) +def test_invoke_jurassic2(make_stubber, error_code): + bedrock_runtime = boto3.client( + service_name="bedrock-runtime", region_name="us-east-1" + ) + bedrock_runtime_stubber = make_stubber(bedrock_runtime) + wrapper = BedrockRuntimeWrapper(bedrock_runtime) + + prompt = "Hey, how are you?" + + bedrock_runtime_stubber.stub_invoke_jurassic2(prompt, error_code=error_code) + + if error_code is None: + got_completion = wrapper.invoke_jurassic2(prompt) + assert len(got_completion) > 0 + else: + with pytest.raises(ClientError) as exc_info: + wrapper.invoke_jurassic2(prompt) + assert exc_info.value.response["Error"]["Code"] == error_code + + +@pytest.mark.asyncio +@pytest.mark.parametrize("error_code", ["ClientError"]) +async def test_invoke_model_with_response_stream(make_stubber, error_code): + bedrock_runtime = boto3.client( + service_name="bedrock-runtime", region_name="us-east-1" + ) + bedrock_runtime_stubber = make_stubber(bedrock_runtime) + wrapper = BedrockRuntimeWrapper(bedrock_runtime) + + prompt = "Hey, how are you?" + + bedrock_runtime_stubber.stub_invoke_model_with_response_stream( + prompt, error_code=error_code + ) + + if error_code: + with pytest.raises(ClientError): + async for _ in wrapper.invoke_model_with_response_stream(prompt): + assert exc_info.value.response["Error"]["Code"] == error_code + + +@pytest.mark.parametrize("error_code", [None, "ClientError"]) +def test_invoke_stable_diffusion(make_stubber, error_code): + bedrock_runtime = boto3.client( + service_name="bedrock-runtime", region_name="us-east-1" + ) + bedrock_runtime_stubber = make_stubber(bedrock_runtime) + wrapper = BedrockRuntimeWrapper(bedrock_runtime) + + prompt = "A sunset over the ocean" + style_preset = "cinematic" + seed = random.randint(0, 4294967295) + + bedrock_runtime_stubber.stub_invoke_stable_diffusion( + prompt, style_preset, seed, error_code=error_code + ) + + if error_code is None: + got_completion = wrapper.invoke_stable_diffusion(prompt, seed, style_preset) + assert len(got_completion) > 0 + else: + with pytest.raises(ClientError) as exc_info: + wrapper.invoke_stable_diffusion(prompt, seed, style_preset) + assert exc_info.value.response["Error"]["Code"] == error_code diff --git a/python/example_code/bedrock/README.md b/python/example_code/bedrock/README.md new file mode 100644 index 00000000000..b783d059d82 --- /dev/null +++ b/python/example_code/bedrock/README.md @@ -0,0 +1,87 @@ + +# Amazon Bedrock code examples for the SDK for Python + +## Overview + +Shows how to use the AWS SDK for Python (Boto3) to work with Amazon Bedrock. + + + + +*Amazon Bedrock enables you to build and scale generative AI applications with foundation models.* + +## ⚠ Important + +* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/?aws-products-pricing.sort-by=item.additionalFields.productNameLowercase&aws-products-pricing.sort-order=asc&awsf.Free%20Tier%20Type=*all&awsf.tech-category=*all) and [Free Tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all). +* Running the tests might result in charges to your AWS account. +* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). +* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). + + + + +## Code examples + +### Prerequisites + +For prerequisites, see the [README](../../README.md#Prerequisites) in the `python` folder. + +Install the packages required by these examples by running the following in a virtual environment: + +``` +python -m pip install -r requirements.txt +``` + + +> ⚠ You must request access to a model before you can use it. If you try to use the model (with the API or console) before you have requested access to it, you will receive an error message. For more information, see [Model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html). + + +### Single actions + +Code excerpts that show you how to call individual service functions. + +* [List available Amazon Bedrock foundation models](bedrock_wrapper.py#L33) (`ListFoundationModels`) + +## Run the examples + +### Instructions + + + + +Run the example by executing the following command inside the `example_code` folder: + +``` +python bedrock_wrapper.py +``` + + + + +### Tests + +⚠ Running tests might result in charges to your AWS account. + + +To find instructions for running these tests, see the [README](../../README.md#Tests) +in the `python` folder. + + + + + + +## Additional resources + +* [Amazon Bedrock User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html) +* [Amazon Bedrock API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html) +* [SDK for Python Amazon Bedrock reference](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/bedrock.html) + + + + +--- + +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 \ No newline at end of file diff --git a/python/example_code/bedrock/bedrock_wrapper.py b/python/example_code/bedrock/bedrock_wrapper.py new file mode 100644 index 00000000000..58057d9d294 --- /dev/null +++ b/python/example_code/bedrock/bedrock_wrapper.py @@ -0,0 +1,91 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +""" +Purpose + +Shows how to use the AWS SDK for Python (Boto3) with Amazon Bedrock to manage +Bedrock models. +""" + +import logging +import boto3 +from botocore.exceptions import ClientError + +logger = logging.getLogger(__name__) + + +# snippet-start:[python.example_code.bedrock.BedrockWrapper.class] +# snippet-start:[python.example_code.bedrock.BedrockWrapper.decl] +class BedrockWrapper: + """Encapsulates Amazon Bedrock foundation model actions.""" + + def __init__(self, bedrock_client): + """ + :param bedrock_client: A Boto3 Amazon Bedrock client, which is a low-level client that + represents Amazon Bedrock and describes the API operations for + creating and managing Bedrock models. + """ + self.bedrock_client = bedrock_client + + # snippet-end:[python.example_code.bedrock.BedrockWrapper.decl] + + # snippet-start:[python.example_code.bedrock.ListFoundationModels] + def list_foundation_models(self): + """ + List the available Amazon Bedrock foundation models. + + :return: The list of available bedrock foundation models. + """ + + try: + response = self.bedrock_client.list_foundation_models() + models = response["modelSummaries"] + logger.info("Got %s foundation models.", len(models)) + except ClientError: + logger.error("Couldn't list foundation models.") + raise + else: + return models + + # snippet-end:[python.example_code.bedrock.ListFoundationModels] + + +# snippet-end:[python.example_code.bedrock.BedrockWrapper.class] + + +def usage_demo(): + """ + Shows how to list the available foundation models. + This demonstration gets the list of available foundation models and + prints their respective summaries. + """ + logging.basicConfig(level=logging.INFO) + print("-" * 88) + print("Welcome to the Amazon Bedrock demo.") + print("-" * 88) + + bedrock_client = boto3.client(service_name="bedrock", region_name="us-east-1") + + wrapper = BedrockWrapper(bedrock_client) + + print("Listing the available Bedrock foundation models.") + + try: + for model in wrapper.list_foundation_models(): + print("\n" + "=" * 42) + print(f' Model: {model["modelId"]}') + print("-" * 42) + print(f' Name: {model["modelName"]}') + print(f' Provider: {model["providerName"]}') + print(f' Input modalities: {model["inputModalities"]}') + print(f' Output modalities: {model["outputModalities"]}') + print(f' Supported inference types: {model["inferenceTypesSupported"]}') + print("=" * 42) + except ClientError: + logger.exception("Couldn't list Bedrock foundation models.") + raise + + +if __name__ == "__main__": + usage_demo() diff --git a/python/example_code/bedrock/requirements.txt b/python/example_code/bedrock/requirements.txt new file mode 100644 index 00000000000..6cc24ac40f7 --- /dev/null +++ b/python/example_code/bedrock/requirements.txt @@ -0,0 +1,4 @@ +boto3==1.28.83 +botocore==1.31.83 +pytest==7.4.3 + diff --git a/python/example_code/bedrock/test/conftest.py b/python/example_code/bedrock/test/conftest.py new file mode 100644 index 00000000000..615c63b51fa --- /dev/null +++ b/python/example_code/bedrock/test/conftest.py @@ -0,0 +1,12 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +""" +Contains common test fixtures used to run Amazon Bedrock tests. +""" + +import sys + +# This is needed so Python can find test_tools on the path. +sys.path.append("../..") +from test_tools.fixtures.common import * diff --git a/python/example_code/bedrock/test/test_bedrock_wrapper.py b/python/example_code/bedrock/test/test_bedrock_wrapper.py new file mode 100644 index 00000000000..af7fcfb2b88 --- /dev/null +++ b/python/example_code/bedrock/test/test_bedrock_wrapper.py @@ -0,0 +1,42 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +""" +Unit tests for bedrock_wrapper.py. +""" + +import boto3 +import pytest +from botocore.exceptions import ClientError + +from bedrock_wrapper import BedrockWrapper + + +@pytest.mark.parametrize("error_code", [None, "ClientError"]) +def test_list_foundation_models(make_stubber, error_code): + bedrock_client = boto3.client(service_name="bedrock", region_name="us-east-1") + bedrock_stubber = make_stubber(bedrock_client) + wrapper = BedrockWrapper(bedrock_client) + models = [ + { + "modelArn": "arn:aws:test:::test-resource", + "modelId": "testId", + "modelName": "testModelName", + "providerName": "testProviderName", + "inputModalities": ["TEXT"], + "outputModalities": ["TEXT"], + "responseStreamingSupported": False, + "customizationsSupported": ["FINE_TUNING"], + "inferenceTypesSupported": ["ON_DEMAND"], + } + ] + + bedrock_stubber.stub_list_foundation_models(models, error_code=error_code) + + if error_code is None: + got_models = wrapper.list_foundation_models() + assert len(got_models) > 0 + else: + with pytest.raises(ClientError) as exc_info: + wrapper.list_foundation_models() + assert exc_info.value.response["Error"]["Code"] == error_code diff --git a/python/test_tools/bedrock_runtime_stubber.py b/python/test_tools/bedrock_runtime_stubber.py new file mode 100644 index 00000000000..fa3bcf88371 --- /dev/null +++ b/python/test_tools/bedrock_runtime_stubber.py @@ -0,0 +1,118 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +""" +Stub functions that are used by the Amazon EC2 Bedrock Runtime unit tests. + +When tests are run against an actual AWS account, the stubber class does not +set up stubs and passes all calls through to the Boto3 client. +""" + +import io +import json +from test_tools.example_stubber import ExampleStubber + + +class BedrockRuntimeStubber(ExampleStubber): + """ + A class that implements stub functions used by Amazon Bedrock Runtime unit tests. + """ + + def __init__(self, client, use_stubs=True): + """ + Initializes the object with a specific client and configures it for + stubbing or AWS passthrough. + + :param client: A Boto3 Amazon Bedrock Runtime client. + :param use_stubs: When True, uses stubs to intercept requests. Otherwise, + passes requests through to AWS. + """ + super().__init__(client, use_stubs) + + def stub_invoke_claude(self, prompt, error_code=None): + expected_params = { + "modelId": "anthropic.claude-v2", + "body": json.dumps( + { + "prompt": f"Human: {prompt}\n\nAssistant:", + "max_tokens_to_sample": 200, + "temperature": 0.5, + "stop_sequences": ["\n\nHuman:"], + } + ), + } + + response = { + "body": io.BytesIO( + '{ "completion": "Fake completion response" }'.encode("utf-8") + ), + "contentType": "", + } + self._stub_bifurcator( + "invoke_model", expected_params, response, error_code=error_code + ) + + def stub_invoke_jurassic2(self, prompt, error_code=None): + expected_params = { + "modelId": "ai21.j2-mid-v1", + "body": json.dumps( + {"prompt": prompt, "temperature": 0.5, "maxTokens": 200} + ), + } + + response_body = io.BytesIO( + json.dumps( + {"completions": [{"data": {"text": "Fake completion response."}}]} + ).encode("utf-8") + ) + + response = {"body": response_body, "contentType": ""} + + self._stub_bifurcator( + "invoke_model", expected_params, response, error_code=error_code + ) + + def stub_invoke_model_with_response_stream(self, prompt, error_code=None): + expected_params = { + "modelId": "anthropic.claude-v2", + "body": json.dumps( + { + "prompt": f"Human: {prompt}\n\nAssistant:", + "max_tokens_to_sample": 1024, + "temperature": 0.5, + "stop_sequences": ["\n\nHuman:"], + } + ), + } + + self._stub_bifurcator( + "invoke_model_with_response_stream", + expected_params, + {}, + error_code=error_code, + ) + + def stub_invoke_stable_diffusion(self, prompt, style_preset, seed, error_code=None): + expected_params = { + "modelId": "stability.stable-diffusion-xl", + "body": json.dumps( + { + "text_prompts": [{"text": prompt}], + "seed": seed, + "cfg_scale": 10, + "steps": 30, + "style_preset": style_preset, + } + ), + } + + response_body = io.BytesIO( + json.dumps({"artifacts": [{"base64": "FakeBase64String=="}]}).encode( + "utf-8" + ) + ) + + response = {"body": response_body, "contentType": ""} + self._stub_bifurcator( + "invoke_model", expected_params, response, error_code=error_code + ) diff --git a/python/test_tools/bedrock_stubber.py b/python/test_tools/bedrock_stubber.py new file mode 100644 index 00000000000..9b4c6780b9c --- /dev/null +++ b/python/test_tools/bedrock_stubber.py @@ -0,0 +1,32 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +""" +Stub functions that are used by the Amazon EC2 Bedrock unit tests. +""" + +from test_tools.example_stubber import ExampleStubber + + +class BedrockStubber(ExampleStubber): + """ + A class that implements stub functions used by Amazon Bedrock unit tests. + """ + + def __init__(self, client, use_stubs=True): + """ + Initializes the object with a specific client and configures it for + stubbing or AWS passthrough. + + :param client: A Boto3 Amazon Bedrock client. + :param use_stubs: When True, uses stubs to intercept requests. Otherwise, + passes requests through to AWS. + """ + super().__init__(client, use_stubs) + + def stub_list_foundation_models(self, models, error_code=None): + expected_params = {} + response = {"modelSummaries": models} + self._stub_bifurcator( + "list_foundation_models", expected_params, response, error_code=error_code + ) diff --git a/python/test_tools/stubber_factory.py b/python/test_tools/stubber_factory.py index ff4db8f2a58..fdd46111500 100644 --- a/python/test_tools/stubber_factory.py +++ b/python/test_tools/stubber_factory.py @@ -14,6 +14,8 @@ from test_tools.apigateway_v2_stubber import ApiGatewayV2Stubber from test_tools.auditmanager_stubber import AuditManagerStubber from test_tools.autoscaling_stubber import AutoScalingStubber +from test_tools.bedrock_stubber import BedrockStubber +from test_tools.bedrock_runtime_stubber import BedrockRuntimeStubber from test_tools.cloudformation_stubber import CloudFormationStubber from test_tools.cloudfront_stubber import CloudFrontStubber from test_tools.cloudwatch_stubber import CloudWatchStubber @@ -76,6 +78,10 @@ def stubber_factory(service_name): return AuditManagerStubber elif service_name == "autoscaling": return AutoScalingStubber + elif service_name == "bedrock": + return BedrockStubber + elif service_name == "bedrock-runtime": + return BedrockRuntimeStubber elif service_name == "cloudformation": return CloudFormationStubber elif service_name == "cloudfront": From 6e742e76f34ab861b3374c0adc0c6794022aa895 Mon Sep 17 00:00:00 2001 From: Steven Meyer <108885656+meyertst-aws@users.noreply.github.com> Date: Mon, 13 Nov 2023 16:00:52 -0500 Subject: [PATCH 07/12] Bash CLI: Enable Super linter linting (#5647) * enabled validate bash for super linter. --- .github/workflows/super-linter.yml | 1 + aws-cli/bash-linux/.shellcheckrc | 4 + .../dynamodb/dynamodb_operations.sh | 1 + .../scenario_getting_started_movies.sh | 1 + .../dynamodb/tests/test_dynamodb_examples.sh | 26 ++-- .../iam_create_user_assume_role_scenario.sh | 2 +- aws-cli/bash-linux/iam/iam_operations.sh | 2 +- .../bash-linux/iam/tests/test_iam_examples.sh | 2 + .../tests/test_medical_imaging_examples.sh | 13 +- aws-cli/bash-linux/run_tests.sh | 9 +- .../awsdocs_general.sh | 1 + .../bucket_operations.sh | 10 +- .../test_bucket_operations.sh | 119 ++++++++++-------- aws-cli/bash-linux/s3/s3_getting_started.sh | 4 +- 14 files changed, 106 insertions(+), 89 deletions(-) create mode 100644 aws-cli/bash-linux/.shellcheckrc diff --git a/.github/workflows/super-linter.yml b/.github/workflows/super-linter.yml index 1ece2b565fb..a5a1c100384 100644 --- a/.github/workflows/super-linter.yml +++ b/.github/workflows/super-linter.yml @@ -37,3 +37,4 @@ jobs: VALIDATE_RUBY: true VALIDATE_KOTLIN: true VALIDATE_EDITORCONFIG: true + VALIDATE_BASH: true diff --git a/aws-cli/bash-linux/.shellcheckrc b/aws-cli/bash-linux/.shellcheckrc new file mode 100644 index 00000000000..114ae6f093f --- /dev/null +++ b/aws-cli/bash-linux/.shellcheckrc @@ -0,0 +1,4 @@ +# Look for 'source'd files relative to the checked script. +source-path=SCRIPTDIR +source-path=SCRIPTDIR/.. +source-path=aws-cli/bash-linux/s3/bucket-lifecycle-operations \ No newline at end of file diff --git a/aws-cli/bash-linux/dynamodb/dynamodb_operations.sh b/aws-cli/bash-linux/dynamodb/dynamodb_operations.sh index 2e69844306d..ea7d2404914 100644 --- a/aws-cli/bash-linux/dynamodb/dynamodb_operations.sh +++ b/aws-cli/bash-linux/dynamodb/dynamodb_operations.sh @@ -17,6 +17,7 @@ # ############################################################################### + source ./awsdocs_general.sh # snippet-start:[aws-cli.bash-linux.dynamodb.CreateTable] diff --git a/aws-cli/bash-linux/dynamodb/scenario_getting_started_movies.sh b/aws-cli/bash-linux/dynamodb/scenario_getting_started_movies.sh index 8b0a1a9fbc5..e19a2e3f4cb 100755 --- a/aws-cli/bash-linux/dynamodb/scenario_getting_started_movies.sh +++ b/aws-cli/bash-linux/dynamodb/scenario_getting_started_movies.sh @@ -344,6 +344,7 @@ function echo_repeat() { # 1 - If an error occurred. ############################################################################### function dynamodb_getting_started_movies() { + source ./dynamodb_operations.sh key_schema_json_file="dynamodb_key_schema.json" diff --git a/aws-cli/bash-linux/dynamodb/tests/test_dynamodb_examples.sh b/aws-cli/bash-linux/dynamodb/tests/test_dynamodb_examples.sh index 444a0d8940f..413e8478db1 100755 --- a/aws-cli/bash-linux/dynamodb/tests/test_dynamodb_examples.sh +++ b/aws-cli/bash-linux/dynamodb/tests/test_dynamodb_examples.sh @@ -35,7 +35,9 @@ function main() { local current_directory current_directory=$(pwd) cd .. + source ./dynamodb_operations.sh + source ./scenario_getting_started_movies.sh # shellcheck disable=SC2164 cd "$current_directory" @@ -127,7 +129,7 @@ function main() { "dynamodb_describe_table -n $test_table_name " \ 0 - if [[ "$test_command_response" != "ACTIVE" ]]; then + if [[ "$test_command_response" != "ACTIVE" ]]; then test_failed "Table is not active." return 1 fi @@ -216,8 +218,8 @@ function main() { test_count=$((test_count + 1)) echo -n "Running test $test_count: Querying table without projection expression..." - local response - response=$(dynamodb_query -n "$test_table_name" -k "#n=:v" -a "$test_attribute_names_json_file" -v "$test_attributes_values_json_file") + + dynamodb_query -n "$test_table_name" -k "#n=:v" -a "$test_attribute_names_json_file" -v "$test_attributes_values_json_file" local error_code=${?} if [[ $error_code -ne 0 ]]; then @@ -229,9 +231,8 @@ function main() { test_count=$((test_count + 1)) echo -n "Running test $test_count: Querying table with projection expression..." - local response - response=$(dynamodb_query -n "$test_table_name" -k "#n=:v" -a "$test_attribute_names_json_file" \ - -v "$test_attributes_values_json_file" -p "title,info.plot") + dynamodb_query -n "$test_table_name" -k "#n=:v" -a "$test_attribute_names_json_file" \ + -v "$test_attributes_values_json_file" -p "title,info.plot" local error_code=${?} if [[ $error_code -ne 0 ]]; then @@ -251,8 +252,8 @@ function main() { test_count=$((test_count + 1)) echo -n "Running test $test_count: Scanning table without projection expression..." - local response - response=$(dynamodb_scan -n "$test_table_name" -f "contains(#n,:v1)" -a "$test_attribute_names_json_file" -v "$test_attributes_values_json_file") + + dynamodb_scan -n "$test_table_name" -f "contains(#n,:v1)" -a "$test_attribute_names_json_file" -v "$test_attributes_values_json_file" local error_code=${?} if [[ $error_code -ne 0 ]]; then @@ -264,9 +265,9 @@ function main() { test_count=$((test_count + 1)) echo -n "Running test $test_count: Scanning table with projection expression..." - local response - response=$(dynamodb_scan -n "$test_table_name" -f "contains(#n,:v1)" -a "$test_attribute_names_json_file" \ - -v "$test_attributes_values_json_file" -p "title,info.plot") + + dynamodb_scan -n "$test_table_name" -f "contains(#n,:v1)" -a "$test_attribute_names_json_file" \ + -v "$test_attributes_values_json_file" -p "title,info.plot" local error_code=${?} if [[ $error_code -ne 0 ]]; then @@ -300,7 +301,7 @@ function main() { "dynamodb_batch_get_item -i $test_requested_values_json_file " \ 0 - skip_tests=false + export skip_tests=false run_test " deleting table" \ "dynamodb_delete_table -n $test_table_name " \ 0 @@ -364,6 +365,7 @@ function main() { rm "$test_requested_values_json_file" echo "$test_succeeded_count tests completed successfully." + echo "$test_failed_count tests failed." } diff --git a/aws-cli/bash-linux/iam/iam_create_user_assume_role_scenario.sh b/aws-cli/bash-linux/iam/iam_create_user_assume_role_scenario.sh index 950ffa59bb6..e541577973a 100755 --- a/aws-cli/bash-linux/iam/iam_create_user_assume_role_scenario.sh +++ b/aws-cli/bash-linux/iam/iam_create_user_assume_role_scenario.sh @@ -321,7 +321,7 @@ function sts_assume_role() { function iam_create_user_assume_role() { { if [ "$IAM_OPERATIONS_SOURCED" != "True" ]; then - # shellcheck disable=SC1091 + source ./iam_operations.sh fi } diff --git a/aws-cli/bash-linux/iam/iam_operations.sh b/aws-cli/bash-linux/iam/iam_operations.sh index 1d7a6ffcff0..26e30c1c40f 100644 --- a/aws-cli/bash-linux/iam/iam_operations.sh +++ b/aws-cli/bash-linux/iam/iam_operations.sh @@ -72,7 +72,7 @@ function iam_user_exists() { ############################################################################### function iam_list_users() { local option OPTARG # Required to use getopts command in a function. - local all_users error_code + local error_code # bashsupport disable=BP5008 function usage() { echo "function iam_list_users" diff --git a/aws-cli/bash-linux/iam/tests/test_iam_examples.sh b/aws-cli/bash-linux/iam/tests/test_iam_examples.sh index 65a57422a16..1f96feae023 100755 --- a/aws-cli/bash-linux/iam/tests/test_iam_examples.sh +++ b/aws-cli/bash-linux/iam/tests/test_iam_examples.sh @@ -35,7 +35,9 @@ function main() { local current_directory current_directory=$(pwd) cd .. + source ./iam_operations.sh + source ./iam_create_user_assume_role_scenario.sh # shellcheck disable=SC2164 cd "$current_directory" diff --git a/aws-cli/bash-linux/medical-imaging/tests/test_medical_imaging_examples.sh b/aws-cli/bash-linux/medical-imaging/tests/test_medical_imaging_examples.sh index dbf9a003444..21e8f07655b 100755 --- a/aws-cli/bash-linux/medical-imaging/tests/test_medical_imaging_examples.sh +++ b/aws-cli/bash-linux/medical-imaging/tests/test_medical_imaging_examples.sh @@ -23,17 +23,18 @@ # This function runs the IAM examples' tests. ############################################################################### function main() { -# Set default values. -# bashsupport disable=BP2001 -export INTERACTIVE=false -# bashsupport disable=BP2001 -export VERBOSE=false + # Set default values. + # bashsupport disable=BP2001 + export INTERACTIVE=false + # bashsupport disable=BP2001 + export VERBOSE=false source ./include_tests.sh { local current_directory current_directory=$(pwd) cd .. + source ./medical_imaging_operations.sh # shellcheck disable=SC2164 cd "$current_directory" @@ -106,7 +107,7 @@ export VERBOSE=false local data_store_found=false local line while IFS=$'\n' read -r line; do - IFS=$'\t' read -ra entries <<<"$line" + IFS=$'\t' read -ra entries <<<"$line" if [ "${entries[1]}" == "$datastore_id" ]; then data_store_found=true fi diff --git a/aws-cli/bash-linux/run_tests.sh b/aws-cli/bash-linux/run_tests.sh index 315ca9cbf72..6541a66d4d8 100755 --- a/aws-cli/bash-linux/run_tests.sh +++ b/aws-cli/bash-linux/run_tests.sh @@ -3,18 +3,15 @@ base_dir=$(pwd) echo "Running tests for S3 Bucket Lifecycle Operations" -cd s3/bucket-lifecycle-operations/ +cd "$base_dir/s3/bucket-lifecycle-operations/" || exit ./test_bucket_operations.sh -cd $base_dir echo "Running IAM tests" -cd iam/tests +cd "$base_dir/iam/tests" || exit ./test_iam_examples.sh -cd $base_dir echo "Running medical-imaging tests" -cd medical-imaging/tests +cd "$base_dir/medical-imaging/tests" || exit ./test_medical_imaging_examples.sh -cd $base_dir echo "Done" \ No newline at end of file diff --git a/aws-cli/bash-linux/s3/bucket-lifecycle-operations/awsdocs_general.sh b/aws-cli/bash-linux/s3/bucket-lifecycle-operations/awsdocs_general.sh index be212a49edd..5eddab51272 100644 --- a/aws-cli/bash-linux/s3/bucket-lifecycle-operations/awsdocs_general.sh +++ b/aws-cli/bash-linux/s3/bucket-lifecycle-operations/awsdocs_general.sh @@ -53,6 +53,7 @@ function run_test() { # Check to see if we got the expected error code. if [[ "$expected_err_code" -ne "$err" ]]; then + # shellcheck disable=SC2154 test_failed "The test \"$description\" returned an unexpected error code: $err. $test_command_response" fi diff --git a/aws-cli/bash-linux/s3/bucket-lifecycle-operations/bucket_operations.sh b/aws-cli/bash-linux/s3/bucket-lifecycle-operations/bucket_operations.sh index 32664aa6341..7135964d34b 100644 --- a/aws-cli/bash-linux/s3/bucket-lifecycle-operations/bucket_operations.sh +++ b/aws-cli/bash-linux/s3/bucket-lifecycle-operations/bucket_operations.sh @@ -19,6 +19,7 @@ ############################################################################### # snippet-start:[s3.bash.bucket-operations.complete] + source ./awsdocs_general.sh # snippet-start:[aws-cli.bash-linux.s3.HeadBucket] @@ -41,12 +42,9 @@ function bucket_exists() { # Check whether the bucket already exists. # We suppress all output - we're interested only in the return code. - aws s3api head-bucket \ + if aws s3api head-bucket \ --bucket "$bucket_name" \ - >/dev/null 2>&1 - - # shellcheck disable=SC2181 - if [[ ${?} -eq 0 ]]; then + >/dev/null 2>&1; then return 0 # 0 in Bash script means true. else return 1 # 1 in Bash script means false. @@ -378,4 +376,4 @@ function delete_bucket() { # snippet-end:[s3.bash.bucket-operations.complete] # shellcheck disable=SC2034 -declare -r BUCKET_OPERATIONS_SOURCED="True" \ No newline at end of file +declare -r BUCKET_OPERATIONS_SOURCED="True" diff --git a/aws-cli/bash-linux/s3/bucket-lifecycle-operations/test_bucket_operations.sh b/aws-cli/bash-linux/s3/bucket-lifecycle-operations/test_bucket_operations.sh index 0c984683364..6d7c3bf609a 100755 --- a/aws-cli/bash-linux/s3/bucket-lifecycle-operations/test_bucket_operations.sh +++ b/aws-cli/bash-linux/s3/bucket-lifecycle-operations/test_bucket_operations.sh @@ -17,47 +17,56 @@ # ############################################################################### - source ./awsdocs_general.sh + source ./bucket_operations.sh { current_directory=$(pwd) - cd .. + cd .. || exit + source ./s3_getting_started.sh - cd $current_directory + cd "$current_directory" || exit } -source ../s3_getting_started.sh function usage() { - echo "This script tests Amazon S3 bucket operations in the AWS CLI." - echo "It creates a randomly named bucket, copies files to it, then" - echo "deletes the files and the bucket." - echo "" - echo "To pause the script between steps so you can see the results in the" - echo "AWS Management Console, include the parameter -i." - echo "" - echo "IMPORTANT: Running this script creates resources in your Amazon" - echo " account that can incur charges. It is your responsibility to" - echo " ensure that no resources are left in your account after the script" - echo " completes. If an error occurs during the operation of the script," - echo " then resources can remain that you might need to delete manually." + echo "This script tests Amazon S3 bucket operations in the AWS CLI." + echo "It creates a randomly named bucket, copies files to it, then" + echo "deletes the files and the bucket." + echo "" + echo "To pause the script between steps so you can see the results in the" + echo "AWS Management Console, include the parameter -i." + echo "" + echo "IMPORTANT: Running this script creates resources in your Amazon" + echo " account that can incur charges. It is your responsibility to" + echo " ensure that no resources are left in your account after the script" + echo " completes. If an error occurs during the operation of the script," + echo " then resources can remain that you might need to delete manually." } - # Set default values. - INTERACTIVE=false - VERBOSE=false - - # Retrieve the calling parameters - while getopts "ivh" OPTION; do - case "${OPTION}" - in - i) INTERACTIVE=true;VERBOSE=true; iecho;; - v) VERBOSE=true;; - h) usage; return 0;; - \?) echo "Invalid parameter"; usage; return 1;; - esac - done - +# Set default values. +INTERACTIVE=false +VERBOSE=false + +# Retrieve the calling parameters +while getopts "ivh" OPTION; do + case "${OPTION}" in + i) + INTERACTIVE=true + VERBOSE=true + iecho + ;; + v) VERBOSE=true ;; + h) + usage + return 0 + ;; + \?) + echo "Invalid parameter" + usage + return 1 + ;; + esac +done if [ "$INTERACTIVE" == "true" ]; then iecho "Tests running in interactive mode."; fi if [ "$VERBOSE" == "true" ]; then iecho "Tests running in verbose mode."; fi @@ -76,49 +85,49 @@ iecho "FILENAME2=$FILENAME2" iecho "**************END OF STEPS******************" run_test "1. Creating bucket with missing bucket_name" \ - "create_bucket -r $REGION" \ - 1 \ - "ERROR: You must provide a bucket name" \ + "create_bucket -r $REGION" \ + 1 \ + "ERROR: You must provide a bucket name" run_test "3. Creating bucket with valid parameters" \ - "create_bucket -r $REGION -b $BUCKETNAME" \ - 0 + "create_bucket -r $REGION -b $BUCKETNAME" \ + 0 run_test "4. Creating bucket with duplicate name and region" \ - "create_bucket -r $REGION -b $BUCKETNAME" \ - 1 \ - "ERROR: A bucket with that name already exists" + "create_bucket -r $REGION -b $BUCKETNAME" \ + 1 \ + "ERROR: A bucket with that name already exists" run_test "5. Copying local file (copy of this script) to bucket" \ - "copy_file_to_bucket $BUCKETNAME ./$0 $FILENAME1" \ - 0 + "copy_file_to_bucket $BUCKETNAME $0 $FILENAME1" \ + 0 run_test "6. Duplicating existing file in bucket" \ - "copy_item_in_bucket $BUCKETNAME $FILENAME1 $FILENAME2" \ - 0 + "copy_item_in_bucket $BUCKETNAME $FILENAME1 $FILENAME2" \ + 0 run_test "7. Listing contents of bucket" \ - "list_items_in_bucket $BUCKETNAME" \ - 0 + "list_items_in_bucket $BUCKETNAME" \ + 0 run_test "8. Deleting first file from bucket" \ - "delete_item_in_bucket $BUCKETNAME $FILENAME1" \ - 0 + "delete_item_in_bucket $BUCKETNAME $FILENAME1" \ + 0 run_test "9. Deleting second file from bucket" \ - "delete_item_in_bucket $BUCKETNAME $FILENAME2" \ - 0 + "delete_item_in_bucket $BUCKETNAME $FILENAME2" \ + 0 run_test "10. Deleting bucket" \ - "delete_bucket $BUCKETNAME" \ - 0 + "delete_bucket $BUCKETNAME" \ + 0 -mock_input="True" -mock_input_array=("README.md" "y" "y" "y") +export mock_input="True" +export mock_input_array=("README.md" "y" "y" "y") run_test "11. s3 getting started scenario" \ - s3_getting_started \ - 0 + s3_getting_started \ + 0 unset mock_input diff --git a/aws-cli/bash-linux/s3/s3_getting_started.sh b/aws-cli/bash-linux/s3/s3_getting_started.sh index 5d5ef54369f..4c979fa79e6 100755 --- a/aws-cli/bash-linux/s3/s3_getting_started.sh +++ b/aws-cli/bash-linux/s3/s3_getting_started.sh @@ -138,7 +138,7 @@ function s3_getting_started() { { if [ "$BUCKET_OPERATIONS_SOURCED" != "True" ]; then cd bucket-lifecycle-operations || exit - # shellcheck disable=SC1091 + source ./bucket_operations.sh cd .. fi @@ -206,6 +206,7 @@ function s3_getting_started() { local bucket_items bucket_items=$(list_items_in_bucket "$bucket_name") + # shellcheck disable=SC2181 if [[ $? -ne 0 ]]; then result=1 fi @@ -247,4 +248,3 @@ function main() { if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main fi - From 2ea3c65d947fbb43138bd44658ca03d0276e0b22 Mon Sep 17 00:00:00 2001 From: Dennis Traub Date: Mon, 13 Nov 2023 22:23:33 +0100 Subject: [PATCH 08/12] Java & Python: Add FM Playground example applications in Amazon Bedrock (#5652) * Add cross content configuration for Java Foundation Model (FM) Playground --------- Co-authored-by: Dennis Traub --- .../cross_FMPlayground_Java_block.xml | 23 +++++++++++++++++++ .../cross_FMPlayground_Python_block.xml | 23 +++++++++++++++++++ .doc_gen/metadata/cross_metadata.yaml | 8 +++++++ 3 files changed, 54 insertions(+) create mode 100644 .doc_gen/cross-content/cross_FMPlayground_Java_block.xml create mode 100644 .doc_gen/cross-content/cross_FMPlayground_Python_block.xml diff --git a/.doc_gen/cross-content/cross_FMPlayground_Java_block.xml b/.doc_gen/cross-content/cross_FMPlayground_Java_block.xml new file mode 100644 index 00000000000..b40b64ca5e8 --- /dev/null +++ b/.doc_gen/cross-content/cross_FMPlayground_Java_block.xml @@ -0,0 +1,23 @@ + + + %phrases-shared; +]> + + + The Java Foundation Model (FM) Playground is a Spring Boot sample application that showcases how to use &BRlong; with Java. This example shows how Java developers can use &BR; to build generative AI-enabled applications. You can test and interact with &BR; foundation models by using the following three playgrounds: + + + + A text playground. + + + A chat playground. + + + An image playground. + + + The example also lists and displays the foundation models you have access to, along with their characteristics. For source code and deployment instructions, see the project in GitHub. + + diff --git a/.doc_gen/cross-content/cross_FMPlayground_Python_block.xml b/.doc_gen/cross-content/cross_FMPlayground_Python_block.xml new file mode 100644 index 00000000000..9b8230c33e9 --- /dev/null +++ b/.doc_gen/cross-content/cross_FMPlayground_Python_block.xml @@ -0,0 +1,23 @@ + + + %phrases-shared; +]> + + + The Python Foundation Model (FM) Playground is a Python/FastAPI sample application that showcases how to use &BRlong; with Python. This example shows how Python developers can use &BR; to build generative AI-enabled applications. You can test and interact with &BR; foundation models by using the following three playgrounds: + + + + A text playground. + + + A chat playground. + + + An image playground. + + + The example also lists and displays the foundation models you have access to, along with their characteristics. For source code and deployment instructions, see the project in GitHub. + + diff --git a/.doc_gen/metadata/cross_metadata.yaml b/.doc_gen/metadata/cross_metadata.yaml index 8520fb42421..d4ddd04d2cb 100644 --- a/.doc_gen/metadata/cross_metadata.yaml +++ b/.doc_gen/metadata/cross_metadata.yaml @@ -704,6 +704,14 @@ cross_FMPlayground: versions: - sdk_version: 3 block_content: cross_FMPlayground_NetV3_block.xml + Java: + versions: + - sdk_version: 2 + block_content: cross_FMPlayground_Java_block.xml + Python: + versions: + - sdk_version: 3 + block_content: cross_FMPlayground_Python_block.xml service_main: bedrock-runtime services: bedrock-runtime: From 4474e61938a4f7e4ccaa9cb8df8166eff733a389 Mon Sep 17 00:00:00 2001 From: Jason Q <81179619+beqqrry-aws@users.noreply.github.com> Date: Wed, 15 Nov 2023 09:28:37 -0700 Subject: [PATCH 09/12] Dependabot updates (#5666) * Bump org.json:json from 20230227 to 20231013 in /javav2/usecases/creating_fsa_app (#5661) * Bump org.json:json from 20230227 to 20231013 in /javav2/example_code/lambda (#5660) * Bump org.json:json from 20230227 to 20231013 in /javav2/example_code/lookoutvision (#5659) * Bump org.json:json from 20230227 to 20231013 in /javav2/usecases/pam_source_files (#5658) * Bump org.json:json from 20210307 to 20231013 in /javav2/usecases/workflow_sagemaker_pipes (#5657) --- javav2/example_code/lambda/pom.xml | 2 +- javav2/example_code/lookoutvision/pom.xml | 2 +- javav2/usecases/creating_fsa_app/pom.xml | 2 +- javav2/usecases/pam_source_files/pom.xml | 2 +- javav2/usecases/workflow_sagemaker_pipes/pom.xml | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/javav2/example_code/lambda/pom.xml b/javav2/example_code/lambda/pom.xml index b6921ff1a94..9867cf27a81 100644 --- a/javav2/example_code/lambda/pom.xml +++ b/javav2/example_code/lambda/pom.xml @@ -66,7 +66,7 @@ org.json json - 20230227 + 20231013 org.junit.jupiter diff --git a/javav2/example_code/lookoutvision/pom.xml b/javav2/example_code/lookoutvision/pom.xml index ed1dc109282..53938f4de64 100644 --- a/javav2/example_code/lookoutvision/pom.xml +++ b/javav2/example_code/lookoutvision/pom.xml @@ -94,7 +94,7 @@ org.json json - 20230227 + 20231013 diff --git a/javav2/usecases/creating_fsa_app/pom.xml b/javav2/usecases/creating_fsa_app/pom.xml index a71bc6f7bf4..243f4eb3cb7 100644 --- a/javav2/usecases/creating_fsa_app/pom.xml +++ b/javav2/usecases/creating_fsa_app/pom.xml @@ -103,7 +103,7 @@ org.json json - 20230227 + 20231013 software.amazon.awssdk diff --git a/javav2/usecases/pam_source_files/pom.xml b/javav2/usecases/pam_source_files/pom.xml index 3139a298d1b..9402ec1204d 100644 --- a/javav2/usecases/pam_source_files/pom.xml +++ b/javav2/usecases/pam_source_files/pom.xml @@ -112,7 +112,7 @@ org.json json - 20230227 + 20231013 software.amazon.awssdk diff --git a/javav2/usecases/workflow_sagemaker_pipes/pom.xml b/javav2/usecases/workflow_sagemaker_pipes/pom.xml index ca3f7189ccb..52c95496619 100644 --- a/javav2/usecases/workflow_sagemaker_pipes/pom.xml +++ b/javav2/usecases/workflow_sagemaker_pipes/pom.xml @@ -84,7 +84,7 @@ org.json json - 20210307 + 20231013 org.junit.jupiter From 87905d01490220dbff334baafe334d67d615b358 Mon Sep 17 00:00:00 2001 From: Dennis Traub Date: Wed, 15 Nov 2023 18:45:47 +0100 Subject: [PATCH 10/12] Java & Python: Add Bedrock Runtime examples for newly released Llama 2 LLM (#5653) * Python: Add Invoke Llama 2 * Java: Add Invoke Llama 2 --- .../metadata/bedrock-runtime_metadata.yaml | 26 +++++++++ javav2/example_code/bedrock-runtime/Readme.md | 5 +- .../example/bedrockruntime/InvokeModel.java | 57 ++++++++++++++++++- .../src/test/java/BedrockRuntimeTest.java | 23 +++++++- python/example_code/bedrock-runtime/README.md | 9 +-- .../bedrock_runtime_wrapper.py | 49 +++++++++++++++- .../test/test_bedrock_runtime_wrapper.py | 21 +++++++ python/test_tools/bedrock_runtime_stubber.py | 18 ++++++ 8 files changed, 195 insertions(+), 13 deletions(-) diff --git a/.doc_gen/metadata/bedrock-runtime_metadata.yaml b/.doc_gen/metadata/bedrock-runtime_metadata.yaml index 58dbd69cb95..4c30326c7ba 100644 --- a/.doc_gen/metadata/bedrock-runtime_metadata.yaml +++ b/.doc_gen/metadata/bedrock-runtime_metadata.yaml @@ -70,6 +70,32 @@ bedrock-runtime_InvokeJurassic2: services: bedrock-runtime: {InvokeModel} +bedrock-runtime_InvokeLlama2: + title: Invoke the Meta Llama 2 Chat model on &BR; to run an inference + title_abbrev: Invoke Meta Llama 2 on &BR; + synopsis: invoke the Meta Llama 2 Chat model on &BR; to run inference. + category: + languages: + Java: + versions: + - sdk_version: 2 + github: javav2/example_code/bedrock-runtime + excerpts: + - description: Invoke the Meta Llama 2 foundation model. + snippet_tags: + - bedrock-runtime.java2.invoke_llama2.main + Python: + versions: + - sdk_version: 3 + github: python/example_code/bedrockruntime + sdkguide: + excerpts: + - description: Invoke the AI 21 Labs Jurassic-2 foundation model. + snippet_tags: + - python.example_code.bedrock-runtime.InvokeMetaLlama2 + services: + bedrock-runtime: {InvokeModel} + bedrock-runtime_InvokeStableDiffusion: title: Invoke the Stability.ai Stable Diffusion XL model on &BR; to run an inference title_abbrev: Invoke Stability.ai Stable Diffusion XL on &BR; diff --git a/javav2/example_code/bedrock-runtime/Readme.md b/javav2/example_code/bedrock-runtime/Readme.md index 798f7a003f5..82126b054b0 100644 --- a/javav2/example_code/bedrock-runtime/Readme.md +++ b/javav2/example_code/bedrock-runtime/Readme.md @@ -1,4 +1,4 @@ - + # Amazon Bedrock Runtime code examples for the SDK for Java 2.x ## Overview @@ -37,7 +37,8 @@ Code excerpts that show you how to call individual service functions. * [Invoke AI21 Labs Jurassic-2 on Amazon Bedrock](src/main/java/com/example/bedrockruntime/InvokeModel.java#L101) (`InvokeModel`) * [Invoke Anthropic Claude 2 on Amazon Bedrock](src/main/java/com/example/bedrockruntime/InvokeModel.java#L51) (`InvokeModel`) -* [Invoke Stability.ai Stable Diffusion XL on Amazon Bedrock](src/main/java/com/example/bedrockruntime/InvokeModel.java#L151) (`InvokeModel`) +* [Invoke Meta Llama 2 on Amazon Bedrock](src/main/java/com/example/bedrockruntime/InvokeModel.java#L151) (`InvokeModel`) +* [Invoke Stability.ai Stable Diffusion XL on Amazon Bedrock](src/main/java/com/example/bedrockruntime/InvokeModel.java#L198) (`InvokeModel`) * [Invoke a model on Amazon Bedrock with a response stream](src/main/java/com/example/bedrockruntime/InvokeModelWithResponseStream.java#L49) (`InvokeModelWithResponseStream`) ## Run the examples diff --git a/javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/InvokeModel.java b/javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/InvokeModel.java index 15723434ffc..f5fc7a514f9 100644 --- a/javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/InvokeModel.java +++ b/javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/InvokeModel.java @@ -148,6 +148,53 @@ public static String invokeJurassic2(BedrockRuntimeClient client, String prompt) } // snippet-end:[bedrock-runtime.java2.invoke_jurassic2.main] + // snippet-start:[bedrock-runtime.java2.invoke_llama2.main] + /** + * Invokes the Meta Llama 2 Chat model to run an inference based on the provided input. + * + * @param client A Bedrock Runtime client + * @param prompt The prompt that you want Llama 2 to complete. + * @return Inference response from the model. + */ + public static String invokeLlama2(BedrockRuntimeClient client, String prompt) { + + try { + + /* + For request/response formats, defaults, and value ranges of Meta Llama 2 Chat, see: + https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html + */ + + JSONObject payload = new JSONObject() + .put("prompt", prompt) + .put("max_gen_len", 512) + .put("temperature", 0.5) + .put("top_p", 0.9); + + SdkBytes body = SdkBytes.fromUtf8String(payload.toString()); + + InvokeModelRequest request = InvokeModelRequest.builder() + .modelId("meta.llama2-13b-chat-v1") + .body(body) + .build(); + + InvokeModelResponse response = client.invokeModel(request); + + JSONObject responseBody = new JSONObject(response.body().asUtf8String()); + + String completion = responseBody.getString("generation"); + + return completion; + + } catch (AwsServiceException e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); + } + return null; + + } + // snippet-end:[bedrock-runtime.java2.invoke_llama2.main] + // snippet-start:[bedrock-runtime.java2.invoke_stable_diffusion.main] /** * Invokes the Stability.ai Stable Diffusion XL model to create an image based on the provided input. @@ -223,6 +270,7 @@ private static void usageDemo() { String textPrompt = "In one sentence, what is a large-language model?"; invoke(client, "anthropic.claude-v2", textPrompt); invoke(client, "ai21.j2-mid-v1", textPrompt); + invoke(client, "meta.llama2-13b-chat-v1", textPrompt); String imagePrompt = "A sunset over the ocean"; String stylePreset = "photographic"; @@ -242,15 +290,18 @@ private static void invoke(BedrockRuntimeClient client, String modelId, String p String completion; if ("anthropic.claude-v2".equals(modelId)) { completion = invokeClaude(client, prompt); - System.out.println("Completion: " + completion.trim()); + System.out.printf("Completion: %s%n", completion); } else if ("ai21.j2-mid-v1".equals(modelId)) { completion = invokeJurassic2(client, prompt); - System.out.println("Completion: " + completion.trim()); + System.out.printf("Completion: %s%n", completion); + } else if ("meta.llama2-13b-chat-v1".equals(modelId)) { + completion = invokeLlama2(client, prompt); + System.out.printf("Completion: %s%n", completion); } else if ("stability.stable-diffusion-xl".equals(modelId)) { long seed = (random.nextLong() & 0xFFFFFFFFL); String base64ImageData = invokeStableDiffusion(client, prompt, seed, stylePreset); String imagePath = saveImage(base64ImageData); - System.out.println("The generated image has been saved to " + imagePath); + System.out.printf("The generated image has been saved to %s%n", imagePath); } } catch (BedrockRuntimeException e) { System.out.println("Couldn't invoke model " + modelId + ": " + e.getMessage()); diff --git a/javav2/example_code/bedrock-runtime/src/test/java/BedrockRuntimeTest.java b/javav2/example_code/bedrock-runtime/src/test/java/BedrockRuntimeTest.java index 85dabbcbf33..5638d4a455b 100644 --- a/javav2/example_code/bedrock-runtime/src/test/java/BedrockRuntimeTest.java +++ b/javav2/example_code/bedrock-runtime/src/test/java/BedrockRuntimeTest.java @@ -64,6 +64,27 @@ void InvokeJurassic2() { @Test @Order(3) @Tag("IntegrationTest") + void InvokeLlama2() { + + try (BedrockRuntimeClient client = BedrockRuntimeClient.builder() + .region(Region.US_EAST_1) + .credentialsProvider(ProfileCredentialsProvider.create()) + .build()) { + + String prompt = "In one sentence, what is a large-language model?"; + + String completion = InvokeModel.invokeLlama2(client, prompt); + + assertNotNull(completion, "The completion is null"); + assertFalse(completion.trim().isEmpty(), "The completion is empty"); + + System.out.printf("Test %d passed.%n", getTestNumber(new Object(){}.getClass().getEnclosingMethod())); + } + } + + @Test + @Order(4) + @Tag("IntegrationTest") void InvokeStableDiffusion() { try (BedrockRuntimeClient client = BedrockRuntimeClient.builder() @@ -85,7 +106,7 @@ void InvokeStableDiffusion() { } @Test - @Order(4) + @Order(5) @Tag("IntegrationTest") void InvokeModelWithResponseStream() { diff --git a/python/example_code/bedrock-runtime/README.md b/python/example_code/bedrock-runtime/README.md index fc40dbf7d95..07ec0047964 100644 --- a/python/example_code/bedrock-runtime/README.md +++ b/python/example_code/bedrock-runtime/README.md @@ -1,4 +1,4 @@ - + # Amazon Bedrock Runtime code examples for the SDK for Python ## Overview @@ -40,10 +40,11 @@ python -m pip install -r requirements.txt Code excerpts that show you how to call individual service functions. -* [Invoke AI21 Labs Jurassic-2 on Amazon Bedrock](bedrock_runtime_wrapper.py#L81) (`InvokeModel`) +* [Invoke AI21 Labs Jurassic-2 on Amazon Bedrock](bedrock_runtime_wrapper.py#L79) (`InvokeModel`) * [Invoke Anthropic Claude 2 on Amazon Bedrock](bedrock_runtime_wrapper.py#L39) (`InvokeModel`) -* [Invoke Stability.ai Stable Diffusion XL on Amazon Bedrock](bedrock_runtime_wrapper.py#L119) (`InvokeModel`) -* [Invoke a model on Amazon Bedrock with a response stream](bedrock_runtime_wrapper.py#L164) (`InvokeModelWithResponseStream`) +* [Invoke Meta Llama 2 on Amazon Bedrock](bedrock_runtime_wrapper.py#L115) (`InvokeModel`) +* [Invoke Stability.ai Stable Diffusion XL on Amazon Bedrock](bedrock_runtime_wrapper.py#L152) (`InvokeModel`) +* [Invoke a model on Amazon Bedrock with a response stream](bedrock_runtime_wrapper.py#L195) (`InvokeModelWithResponseStream`) ## Run the examples diff --git a/python/example_code/bedrock-runtime/bedrock_runtime_wrapper.py b/python/example_code/bedrock-runtime/bedrock_runtime_wrapper.py index 10d258ba662..5a534cff569 100644 --- a/python/example_code/bedrock-runtime/bedrock_runtime_wrapper.py +++ b/python/example_code/bedrock-runtime/bedrock_runtime_wrapper.py @@ -112,6 +112,43 @@ def invoke_jurassic2(self, prompt): # snippet-end:[python.example_code.bedrock-runtime.InvokeAi21Jurassic2] + # snippet-start:[python.example_code.bedrock-runtime.InvokeMetaLlama2] + def invoke_llama2(self, prompt): + """ + Invokes the Meta Llama 2 large-language model to run an inference + using the input provided in the request body. + + :param prompt: The prompt that you want Jurassic-2 to complete. + :return: Inference response from the model. + """ + + try: + # The different model providers have individual request and response formats. + # For the format, ranges, and default values for Meta Llama 2 Chat, refer to: + # https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html + + body = { + "prompt": prompt, + "temperature": 0.5, + "top_p": 0.9, + "max_gen_len": 512, + } + + response = self.bedrock_runtime_client.invoke_model( + modelId="meta.llama2-13b-chat-v1", body=json.dumps(body) + ) + + response_body = json.loads(response["body"].read()) + completion = response_body["generation"] + + return completion + + except ClientError: + logger.error("Couldn't invoke Llama 2") + raise + + # snippet-end:[python.example_code.bedrock-runtime.InvokeMetaLlama2] + # snippet-start:[python.example_code.bedrock-runtime.InvokeStableDiffusion] def invoke_stable_diffusion(self, prompt, seed, style_preset=None): """ @@ -227,11 +264,15 @@ def invoke(wrapper, model_id, prompt, style_preset=None): try: if model_id == "anthropic.claude-v2": completion = wrapper.invoke_claude(prompt) - print("Completion: " + completion.strip()) + print("Completion: " + completion) elif model_id == "ai21.j2-mid-v1": completion = wrapper.invoke_jurassic2(prompt) - print("Completion: " + completion.strip()) + print("Completion: " + completion) + + elif model_id == "meta.llama2-13b-chat-v1": + completion = wrapper.invoke_llama2(prompt) + print("Completion: " + completion) elif model_id == "stability.stable-diffusion-xl": seed = random.randint(0, 4294967295) @@ -277,7 +318,7 @@ def usage_demo(): wrapper = BedrockRuntimeWrapper(client) - text_generation_prompt = "Hi, write a paragraph about yourself" + text_generation_prompt = "Hi, write a paragraph about yourself." image_generation_prompt = "A sunset over the ocean" image_style_preset = "photographic" @@ -285,6 +326,8 @@ def usage_demo(): invoke(wrapper, "ai21.j2-mid-v1", text_generation_prompt) + invoke(wrapper, "meta.llama2-13b-chat-v1", text_generation_prompt) + asyncio.run( invoke_with_response_stream( wrapper, "anthropic.claude-v2", text_generation_prompt diff --git a/python/example_code/bedrock-runtime/test/test_bedrock_runtime_wrapper.py b/python/example_code/bedrock-runtime/test/test_bedrock_runtime_wrapper.py index 68e9da62556..52f59353748 100644 --- a/python/example_code/bedrock-runtime/test/test_bedrock_runtime_wrapper.py +++ b/python/example_code/bedrock-runtime/test/test_bedrock_runtime_wrapper.py @@ -56,6 +56,27 @@ def test_invoke_jurassic2(make_stubber, error_code): assert exc_info.value.response["Error"]["Code"] == error_code +@pytest.mark.parametrize("error_code", [None, "ClientError"]) +def test_invoke_llama2(make_stubber, error_code): + bedrock_runtime = boto3.client( + service_name="bedrock-runtime", region_name="us-east-1" + ) + bedrock_runtime_stubber = make_stubber(bedrock_runtime) + wrapper = BedrockRuntimeWrapper(bedrock_runtime) + + prompt = "Hey, how are you?" + + bedrock_runtime_stubber.stub_invoke_llama2(prompt, error_code=error_code) + + if error_code is None: + got_completion = wrapper.invoke_llama2(prompt) + assert len(got_completion) > 0 + else: + with pytest.raises(ClientError) as exc_info: + wrapper.invoke_llama2(prompt) + assert exc_info.value.response["Error"]["Code"] == error_code + + @pytest.mark.asyncio @pytest.mark.parametrize("error_code", ["ClientError"]) async def test_invoke_model_with_response_stream(make_stubber, error_code): diff --git a/python/test_tools/bedrock_runtime_stubber.py b/python/test_tools/bedrock_runtime_stubber.py index fa3bcf88371..f9c8d74358d 100644 --- a/python/test_tools/bedrock_runtime_stubber.py +++ b/python/test_tools/bedrock_runtime_stubber.py @@ -72,6 +72,24 @@ def stub_invoke_jurassic2(self, prompt, error_code=None): "invoke_model", expected_params, response, error_code=error_code ) + def stub_invoke_llama2(self, prompt, error_code=None): + expected_params = { + "modelId": "meta.llama2-13b-chat-v1", + "body": json.dumps( + {"prompt": prompt, "temperature": 0.5, "top_p": 0.9, "max_gen_len": 512} + ), + } + + response_body = io.BytesIO( + json.dumps({"generation": "Fake completion response."}).encode("utf-8") + ) + + response = {"body": response_body, "contentType": ""} + + self._stub_bifurcator( + "invoke_model", expected_params, response, error_code=error_code + ) + def stub_invoke_model_with_response_stream(self, prompt, error_code=None): expected_params = { "modelId": "anthropic.claude-v2", From 2e804c4f17dfc13515c64be0be56d4644114d606 Mon Sep 17 00:00:00 2001 From: Dennis Traub Date: Wed, 15 Nov 2023 18:50:22 +0100 Subject: [PATCH 11/12] Add supported customizations to model output (#5664) Reintroduce supported customizations --- python/example_code/bedrock/bedrock_wrapper.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/example_code/bedrock/bedrock_wrapper.py b/python/example_code/bedrock/bedrock_wrapper.py index 58057d9d294..d0fa98b2a21 100644 --- a/python/example_code/bedrock/bedrock_wrapper.py +++ b/python/example_code/bedrock/bedrock_wrapper.py @@ -80,6 +80,7 @@ def usage_demo(): print(f' Provider: {model["providerName"]}') print(f' Input modalities: {model["inputModalities"]}') print(f' Output modalities: {model["outputModalities"]}') + print(f' Supported customizations: {model["customizationsSupported"]}') print(f' Supported inference types: {model["inferenceTypesSupported"]}') print("=" * 42) except ClientError: From 9f08f276537a38dc094727f1ad0b0ceacbb1dc7b Mon Sep 17 00:00:00 2001 From: John DiSanti Date: Wed, 15 Nov 2023 17:36:56 -0800 Subject: [PATCH 12/12] Fix examples after Rust SDK breaking changes (#5662) * Fix examples after Rust SDK breaking changes * Use a combination of crate and local BehaviorVersions --- .../cross_service/detect_faces/src/main.rs | 5 +- .../cross_service/detect_labels/src/main.rs | 7 +- .../photo_asset_management/src/common.rs | 4 +- .../src/handlers/detect_labels.rs | 8 +- .../tests/test_upload_part.rs | 3 +- .../cross_service/rest_ses/src/main.rs | 3 +- .../rest_ses/tests/api/startup.rs | 6 +- .../cross_service/telephone/src/main.rs | 7 +- .../examples/apigateway/Cargo.toml | 2 +- .../examples/apigatewaymanagement/Cargo.toml | 2 +- .../applicationautoscaling/Cargo.toml | 2 +- rust_dev_preview/examples/aurora/Cargo.toml | 2 +- .../aurora/src/aurora_scenario/tests.rs | 26 +++--- .../examples/auto-scaling/Cargo.toml | 2 +- .../examples/autoscalingplans/Cargo.toml | 2 +- rust_dev_preview/examples/batch/Cargo.toml | 2 +- .../examples/cloudformation/Cargo.toml | 2 +- .../examples/cloudwatch/Cargo.toml | 2 +- .../examples/cloudwatchlogs/Cargo.toml | 2 +- .../examples/cognitoidentity/Cargo.toml | 2 +- .../cognitoidentityprovider/Cargo.toml | 2 +- .../examples/cognitosync/Cargo.toml | 2 +- rust_dev_preview/examples/config/Cargo.toml | 2 +- .../config/src/bin/config-helloworld.rs | 10 +-- .../config/src/bin/show-resource-history.rs | 12 ++- .../custom-root-certificates/Cargo.toml | 2 +- .../custom-root-certificates/src/main.rs | 6 +- rust_dev_preview/examples/dynamodb/Cargo.toml | 2 +- .../dynamodb/src/bin/list-tables-main.rs | 6 +- rust_dev_preview/examples/ebs/Cargo.toml | 2 +- .../examples/ebs/src/bin/create-snapshot.rs | 17 ++-- rust_dev_preview/examples/ec2/Cargo.toml | 2 +- rust_dev_preview/examples/ecr/Cargo.toml | 2 +- rust_dev_preview/examples/ecs/Cargo.toml | 2 +- rust_dev_preview/examples/eks/Cargo.toml | 2 +- rust_dev_preview/examples/firehose/Cargo.toml | 2 +- .../examples/globalaccelerator/Cargo.toml | 2 +- rust_dev_preview/examples/glue/Cargo.toml | 2 +- .../examples/greengrassv2/Cargo.toml | 2 +- rust_dev_preview/examples/iam/Cargo.toml | 2 +- rust_dev_preview/examples/iot/Cargo.toml | 2 +- rust_dev_preview/examples/kinesis/Cargo.toml | 2 +- rust_dev_preview/examples/kms/Cargo.toml | 2 +- rust_dev_preview/examples/lambda/Cargo.toml | 2 +- .../examples/lambda/src/actions.rs | 13 ++- rust_dev_preview/examples/lambda/src/main.rs | 3 +- .../examples/localstack/Cargo.toml | 2 +- .../localstack/src/bin/use-localstack.rs | 3 +- .../examples/logging/logger/Cargo.toml | 2 +- .../examples/logging/logger/src/main.rs | 6 +- .../examples/logging/tracing/Cargo.toml | 2 +- .../examples/logging/tracing/src/main.rs | 6 +- .../examples/medialive/Cargo.toml | 2 +- .../examples/mediapackage/Cargo.toml | 2 +- rust_dev_preview/examples/polly/Cargo.toml | 2 +- rust_dev_preview/examples/qldb/Cargo.toml | 2 +- rust_dev_preview/examples/rds/Cargo.toml | 2 +- rust_dev_preview/examples/rdsdata/Cargo.toml | 2 +- rust_dev_preview/examples/route53/Cargo.toml | 2 +- rust_dev_preview/examples/s3/Cargo.toml | 2 +- .../examples/s3/src/bin/get-object.rs | 2 +- .../examples/s3/src/bin/if-modified-since.rs | 15 ++-- .../s3/src/bin/put-object-progress.rs | 2 +- .../examples/sagemaker/Cargo.toml | 2 +- .../examples/sdk-config/Cargo.toml | 2 +- .../sdk-config/src/bin/disable_retries.rs | 3 +- .../sdk-config/src/bin/set_retries.rs | 6 +- .../examples/secretsmanager/Cargo.toml | 2 +- .../sending-presigned-requests/Cargo.toml | 2 +- rust_dev_preview/examples/ses/Cargo.toml | 2 +- rust_dev_preview/examples/sitewise/Cargo.toml | 2 +- rust_dev_preview/examples/snowball/Cargo.toml | 2 +- rust_dev_preview/examples/sns/Cargo.toml | 2 +- rust_dev_preview/examples/sqs/Cargo.toml | 2 +- rust_dev_preview/examples/ssm/Cargo.toml | 2 +- .../examples/stepfunction/Cargo.toml | 2 +- rust_dev_preview/examples/sts/Cargo.toml | 2 +- rust_dev_preview/examples/testing/Cargo.toml | 2 +- .../examples/testing/src/replay.rs | 3 + rust_dev_preview/examples/textract/Cargo.toml | 2 +- rust_dev_preview/examples/tls/Cargo.toml | 2 +- rust_dev_preview/examples/tls/src/lib.rs | 6 +- .../examples/transcribestreaming/Cargo.toml | 2 +- rust_dev_preview/test-utils/src/macros.rs | 1 + .../tools/add_aws_config_feature.py | 83 +++++++++++++++++++ rust_dev_preview/webassembly/src/lib.rs | 7 +- 86 files changed, 250 insertions(+), 143 deletions(-) create mode 100755 rust_dev_preview/tools/add_aws_config_feature.py diff --git a/rust_dev_preview/cross_service/detect_faces/src/main.rs b/rust_dev_preview/cross_service/detect_faces/src/main.rs index f0298b13ad1..9396ab8ec14 100644 --- a/rust_dev_preview/cross_service/detect_faces/src/main.rs +++ b/rust_dev_preview/cross_service/detect_faces/src/main.rs @@ -4,6 +4,7 @@ */ use aws_config::meta::region::RegionProviderChain; +use aws_config::BehaviorVersion; use aws_sdk_s3::config::Region; use clap::Parser; use std::error::Error; @@ -205,13 +206,13 @@ async fn main() -> Result<(), Box> { println!(); } - let s3_shared_config = aws_config::from_env() + let s3_shared_config = aws_config::defaults(BehaviorVersion::latest()) .region(s3_region_provider) .load() .await; let s3_client = aws_sdk_s3::Client::new(&s3_shared_config); - let rek_shared_config = aws_config::from_env() + let rek_shared_config = aws_config::defaults(BehaviorVersion::latest()) .region(rek_region_provider) .load() .await; diff --git a/rust_dev_preview/cross_service/detect_labels/src/main.rs b/rust_dev_preview/cross_service/detect_labels/src/main.rs index c5ae6be8d29..862c31a6ce8 100644 --- a/rust_dev_preview/cross_service/detect_labels/src/main.rs +++ b/rust_dev_preview/cross_service/detect_labels/src/main.rs @@ -6,6 +6,7 @@ extern crate exif; use aws_config::meta::region::RegionProviderChain; +use aws_config::BehaviorVersion; use aws_sdk_dynamodb::config::Region; use aws_sdk_dynamodb::types::AttributeValue; use clap::Parser; @@ -279,7 +280,7 @@ async fn main() -> Result<(), exif::Error> { println!(); } - let s3_shared_config = aws_config::from_env() + let s3_shared_config = aws_config::defaults(BehaviorVersion::latest()) .region(s3_region_provider) .load() .await; @@ -289,7 +290,7 @@ async fn main() -> Result<(), exif::Error> { let edata = get_exif_data(&filename); - let rek_shared_config = aws_config::from_env() + let rek_shared_config = aws_config::defaults(BehaviorVersion::latest()) .region(rek_region_provider) .load() .await; @@ -297,7 +298,7 @@ async fn main() -> Result<(), exif::Error> { let labels = get_label_data(&rek_client, &bucket, &filename).await; - let dynamo_shared_config = aws_config::from_env() + let dynamo_shared_config = aws_config::defaults(BehaviorVersion::latest()) .region(dynamo_region_provider) .load() .await; diff --git a/rust_dev_preview/cross_service/photo_asset_management/src/common.rs b/rust_dev_preview/cross_service/photo_asset_management/src/common.rs index 8f6c10e1bd7..7f1eaa90333 100644 --- a/rust_dev_preview/cross_service/photo_asset_management/src/common.rs +++ b/rust_dev_preview/cross_service/photo_asset_management/src/common.rs @@ -1,4 +1,4 @@ -use aws_config::SdkConfig; +use aws_config::{BehaviorVersion, SdkConfig}; #[cfg(not(debug_assertions))] pub fn init_tracing_subscriber() { @@ -62,7 +62,7 @@ impl Common { } pub async fn load_from_env() -> Self { - let sdk_config = aws_config::load_from_env().await; + let sdk_config = aws_config::load_defaults(BehaviorVersion::latest()).await; // PAM environment is declared in the cdk, in lib/backend/lambdas.ts let storage_bucket = std::env::var("STORAGE_BUCKET_NAME").expect("storage bucket in environment"); diff --git a/rust_dev_preview/cross_service/photo_asset_management/src/handlers/detect_labels.rs b/rust_dev_preview/cross_service/photo_asset_management/src/handlers/detect_labels.rs index b0b2ab1bec4..68138ebe0d6 100644 --- a/rust_dev_preview/cross_service/photo_asset_management/src/handlers/detect_labels.rs +++ b/rust_dev_preview/cross_service/photo_asset_management/src/handlers/detect_labels.rs @@ -127,7 +127,7 @@ pub async fn handler( #[cfg(test)] mod test { use super::prepare_update_expression; - use aws_config::SdkConfig; + use aws_config::{BehaviorVersion, SdkConfig}; #[tokio::test] async fn test_prepare_update_statement() { @@ -136,7 +136,11 @@ mod test { .name("label") .build(); - let client = aws_sdk_dynamodb::Client::new(&SdkConfig::builder().build()); + let client = aws_sdk_dynamodb::Client::new( + &SdkConfig::builder() + .behavior_version(BehaviorVersion::latest()) + .build(), + ); let update = client.update_item(); let update = prepare_update_expression(update, &object, &label); diff --git a/rust_dev_preview/cross_service/photo_asset_management/tests/test_upload_part.rs b/rust_dev_preview/cross_service/photo_asset_management/tests/test_upload_part.rs index 3886a8fb668..66cded221f0 100644 --- a/rust_dev_preview/cross_service/photo_asset_management/tests/test_upload_part.rs +++ b/rust_dev_preview/cross_service/photo_asset_management/tests/test_upload_part.rs @@ -1,8 +1,9 @@ +use aws_config::BehaviorVersion; use aws_sdk_s3::Client; #[tokio::main] async fn main() -> Result<(), anyhow::Error> { - let s3_client = Client::new(&aws_config::load_from_env().await); + let s3_client = Client::new(&aws_config::load_defaults(BehaviorVersion::latest()).await); let part = 0; let bucket: String = "bucket".to_string(); let key: String = "key".to_string(); diff --git a/rust_dev_preview/cross_service/rest_ses/src/main.rs b/rust_dev_preview/cross_service/rest_ses/src/main.rs index c3d0e93ea2c..faa5f8f2337 100644 --- a/rust_dev_preview/cross_service/rest_ses/src/main.rs +++ b/rust_dev_preview/cross_service/rest_ses/src/main.rs @@ -8,6 +8,7 @@ //! Main that loads environments & prepares clients, and hands them to `startup`. use std::net::TcpListener; +use aws_config::BehaviorVersion; use rest_ses::client::{RdsClient, SesClient}; use rest_ses::configuration::{get_settings, init_environment}; use rest_ses::startup::run; @@ -30,7 +31,7 @@ async fn main() -> std::io::Result<()> { init_subscriber(subscriber); // AWS Settings (Region & role) come from the environment. - let config = aws_config::from_env().load().await; + let config = aws_config::load_defaults(BehaviorVersion::latest()).await; let rds = RdsClient::new(&settings.rds, &config); let ses = SesClient::new(&settings.ses, &config); diff --git a/rust_dev_preview/cross_service/rest_ses/tests/api/startup.rs b/rust_dev_preview/cross_service/rest_ses/tests/api/startup.rs index a99f9f82b1f..70795fbed58 100644 --- a/rust_dev_preview/cross_service/rest_ses/tests/api/startup.rs +++ b/rust_dev_preview/cross_service/rest_ses/tests/api/startup.rs @@ -3,6 +3,7 @@ * SPDX-License-Identifier: Apache-2.0. */ +use aws_config::BehaviorVersion; use once_cell::sync::Lazy; use rest_ses::{ client::{RdsClient, SesClient}, @@ -27,14 +28,15 @@ static TRACING: Lazy = Lazy::new(|| { /// Spawn the app against a MockServer resolved backend. pub async fn spawn_app_mocked() -> (String, MockServer) { let mock_server = MockServer::builder().start().await; - let config_loader = aws_config::from_env().endpoint_url(mock_server.uri()); + let config_loader = + aws_config::defaults(BehaviorVersion::latest()).endpoint_url(mock_server.uri()); let (app, _) = prep_app(config_loader).await; (app, mock_server) } /// Spawn the app using production AWS credentials. pub async fn spawn_app() -> (String, RdsClient) { - prep_app(aws_config::from_env()).await + prep_app(aws_config::defaults(BehaviorVersion::latest())).await } /// Prepare the application for testing. diff --git a/rust_dev_preview/cross_service/telephone/src/main.rs b/rust_dev_preview/cross_service/telephone/src/main.rs index 46ca79f9a5e..30651242c08 100644 --- a/rust_dev_preview/cross_service/telephone/src/main.rs +++ b/rust_dev_preview/cross_service/telephone/src/main.rs @@ -5,6 +5,7 @@ use anyhow::{Context, Result}; use aws_config::meta::region::RegionProviderChain; +use aws_config::BehaviorVersion; use aws_sdk_polly::config::Region; use aws_sdk_polly::types::{OutputFormat, VoiceId}; use aws_sdk_transcribe::types::{LanguageCode, Media, MediaFormat, TranscriptionJobStatus}; @@ -269,15 +270,15 @@ async fn main() -> Result<(), anyhow::Error> { } // Create configurations for each service client. - let polly_shared_config = aws_config::from_env() + let polly_shared_config = aws_config::defaults(BehaviorVersion::latest()) .region(polly_region_provider) .load() .await; - let s3_shared_config = aws_config::from_env() + let s3_shared_config = aws_config::defaults(BehaviorVersion::latest()) .region(s3_region_provider) .load() .await; - let transcribe_shared_config = aws_config::from_env() + let transcribe_shared_config = aws_config::defaults(BehaviorVersion::latest()) .region(transcribe_region_provider) .load() .await; diff --git a/rust_dev_preview/examples/apigateway/Cargo.toml b/rust_dev_preview/examples/apigateway/Cargo.toml index ee8ead63b19..1320d333869 100644 --- a/rust_dev_preview/examples/apigateway/Cargo.toml +++ b/rust_dev_preview/examples/apigateway/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-apigateway = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-smithy-types-convert = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = [ "convert-chrono", diff --git a/rust_dev_preview/examples/apigatewaymanagement/Cargo.toml b/rust_dev_preview/examples/apigatewaymanagement/Cargo.toml index 0aacb7f2a26..7caec62b3c9 100644 --- a/rust_dev_preview/examples/apigatewaymanagement/Cargo.toml +++ b/rust_dev_preview/examples/apigatewaymanagement/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-apigatewaymanagement = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } http = "0.2.5" tokio = { version = "1.20.1", features = ["full"] } diff --git a/rust_dev_preview/examples/applicationautoscaling/Cargo.toml b/rust_dev_preview/examples/applicationautoscaling/Cargo.toml index 51e4c21f90a..e72ebea4159 100644 --- a/rust_dev_preview/examples/applicationautoscaling/Cargo.toml +++ b/rust_dev_preview/examples/applicationautoscaling/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-applicationautoscaling = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/aurora/Cargo.toml b/rust_dev_preview/examples/aurora/Cargo.toml index c2a8f6760fa..7814c8c4f19 100644 --- a/rust_dev_preview/examples/aurora/Cargo.toml +++ b/rust_dev_preview/examples/aurora/Cargo.toml @@ -11,7 +11,7 @@ version = "0.1.0" [dependencies] anyhow = "1.0.75" assert_matches = "1.5.0" -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-smithy-types = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-smithy-runtime-api = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-sdk-rds = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } diff --git a/rust_dev_preview/examples/aurora/src/aurora_scenario/tests.rs b/rust_dev_preview/examples/aurora/src/aurora_scenario/tests.rs index 317e02c8a43..54f0ce1dd62 100644 --- a/rust_dev_preview/examples/aurora/src/aurora_scenario/tests.rs +++ b/rust_dev_preview/examples/aurora/src/aurora_scenario/tests.rs @@ -39,7 +39,7 @@ use aws_sdk_rds::{ OrderableDbInstanceOption, }, }; -use aws_smithy_runtime_api::client::orchestrator::HttpResponse; +use aws_smithy_runtime_api::http::{Response, StatusCode}; use aws_smithy_types::body::SdkBody; use mockall::predicate::eq; use secrecy::ExposeSecret; @@ -103,7 +103,7 @@ async fn test_scenario_set_engine_param_group_exists() { CreateDBClusterParameterGroupError::DbParameterGroupAlreadyExistsFault( DbParameterGroupAlreadyExistsFault::builder().build(), ), - HttpResponse::new(SdkBody::empty()), + Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()), )) }); @@ -173,7 +173,7 @@ async fn test_scenario_get_engines_failed() { ErrorKind::Other, "describe_db_engine_versions error", ))), - HttpResponse::new(SdkBody::empty()), + Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()), )) }); @@ -244,7 +244,7 @@ async fn test_scenario_get_instance_classes_error() { ErrorKind::Other, "describe_orderable_db_instance_options_error", ))), - HttpResponse::new(SdkBody::empty()), + Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()), )) }); @@ -327,7 +327,7 @@ async fn test_scenario_get_cluster_error() { ErrorKind::Other, "describe_db_clusters_error", ))), - HttpResponse::new(SdkBody::empty()), + Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()), )) }); @@ -419,7 +419,7 @@ async fn test_scenario_cluster_parameters_error() { ErrorKind::Other, "describe_db_cluster_parameters_error", ))), - HttpResponse::new(SdkBody::empty()), + Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()), )) }); @@ -478,7 +478,7 @@ async fn test_scenario_update_auto_increment_error() { ErrorKind::Other, "modify_db_cluster_parameter_group_error", ))), - HttpResponse::new(SdkBody::empty()), + Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()), )) }); @@ -602,7 +602,7 @@ async fn test_start_cluster_and_instance_cluster_create_error() { ErrorKind::Other, "create db cluster error", ))), - HttpResponse::new(SdkBody::empty()), + Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()), )) }); @@ -667,7 +667,7 @@ async fn test_start_cluster_and_instance_instance_create_error() { ErrorKind::Other, "create db instance error", ))), - HttpResponse::new(SdkBody::empty()), + Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()), )) }); @@ -733,7 +733,7 @@ async fn test_start_cluster_and_instance_wait_hiccup() { ErrorKind::Other, "describe cluster error", ))), - HttpResponse::new(SdkBody::empty()), + Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()), )) }) .with(eq("RustSDKCodeExamplesDBCluster")) @@ -892,7 +892,7 @@ async fn test_scenario_clean_up_errors() { ErrorKind::Other, "describe db instances error", ))), - HttpResponse::new(SdkBody::empty()), + Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()), )) }); @@ -923,7 +923,7 @@ async fn test_scenario_clean_up_errors() { ErrorKind::Other, "describe db clusters error", ))), - HttpResponse::new(SdkBody::empty()), + Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()), )) }); @@ -1000,7 +1000,7 @@ async fn test_scenario_snapshot_error() { ErrorKind::Other, "create snapshot error", ))), - HttpResponse::new(SdkBody::empty()), + Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()), )) }); diff --git a/rust_dev_preview/examples/auto-scaling/Cargo.toml b/rust_dev_preview/examples/auto-scaling/Cargo.toml index e24873e947d..8aac60e63b5 100644 --- a/rust_dev_preview/examples/auto-scaling/Cargo.toml +++ b/rust_dev_preview/examples/auto-scaling/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-autoscaling = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-sdk-ec2 = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-types = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } diff --git a/rust_dev_preview/examples/autoscalingplans/Cargo.toml b/rust_dev_preview/examples/autoscalingplans/Cargo.toml index 29cbf65ecde..d8e183ab5e7 100644 --- a/rust_dev_preview/examples/autoscalingplans/Cargo.toml +++ b/rust_dev_preview/examples/autoscalingplans/Cargo.toml @@ -11,7 +11,7 @@ edition = "2021" [dependencies] aws-sdk-autoscalingplans = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-types = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/batch/Cargo.toml b/rust_dev_preview/examples/batch/Cargo.toml index 68079b586ba..ef0193ceddc 100644 --- a/rust_dev_preview/examples/batch/Cargo.toml +++ b/rust_dev_preview/examples/batch/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-batch = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/cloudformation/Cargo.toml b/rust_dev_preview/examples/cloudformation/Cargo.toml index de701d2ba24..780db75623a 100644 --- a/rust_dev_preview/examples/cloudformation/Cargo.toml +++ b/rust_dev_preview/examples/cloudformation/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-cloudformation = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/cloudwatch/Cargo.toml b/rust_dev_preview/examples/cloudwatch/Cargo.toml index 71df748c7b4..6d54cc9c6af 100644 --- a/rust_dev_preview/examples/cloudwatch/Cargo.toml +++ b/rust_dev_preview/examples/cloudwatch/Cargo.toml @@ -11,7 +11,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-cloudwatch = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/cloudwatchlogs/Cargo.toml b/rust_dev_preview/examples/cloudwatchlogs/Cargo.toml index deeb2e6a5da..93e4d43fe27 100644 --- a/rust_dev_preview/examples/cloudwatchlogs/Cargo.toml +++ b/rust_dev_preview/examples/cloudwatchlogs/Cargo.toml @@ -11,7 +11,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-cloudwatchlogs = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-types = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } diff --git a/rust_dev_preview/examples/cognitoidentity/Cargo.toml b/rust_dev_preview/examples/cognitoidentity/Cargo.toml index 612a15f9592..def2d2a5efc 100644 --- a/rust_dev_preview/examples/cognitoidentity/Cargo.toml +++ b/rust_dev_preview/examples/cognitoidentity/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-cognitoidentity = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-smithy-types-convert = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = [ "convert-chrono", diff --git a/rust_dev_preview/examples/cognitoidentityprovider/Cargo.toml b/rust_dev_preview/examples/cognitoidentityprovider/Cargo.toml index 884083a312b..5e011e552f2 100644 --- a/rust_dev_preview/examples/cognitoidentityprovider/Cargo.toml +++ b/rust_dev_preview/examples/cognitoidentityprovider/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-cognitoidentityprovider = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-smithy-types-convert = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = [ "convert-chrono", diff --git a/rust_dev_preview/examples/cognitosync/Cargo.toml b/rust_dev_preview/examples/cognitosync/Cargo.toml index 75ff49f5131..08eec56b564 100644 --- a/rust_dev_preview/examples/cognitosync/Cargo.toml +++ b/rust_dev_preview/examples/cognitosync/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-cognitosync = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-smithy-types-convert = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = [ "convert-chrono", diff --git a/rust_dev_preview/examples/config/Cargo.toml b/rust_dev_preview/examples/config/Cargo.toml index da03a3ccb5a..1aa9fc488c4 100644 --- a/rust_dev_preview/examples/config/Cargo.toml +++ b/rust_dev_preview/examples/config/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/config/src/bin/config-helloworld.rs b/rust_dev_preview/examples/config/src/bin/config-helloworld.rs index c388a551fea..732f95ad805 100644 --- a/rust_dev_preview/examples/config/src/bin/config-helloworld.rs +++ b/rust_dev_preview/examples/config/src/bin/config-helloworld.rs @@ -89,14 +89,14 @@ async fn main() -> Result<(), Error> { let client = Client::new(&shared_config); // parse resource type from user input - let parsed = ResourceType::from(resource_type.as_str()); - if matches!(parsed, ResourceType::Unknown(_)) { - panic!( + let parsed = match ResourceType::try_parse(resource_type.as_str()) { + Ok(parsed) => parsed, + Err(_) => panic!( "unknown resource type: `{}`. Valid resource types: {:#?}", &resource_type, ResourceType::values() - ) - } + ), + }; get_history(&client, &resource_id, parsed).await } diff --git a/rust_dev_preview/examples/config/src/bin/show-resource-history.rs b/rust_dev_preview/examples/config/src/bin/show-resource-history.rs index 4726f756f3f..48e2e918f66 100644 --- a/rust_dev_preview/examples/config/src/bin/show-resource-history.rs +++ b/rust_dev_preview/examples/config/src/bin/show-resource-history.rs @@ -85,16 +85,14 @@ async fn main() -> Result<(), Error> { } // Parse resource type from user input. - let parsed = ResourceType::from(resource_type.as_str()); - - // Make sure it's a known type. - if matches!(parsed, ResourceType::Unknown(_)) { - panic!( + let parsed = match ResourceType::try_parse(resource_type.as_str()) { + Ok(parsed) => parsed, + Err(_) => panic!( "unknown resource type: `{}`. Valid resource types: {:#?}", &resource_type, ResourceType::values() - ) - } + ), + }; let shared_config = aws_config::from_env().region(region_provider).load().await; let client = Client::new(&shared_config); diff --git a/rust_dev_preview/examples/custom-root-certificates/Cargo.toml b/rust_dev_preview/examples/custom-root-certificates/Cargo.toml index a3605ff91e5..643fcf13761 100644 --- a/rust_dev_preview/examples/custom-root-certificates/Cargo.toml +++ b/rust_dev_preview/examples/custom-root-certificates/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" description = "An example demonstrating setting a custom root certificate with rustls" [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} # bringing our own HTTPs so no need for the default features aws-sdk-s3 = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", default-features = false } tokio = { version = "1.21.2", features = ["full"] } diff --git a/rust_dev_preview/examples/custom-root-certificates/src/main.rs b/rust_dev_preview/examples/custom-root-certificates/src/main.rs index 2f118a3129e..c762f6600ec 100644 --- a/rust_dev_preview/examples/custom-root-certificates/src/main.rs +++ b/rust_dev_preview/examples/custom-root-certificates/src/main.rs @@ -4,9 +4,7 @@ */ use aws_config::provider_config::ProviderConfig; -use aws_smithy_client::erase::DynConnector; -use aws_smithy_client::http_connector::HttpConnector; -use aws_smithy_client::hyper_ext; +use aws_config::BehaviorVersion; use rustls::RootCertStore; use std::sync::Arc; @@ -28,7 +26,7 @@ async fn main() { // Currently, aws_config connectors are buildable directly from something that implements `hyper::Connect`. // This enables different providers to construct clients with different timeouts. let provider_config = ProviderConfig::default().with_tcp_connector(rustls_connector.clone()); - let sdk_config = aws_config::from_env() + let sdk_config = aws_config::defaults(BehaviorVersion::latest()) .configure(provider_config) .http_connector(HttpConnector::ConnectorFn(Arc::new( move |connector_settings, sleep_impl| { diff --git a/rust_dev_preview/examples/dynamodb/Cargo.toml b/rust_dev_preview/examples/dynamodb/Cargo.toml index 259bbaf7ef5..b373f78c9c1 100644 --- a/rust_dev_preview/examples/dynamodb/Cargo.toml +++ b/rust_dev_preview/examples/dynamodb/Cargo.toml @@ -11,7 +11,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-http = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-sdk-dynamodb = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-smithy-runtime = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["test-util"] } diff --git a/rust_dev_preview/examples/dynamodb/src/bin/list-tables-main.rs b/rust_dev_preview/examples/dynamodb/src/bin/list-tables-main.rs index 1a3bb6e9d72..3355655e04c 100644 --- a/rust_dev_preview/examples/dynamodb/src/bin/list-tables-main.rs +++ b/rust_dev_preview/examples/dynamodb/src/bin/list-tables-main.rs @@ -7,13 +7,17 @@ // snippet-start:[dynamodb.rust.list-tables-main] use aws_config::meta::region::RegionProviderChain; +use aws_config::BehaviorVersion; use aws_sdk_dynamodb::{Client, Error}; /// Lists your DynamoDB tables in the default Region or us-east-1 if a default Region isn't set. #[tokio::main] async fn main() -> Result<(), Error> { let region_provider = RegionProviderChain::default_provider().or_else("us-east-1"); - let config = aws_config::from_env().region(region_provider).load().await; + let config = aws_config::defaults(BehaviorVersion::latest()) + .region(region_provider) + .load() + .await; let client = Client::new(&config); let resp = client.list_tables().send().await?; diff --git a/rust_dev_preview/examples/ebs/Cargo.toml b/rust_dev_preview/examples/ebs/Cargo.toml index 40f689bde8e..631dd8f1f24 100644 --- a/rust_dev_preview/examples/ebs/Cargo.toml +++ b/rust_dev_preview/examples/ebs/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-ebs = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-sdk-ec2 = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } diff --git a/rust_dev_preview/examples/ebs/src/bin/create-snapshot.rs b/rust_dev_preview/examples/ebs/src/bin/create-snapshot.rs index 2af75311ba2..29cf0c5abf7 100644 --- a/rust_dev_preview/examples/ebs/src/bin/create-snapshot.rs +++ b/rust_dev_preview/examples/ebs/src/bin/create-snapshot.rs @@ -126,17 +126,12 @@ async fn main() -> Result<(), Error> { let snapshot_id = start(&client, &description).await.unwrap(); - let mut blocks = vec![]; - - // Append a block of all 1s. - let mut block: Vec = Vec::new(); - block.resize(EBS_BLOCK_SIZE, 1); - blocks.push(block); - - // Append a block of all 0s. - let mut block: Vec = Vec::new(); - block.resize(EBS_BLOCK_SIZE, 0); - blocks.push(block); + let blocks = vec![ + // Block of all 1s. + vec![1u8; EBS_BLOCK_SIZE], + // Block of all 0s. + vec![0u8; EBS_BLOCK_SIZE], + ]; for (idx, block) in blocks.into_iter().enumerate() { let mut hasher = sha2::Sha256::new(); diff --git a/rust_dev_preview/examples/ec2/Cargo.toml b/rust_dev_preview/examples/ec2/Cargo.toml index d8bafb16102..4d1048aae01 100644 --- a/rust_dev_preview/examples/ec2/Cargo.toml +++ b/rust_dev_preview/examples/ec2/Cargo.toml @@ -11,7 +11,7 @@ edition = "2021" [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-ec2 = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-types = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } diff --git a/rust_dev_preview/examples/ecr/Cargo.toml b/rust_dev_preview/examples/ecr/Cargo.toml index 02a30cbe237..0efa57fed1c 100644 --- a/rust_dev_preview/examples/ecr/Cargo.toml +++ b/rust_dev_preview/examples/ecr/Cargo.toml @@ -8,7 +8,7 @@ authors = [ edition = "2021" [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-ecr = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-types = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } diff --git a/rust_dev_preview/examples/ecs/Cargo.toml b/rust_dev_preview/examples/ecs/Cargo.toml index d4f167ad6a5..d3423a58845 100644 --- a/rust_dev_preview/examples/ecs/Cargo.toml +++ b/rust_dev_preview/examples/ecs/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-ecs = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/eks/Cargo.toml b/rust_dev_preview/examples/eks/Cargo.toml index 2b52c925b5b..52aa98af35f 100644 --- a/rust_dev_preview/examples/eks/Cargo.toml +++ b/rust_dev_preview/examples/eks/Cargo.toml @@ -8,7 +8,7 @@ authors = [ edition = "2021" [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-eks = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-types = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } diff --git a/rust_dev_preview/examples/firehose/Cargo.toml b/rust_dev_preview/examples/firehose/Cargo.toml index 17355aca34e..1a2191507aa 100644 --- a/rust_dev_preview/examples/firehose/Cargo.toml +++ b/rust_dev_preview/examples/firehose/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-firehose = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/globalaccelerator/Cargo.toml b/rust_dev_preview/examples/globalaccelerator/Cargo.toml index bbfc8931f23..1016d7ef15c 100644 --- a/rust_dev_preview/examples/globalaccelerator/Cargo.toml +++ b/rust_dev_preview/examples/globalaccelerator/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-globalaccelerator = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } tokio-stream = "0.1.8" diff --git a/rust_dev_preview/examples/glue/Cargo.toml b/rust_dev_preview/examples/glue/Cargo.toml index 5cd8e2a768b..648de15c84a 100644 --- a/rust_dev_preview/examples/glue/Cargo.toml +++ b/rust_dev_preview/examples/glue/Cargo.toml @@ -13,7 +13,7 @@ name = "scenario" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-glue = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-sdk-s3 = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-http = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } diff --git a/rust_dev_preview/examples/greengrassv2/Cargo.toml b/rust_dev_preview/examples/greengrassv2/Cargo.toml index bd42e8c7fa5..b3f81f2448f 100644 --- a/rust_dev_preview/examples/greengrassv2/Cargo.toml +++ b/rust_dev_preview/examples/greengrassv2/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Aaron Tsui "] edition = "2021" [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-greengrassv2 = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-types = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1", features = ["full"] } diff --git a/rust_dev_preview/examples/iam/Cargo.toml b/rust_dev_preview/examples/iam/Cargo.toml index 3a764467e29..adea5f7653b 100644 --- a/rust_dev_preview/examples/iam/Cargo.toml +++ b/rust_dev_preview/examples/iam/Cargo.toml @@ -17,7 +17,7 @@ name = "iam_getting_started" path = "src/bin/iam-getting-started.rs" [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-credential-types = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["hardcoded-credentials"] } aws-sdk-iam = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-sdk-s3 = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } diff --git a/rust_dev_preview/examples/iot/Cargo.toml b/rust_dev_preview/examples/iot/Cargo.toml index 14baa96e34e..bdd1f95a6f6 100644 --- a/rust_dev_preview/examples/iot/Cargo.toml +++ b/rust_dev_preview/examples/iot/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-iot = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-types = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } diff --git a/rust_dev_preview/examples/kinesis/Cargo.toml b/rust_dev_preview/examples/kinesis/Cargo.toml index 8770ea46185..6e4a0bd554a 100644 --- a/rust_dev_preview/examples/kinesis/Cargo.toml +++ b/rust_dev_preview/examples/kinesis/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-kinesis = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/kms/Cargo.toml b/rust_dev_preview/examples/kms/Cargo.toml index ba1ebaf9e52..92c96004ca4 100644 --- a/rust_dev_preview/examples/kms/Cargo.toml +++ b/rust_dev_preview/examples/kms/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" description = "Example usage of the KMS service" [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-kms = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } base64 = "0.13.0" diff --git a/rust_dev_preview/examples/lambda/Cargo.toml b/rust_dev_preview/examples/lambda/Cargo.toml index 2352f4c5b35..252b890d356 100644 --- a/rust_dev_preview/examples/lambda/Cargo.toml +++ b/rust_dev_preview/examples/lambda/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-ec2 = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-sdk-iam = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-sdk-lambda = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } diff --git a/rust_dev_preview/examples/lambda/src/actions.rs b/rust_dev_preview/examples/lambda/src/actions.rs index 7b822b1e229..fb08df06b96 100644 --- a/rust_dev_preview/examples/lambda/src/actions.rs +++ b/rust_dev_preview/examples/lambda/src/actions.rs @@ -22,7 +22,7 @@ use aws_sdk_s3::{ use aws_smithy_types::Blob; use serde::{ser::SerializeMap, Serialize}; use std::{path::PathBuf, str::FromStr, time::Duration}; -use tracing::{debug, error, info, warn}; +use tracing::{debug, info, warn}; /* Operation describes */ #[derive(Clone, Copy, Debug, Serialize)] @@ -345,16 +345,15 @@ impl LambdaManager { LastUpdateStatus::Failed | LastUpdateStatus::InProgress => { return Ok(false); } - LastUpdateStatus::Unknown(status_variant) => { - warn!(?status_variant, "LastUpdateStatus unknown"); + unknown => { + warn!( + status_variant = unknown.as_str(), + "LastUpdateStatus unknown" + ); return Err(anyhow!( "Unknown LastUpdateStatus, fn config is {config:?}" )); } - _ => { - error!("Unmatched LastUpdateStatus"); - return Err(anyhow!("Unmatched LastUpdateStatus")); - } } } None => { diff --git a/rust_dev_preview/examples/lambda/src/main.rs b/rust_dev_preview/examples/lambda/src/main.rs index 12d3b12f9d7..71465f29c57 100644 --- a/rust_dev_preview/examples/lambda/src/main.rs +++ b/rust_dev_preview/examples/lambda/src/main.rs @@ -3,6 +3,7 @@ * SPDX-License-Identifier: Apache-2.0. */ // snippet-start:[lambda.rust.main] +use aws_config::BehaviorVersion; use lambda_runtime::{service_fn, Error, LambdaEvent}; use serde::{Deserialize, Serialize}; use std::time::SystemTime; @@ -99,7 +100,7 @@ async fn main() -> Result<(), Error> { // // No extra configuration is needed as long as your Lambda has // the necessary permissions attached to its role. - let config = aws_config::load_from_env().await; + let config = aws_config::load_defaults(BehaviorVersion::latest()).await; let s3_client = aws_sdk_s3::Client::new(&config); lambda_runtime::run(service_fn(|event: LambdaEvent| async { diff --git a/rust_dev_preview/examples/localstack/Cargo.toml b/rust_dev_preview/examples/localstack/Cargo.toml index 89d6895933e..ceee085ec64 100644 --- a/rust_dev_preview/examples/localstack/Cargo.toml +++ b/rust_dev_preview/examples/localstack/Cargo.toml @@ -6,7 +6,7 @@ authors = ["Doug Schwartz "] edition = "2021" [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-s3 = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-sdk-sqs = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } diff --git a/rust_dev_preview/examples/localstack/src/bin/use-localstack.rs b/rust_dev_preview/examples/localstack/src/bin/use-localstack.rs index 21b5eb9da28..8b63451a265 100644 --- a/rust_dev_preview/examples/localstack/src/bin/use-localstack.rs +++ b/rust_dev_preview/examples/localstack/src/bin/use-localstack.rs @@ -6,13 +6,14 @@ #![allow(clippy::result_large_err)] // snippet-start:[localstack.rust.use-localstack] +use aws_config::BehaviorVersion; use std::error::Error; #[tokio::main] async fn main() -> Result<(), Box> { tracing_subscriber::fmt::init(); - let shared_config = aws_config::from_env().load().await; + let shared_config = aws_config::load_defaults(BehaviorVersion::latest()).await; let sqs_client = sqs_client(&shared_config); let s3_client = s3_client(&shared_config); diff --git a/rust_dev_preview/examples/logging/logger/Cargo.toml b/rust_dev_preview/examples/logging/logger/Cargo.toml index 90b72ceb937..309b3d5da8e 100644 --- a/rust_dev_preview/examples/logging/logger/Cargo.toml +++ b/rust_dev_preview/examples/logging/logger/Cargo.toml @@ -11,7 +11,7 @@ version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-dynamodb = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } # snippet-start:[logging.rust.logger-cargo.toml-env_logger] env_logger = "0.9.0" diff --git a/rust_dev_preview/examples/logging/logger/src/main.rs b/rust_dev_preview/examples/logging/logger/src/main.rs index 70aa8539cba..5e26de4d03a 100644 --- a/rust_dev_preview/examples/logging/logger/src/main.rs +++ b/rust_dev_preview/examples/logging/logger/src/main.rs @@ -7,6 +7,7 @@ // snippet-start:[logging.rust.main] use aws_config::meta::region::RegionProviderChain; +use aws_config::BehaviorVersion; use aws_sdk_dynamodb::{config::Region, meta::PKG_VERSION, Client, Error}; use clap::Parser; @@ -45,7 +46,10 @@ async fn main() -> Result<(), Error> { println!(); } - let shared_config = aws_config::from_env().region(region_provider).load().await; + let shared_config = aws_config::defaults(BehaviorVersion::latest()) + .region(region_provider) + .load() + .await; let client = Client::new(&shared_config); let resp = client.list_tables().send().await?; diff --git a/rust_dev_preview/examples/logging/tracing/Cargo.toml b/rust_dev_preview/examples/logging/tracing/Cargo.toml index ace0828c4e1..2e7fc320f18 100644 --- a/rust_dev_preview/examples/logging/tracing/Cargo.toml +++ b/rust_dev_preview/examples/logging/tracing/Cargo.toml @@ -11,7 +11,7 @@ version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-dynamodb = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } clap = { version = "~4.4", features = ["derive"] } tokio = { version = "1.20.1", features = ["full"] } diff --git a/rust_dev_preview/examples/logging/tracing/src/main.rs b/rust_dev_preview/examples/logging/tracing/src/main.rs index e1086bbd15f..86e024cf7d1 100644 --- a/rust_dev_preview/examples/logging/tracing/src/main.rs +++ b/rust_dev_preview/examples/logging/tracing/src/main.rs @@ -7,6 +7,7 @@ // snippet-start:[tracing.rust.main] use aws_config::meta::region::RegionProviderChain; +use aws_config::BehaviorVersion; use aws_sdk_dynamodb::{config::Region, meta::PKG_VERSION, Client, Error}; use clap::Parser; @@ -45,7 +46,10 @@ async fn main() -> Result<(), Error> { println!(); } - let shared_config = aws_config::from_env().region(region_provider).load().await; + let shared_config = aws_config::defaults(BehaviorVersion::latest()) + .region(region_provider) + .load() + .await; let client = Client::new(&shared_config); let resp = client.list_tables().send().await?; diff --git a/rust_dev_preview/examples/medialive/Cargo.toml b/rust_dev_preview/examples/medialive/Cargo.toml index b662e554659..72d18f02230 100644 --- a/rust_dev_preview/examples/medialive/Cargo.toml +++ b/rust_dev_preview/examples/medialive/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-medialive = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/mediapackage/Cargo.toml b/rust_dev_preview/examples/mediapackage/Cargo.toml index c91c4f34f5e..9c17fd2c209 100644 --- a/rust_dev_preview/examples/mediapackage/Cargo.toml +++ b/rust_dev_preview/examples/mediapackage/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-mediapackage = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/polly/Cargo.toml b/rust_dev_preview/examples/polly/Cargo.toml index b8d63f71a64..588c3c06e0c 100644 --- a/rust_dev_preview/examples/polly/Cargo.toml +++ b/rust_dev_preview/examples/polly/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-polly = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } bytes = "1" diff --git a/rust_dev_preview/examples/qldb/Cargo.toml b/rust_dev_preview/examples/qldb/Cargo.toml index e1c64525f17..7001e026514 100644 --- a/rust_dev_preview/examples/qldb/Cargo.toml +++ b/rust_dev_preview/examples/qldb/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-qldb = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-sdk-qldbsession = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } diff --git a/rust_dev_preview/examples/rds/Cargo.toml b/rust_dev_preview/examples/rds/Cargo.toml index 5c8be6e452c..962f3be3a41 100644 --- a/rust_dev_preview/examples/rds/Cargo.toml +++ b/rust_dev_preview/examples/rds/Cargo.toml @@ -14,7 +14,7 @@ version = "0.1.0" anyhow = "1.0.75" assert_matches = "1.5.0" async-trait = "0.1.73" -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-smithy-http = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-smithy-runtime-api = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-sdk-rds = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } diff --git a/rust_dev_preview/examples/rdsdata/Cargo.toml b/rust_dev_preview/examples/rdsdata/Cargo.toml index 6591e3ee8f9..3356e5b6f4c 100644 --- a/rust_dev_preview/examples/rdsdata/Cargo.toml +++ b/rust_dev_preview/examples/rdsdata/Cargo.toml @@ -10,7 +10,7 @@ version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-rdsdata = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/route53/Cargo.toml b/rust_dev_preview/examples/route53/Cargo.toml index 7ac833d7e3c..5a4d7a72f9b 100644 --- a/rust_dev_preview/examples/route53/Cargo.toml +++ b/rust_dev_preview/examples/route53/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-route53 = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/s3/Cargo.toml b/rust_dev_preview/examples/s3/Cargo.toml index 7bbdfb0efd1..7eb878e1b4e 100644 --- a/rust_dev_preview/examples/s3/Cargo.toml +++ b/rust_dev_preview/examples/s3/Cargo.toml @@ -21,7 +21,7 @@ assert_cmd = "2.0" predicates = "3.0.3" [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} # snippet-start:[s3.rust.s3-object-lambda-cargo.toml] aws-endpoint = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } # snippet-end:[s3.rust.s3-object-lambda-cargo.toml] diff --git a/rust_dev_preview/examples/s3/src/bin/get-object.rs b/rust_dev_preview/examples/s3/src/bin/get-object.rs index 27d3215ce6b..a3cae7ca373 100644 --- a/rust_dev_preview/examples/s3/src/bin/get-object.rs +++ b/rust_dev_preview/examples/s3/src/bin/get-object.rs @@ -3,7 +3,7 @@ use std::{fs::File, io::Write, path::PathBuf, process::exit}; use aws_sdk_s3::Client; use clap::Parser; -use tracing::log::trace; +use tracing::trace; #[derive(Debug, Parser)] struct Opt { diff --git a/rust_dev_preview/examples/s3/src/bin/if-modified-since.rs b/rust_dev_preview/examples/s3/src/bin/if-modified-since.rs index 9595c1ea96e..8a251c81d11 100644 --- a/rust_dev_preview/examples/s3/src/bin/if-modified-since.rs +++ b/rust_dev_preview/examples/s3/src/bin/if-modified-since.rs @@ -10,7 +10,6 @@ use aws_sdk_s3::{ primitives::{ByteStream, DateTime, DateTimeFormat}, Client, Error, }; -use http::StatusCode; use tracing::{error, warn}; const KEY: &str = "key"; @@ -85,21 +84,17 @@ async fn main() -> Result<(), Error> { Err(err) => match err { SdkError::ServiceError(err) => { let http = err.raw(); - match http.status() { - StatusCode::NOT_MODIFIED => ( + match http.status().as_u16() { + // HTTP 304: not modified + 304 => ( Ok(Some( DateTime::from_str( - http.headers() - .get("last-modified") - .map(|t| t.to_str().unwrap()) - .unwrap(), + http.headers().get("last-modified").unwrap(), DateTimeFormat::HttpDate, ) .unwrap(), )), - http.headers() - .get("etag") - .map(|t| t.to_str().unwrap().into()), + http.headers().get("etag").map(|t| t.into()), ), _ => (Err(SdkError::ServiceError(err)), None), } diff --git a/rust_dev_preview/examples/s3/src/bin/put-object-progress.rs b/rust_dev_preview/examples/s3/src/bin/put-object-progress.rs index 9f47c536254..be73f00b14e 100644 --- a/rust_dev_preview/examples/s3/src/bin/put-object-progress.rs +++ b/rust_dev_preview/examples/s3/src/bin/put-object-progress.rs @@ -11,7 +11,7 @@ use aws_sdk_s3::{ primitives::{ByteStream, SdkBody}, Client, }; -use aws_smithy_runtime_api::client::http::request::Request; +use aws_smithy_runtime_api::http::Request; use bytes::Bytes; use clap::Parser; use http_body::{Body, SizeHint}; diff --git a/rust_dev_preview/examples/sagemaker/Cargo.toml b/rust_dev_preview/examples/sagemaker/Cargo.toml index 8904a80d4e6..ddfc727492b 100644 --- a/rust_dev_preview/examples/sagemaker/Cargo.toml +++ b/rust_dev_preview/examples/sagemaker/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-sagemaker = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-smithy-types-convert = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = [ "convert-chrono", diff --git a/rust_dev_preview/examples/sdk-config/Cargo.toml b/rust_dev_preview/examples/sdk-config/Cargo.toml index da5e527a895..865fcf3413c 100644 --- a/rust_dev_preview/examples/sdk-config/Cargo.toml +++ b/rust_dev_preview/examples/sdk-config/Cargo.toml @@ -11,7 +11,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-credential-types = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-sdk-s3 = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } diff --git a/rust_dev_preview/examples/sdk-config/src/bin/disable_retries.rs b/rust_dev_preview/examples/sdk-config/src/bin/disable_retries.rs index ebfb893f4ad..e200ef85a4b 100644 --- a/rust_dev_preview/examples/sdk-config/src/bin/disable_retries.rs +++ b/rust_dev_preview/examples/sdk-config/src/bin/disable_retries.rs @@ -6,6 +6,7 @@ #![allow(clippy::result_large_err)] use aws_config::meta::region::RegionProviderChain; +use aws_config::BehaviorVersion; use aws_sdk_s3::config::retry::RetryConfig; use aws_sdk_s3::{config::Region, meta::PKG_VERSION, Client, Error}; use clap::Parser; @@ -58,7 +59,7 @@ async fn main() -> Result<(), Error> { } // snippet-start:[custom_retries.rust.disable_retries] - let shared_config = aws_config::from_env() + let shared_config = aws_config::defaults(BehaviorVersion::latest()) .region(region_provider) // Disable retries .retry_config(RetryConfig::disabled()) diff --git a/rust_dev_preview/examples/sdk-config/src/bin/set_retries.rs b/rust_dev_preview/examples/sdk-config/src/bin/set_retries.rs index c8ec67e969e..ddce0e10215 100644 --- a/rust_dev_preview/examples/sdk-config/src/bin/set_retries.rs +++ b/rust_dev_preview/examples/sdk-config/src/bin/set_retries.rs @@ -6,6 +6,7 @@ #![allow(clippy::result_large_err)] use aws_config::meta::region::RegionProviderChain; +use aws_config::BehaviorVersion; use aws_sdk_s3::config::retry::RetryConfig; use aws_sdk_s3::{config, config::Region, meta::PKG_VERSION, Client, Error}; use clap::Parser; @@ -71,7 +72,10 @@ async fn main() -> Result<(), Error> { assert_ne!(tries, 0, "You cannot set zero retries."); // snippet-start:[custom_retries.rust.set_retries] - let shared_config = aws_config::from_env().region(region_provider).load().await; + let shared_config = aws_config::defaults(BehaviorVersion::latest()) + .region(region_provider) + .load() + .await; // Construct an S3 client with customized retry configuration. let client = Client::from_conf( diff --git a/rust_dev_preview/examples/secretsmanager/Cargo.toml b/rust_dev_preview/examples/secretsmanager/Cargo.toml index 889f1f74318..7b9fd05c513 100644 --- a/rust_dev_preview/examples/secretsmanager/Cargo.toml +++ b/rust_dev_preview/examples/secretsmanager/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" description = "Example usage of the SecretManager service" [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-secretsmanager = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/sending-presigned-requests/Cargo.toml b/rust_dev_preview/examples/sending-presigned-requests/Cargo.toml index 7d326721311..9ce803de70d 100644 --- a/rust_dev_preview/examples/sending-presigned-requests/Cargo.toml +++ b/rust_dev_preview/examples/sending-presigned-requests/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-s3 = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-smithy-client = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } http = "0.2.6" diff --git a/rust_dev_preview/examples/ses/Cargo.toml b/rust_dev_preview/examples/ses/Cargo.toml index b9ef5b5c0d8..db5d314cfad 100644 --- a/rust_dev_preview/examples/ses/Cargo.toml +++ b/rust_dev_preview/examples/ses/Cargo.toml @@ -8,7 +8,7 @@ authors = [ edition = "2021" [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-sesv2 = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/sitewise/Cargo.toml b/rust_dev_preview/examples/sitewise/Cargo.toml index be1f3aaec1c..b9ffcb23e4a 100644 --- a/rust_dev_preview/examples/sitewise/Cargo.toml +++ b/rust_dev_preview/examples/sitewise/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" # For more keys and their definitions, see https://doc.rust-lang.org/cargo/reference/manifest.html. [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-iotsitewise = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-smithy-types-convert = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = [ "convert-chrono", diff --git a/rust_dev_preview/examples/snowball/Cargo.toml b/rust_dev_preview/examples/snowball/Cargo.toml index 97f9f5e487e..0c69f54444d 100644 --- a/rust_dev_preview/examples/snowball/Cargo.toml +++ b/rust_dev_preview/examples/snowball/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-snowball = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/sns/Cargo.toml b/rust_dev_preview/examples/sns/Cargo.toml index 57b95ba165d..74643a5bb7b 100644 --- a/rust_dev_preview/examples/sns/Cargo.toml +++ b/rust_dev_preview/examples/sns/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-sns = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/sqs/Cargo.toml b/rust_dev_preview/examples/sqs/Cargo.toml index 19d8b3d738f..3c1cfb27547 100644 --- a/rust_dev_preview/examples/sqs/Cargo.toml +++ b/rust_dev_preview/examples/sqs/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-sqs = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/ssm/Cargo.toml b/rust_dev_preview/examples/ssm/Cargo.toml index b97f44573fd..73735f7455e 100644 --- a/rust_dev_preview/examples/ssm/Cargo.toml +++ b/rust_dev_preview/examples/ssm/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-ssm = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/stepfunction/Cargo.toml b/rust_dev_preview/examples/stepfunction/Cargo.toml index 5925a62ca60..835ff2e086c 100644 --- a/rust_dev_preview/examples/stepfunction/Cargo.toml +++ b/rust_dev_preview/examples/stepfunction/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Daniele Frasca "] edition = "2021" [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-sfn = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } clap = { version = "~4.4", features = ["derive"] } diff --git a/rust_dev_preview/examples/sts/Cargo.toml b/rust_dev_preview/examples/sts/Cargo.toml index 1b92f2a1c52..ece89040743 100644 --- a/rust_dev_preview/examples/sts/Cargo.toml +++ b/rust_dev_preview/examples/sts/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-sts = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-types = {git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.20.1", features = ["full"] } diff --git a/rust_dev_preview/examples/testing/Cargo.toml b/rust_dev_preview/examples/testing/Cargo.toml index 4c3010f9efe..1593934f5bd 100644 --- a/rust_dev_preview/examples/testing/Cargo.toml +++ b/rust_dev_preview/examples/testing/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # snippet-start:[testing.rust.Cargo.toml] [dependencies] async-trait = "0.1.51" -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-credential-types = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = [ "hardcoded-credentials", ] } aws-sdk-s3 = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-smithy-types = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } diff --git a/rust_dev_preview/examples/testing/src/replay.rs b/rust_dev_preview/examples/testing/src/replay.rs index af78f6b6a89..f6f349cbca6 100644 --- a/rust_dev_preview/examples/testing/src/replay.rs +++ b/rust_dev_preview/examples/testing/src/replay.rs @@ -61,6 +61,7 @@ fn make_s3_test_credentials() -> s3::config::Credentials { mod test { // snippet-start:[testing.rust.replay-test-single] use super::*; + use aws_config::BehaviorVersion; use aws_sdk_s3 as s3; use aws_smithy_runtime::client::http::test_util::{ReplayEvent, StaticReplayClient}; use aws_smithy_types::body::SdkBody; @@ -81,6 +82,7 @@ mod test { let replay_client = StaticReplayClient::new(vec![page_1]); let client: s3::Client = s3::Client::from_conf( s3::Config::builder() + .behavior_version(BehaviorVersion::latest()) .credentials_provider(make_s3_test_credentials()) .region(s3::config::Region::new("us-east-1")) .http_client(replay_client.clone()) @@ -129,6 +131,7 @@ mod test { // snippet-start:[testing.rust.replay-create-client] let client: s3::Client = s3::Client::from_conf( s3::Config::builder() + .behavior_version(BehaviorVersion::latest()) .credentials_provider(make_s3_test_credentials()) .region(s3::config::Region::new("us-east-1")) .http_client(replay_client.clone()) diff --git a/rust_dev_preview/examples/textract/Cargo.toml b/rust_dev_preview/examples/textract/Cargo.toml index 42c79387869..18b780ece25 100644 --- a/rust_dev_preview/examples/textract/Cargo.toml +++ b/rust_dev_preview/examples/textract/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" # For more keys and their definitions, see https://doc.rust-lang.org/cargo/reference/manifest.html. [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-textract = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } tokio = { version = "1.27", features = ["full"] } structopt = { version = "0.3", default-features = false } diff --git a/rust_dev_preview/examples/tls/Cargo.toml b/rust_dev_preview/examples/tls/Cargo.toml index 0f1f08c39fa..9cd1530f26e 100644 --- a/rust_dev_preview/examples/tls/Cargo.toml +++ b/rust_dev_preview/examples/tls/Cargo.toml @@ -12,7 +12,7 @@ name = "tls" path = "src/lib.rs" [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-smithy-runtime = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } aws-sdk-kms = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", default-features = false } webpki-roots = "0.22.4" diff --git a/rust_dev_preview/examples/tls/src/lib.rs b/rust_dev_preview/examples/tls/src/lib.rs index 47ab6be57c2..d0fcc2764f6 100644 --- a/rust_dev_preview/examples/tls/src/lib.rs +++ b/rust_dev_preview/examples/tls/src/lib.rs @@ -14,6 +14,7 @@ This example assumes you have set up environment variables for authentication. */ +use aws_config::BehaviorVersion; use aws_sdk_kms::Error; use aws_smithy_runtime::client::http::hyper_014::HyperClientBuilder; use rustls::RootCertStore; @@ -52,7 +53,10 @@ pub async fn connect_via_tls_13() -> Result<(), Error> { // See https://github.com/awslabs/smithy-rs/discussions/3022 for the HyperClientBuilder let http_client = HyperClientBuilder::new().build(rustls_connector); - let shared_conf = aws_config::from_env().http_client(http_client).load().await; + let shared_conf = aws_config::defaults(BehaviorVersion::latest()) + .http_client(http_client) + .load() + .await; let kms_client = aws_sdk_kms::Client::new(&shared_conf); let response = kms_client.list_keys().send().await?; diff --git a/rust_dev_preview/examples/transcribestreaming/Cargo.toml b/rust_dev_preview/examples/transcribestreaming/Cargo.toml index 9a6492580f6..7094fdd36ba 100644 --- a/rust_dev_preview/examples/transcribestreaming/Cargo.toml +++ b/rust_dev_preview/examples/transcribestreaming/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } +aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["behavior-version-latest"]} aws-sdk-transcribestreaming = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" } async-stream = "0.3" bytes = "1" diff --git a/rust_dev_preview/test-utils/src/macros.rs b/rust_dev_preview/test-utils/src/macros.rs index a4fa0dfd714..a9d857eb096 100644 --- a/rust_dev_preview/test-utils/src/macros.rs +++ b/rust_dev_preview/test-utils/src/macros.rs @@ -90,6 +90,7 @@ macro_rules! client_config { ) => { /// TODO: remove after https://github.com/awslabs/smithy-rs/pull/2145 $sdk_crate::Config::builder() + .behavior_version($sdk_crate::config::BehaviorVersion::latest()) .credentials_provider($sdk_crate::config::Credentials::new( "ATESTCLIENT", "atestsecretkey", diff --git a/rust_dev_preview/tools/add_aws_config_feature.py b/rust_dev_preview/tools/add_aws_config_feature.py new file mode 100755 index 00000000000..88c7bd509e7 --- /dev/null +++ b/rust_dev_preview/tools/add_aws_config_feature.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python3 + +try: + from tomlkit import dumps, parse, TOMLDocument +except: + print("Couldn't import tomlkit, either install it directly or instantiate a venv.") + exit(1) + +from argparse import ArgumentParser +from glob import glob +from typing import Union +import logging + +SDK_ORIGIN = "https://github.com/awslabs/aws-sdk-rust" + + +def list_cargos(pathname: str): + for file in glob(f"{pathname}/**/Cargo.toml", recursive=True): + yield file + + +def read_cargo(pathname: str) -> TOMLDocument: + with open(pathname, "+rt") as cargo: + return parse(cargo.read()) + + +def write_cargo(pathname: str, cargo: TOMLDocument) -> None: + with open(pathname, "+wt") as file: + file.write(dumps(cargo)) + + +def update_sdk_dependencies(cargo: TOMLDocument, feature: str) -> None: + dependencies = cargo.get("dependencies", []) + for name in dependencies: + if isinstance(name, str): + dependency = dependencies[name] + if name == "aws-config" and not isinstance(dependency, str): + logging.debug( + f"Found aws-config with features {dependency.get('features')}" + ) + features = dependency.get("features") or [] + features.append(feature) + dependency["features"] = features + + +def update_cargo(pathname: str, branch: Union["main", "next"], dry_run=False): + logging.debug(f"Found Cargo.toml at {pathname}") + cargo = read_cargo(pathname) + update_sdk_dependencies(cargo, branch) + if not dry_run: + logging.debug(f"Writing new content to {pathname}") + write_cargo(pathname, cargo) + + +arg_parser = ArgumentParser() +arg_parser.add_argument( + "--root", + default="../", + help="Root path for rust_dev_preview cargos. Default ../ assumes running this script from its location in tools. Update single examples by setting root to their folder directly.", +) +arg_parser.add_argument("--feature", help="Feature to add") +arg_parser.add_argument( + "--dry-run", + dest="dry_run", + action="store_true", + default=False, + help="Don't write updated Cargo files.", +) +arg_parser.add_argument( + "--verbose", action="store_true", default=False, help="Write verbose logging" +) + + +def main(): + args = arg_parser.parse_args() + if args.verbose: + logging.basicConfig(level=logging.DEBUG) + for f in list_cargos(args.root): + update_cargo(f, args.feature, dry_run=args.dry_run) + + +if __name__ == "__main__": + main() diff --git a/rust_dev_preview/webassembly/src/lib.rs b/rust_dev_preview/webassembly/src/lib.rs index 851f3f6764a..0b84b9eabff 100644 --- a/rust_dev_preview/webassembly/src/lib.rs +++ b/rust_dev_preview/webassembly/src/lib.rs @@ -9,8 +9,8 @@ use aws_sdk_lambda::config::{AsyncSleep, Region, Sleep}; use aws_sdk_lambda::primitives::SdkBody; use aws_sdk_lambda::{meta::PKG_VERSION, Client}; use aws_smithy_async::time::TimeSource; -use aws_smithy_runtime_api::client::http::request::Request; use aws_smithy_runtime_api::client::result::ConnectorError; +use aws_smithy_runtime_api::http::{Request, Response}; use aws_smithy_runtime_api::{ client::{ http::{ @@ -23,6 +23,7 @@ use aws_smithy_runtime_api::{ shared::IntoShared, }; +use aws_config::BehaviorVersion; use serde::Deserialize; use std::time::SystemTime; use wasm_bindgen::{prelude::*, JsCast}; @@ -71,7 +72,7 @@ pub async fn main(region: String, verbose: bool) -> Result { let credentials = credentials_provider.provide_credentials().await.unwrap(); let access_key = credentials.access_key_id(); - let shared_config = aws_config::from_env() + let shared_config = aws_config::defaults(BehaviorVersion::latest()) .sleep_impl(BrowserSleep) .region(Region::new(region)) .time_source(BrowserNow) @@ -267,7 +268,7 @@ impl HttpConnector for Adapter { HttpConnectorFuture::new(async move { let response = rx.await.map_err(|e| ConnectorError::user(Box::new(e)))?; log!("response received"); - Ok(response) + Ok(Response::try_from(response).unwrap()) }) } }