AWS code-library documentation change
Summary
Added multiple scenarios covering advanced DynamoDB features including Global Secondary Index configurations, resource-based policies, ABAC implementation, encryption management, and security-related access controls.
Security assessment
The changes add documentation for security features like resource-based policies (controlling table access via IAM), Attribute-Based Access Control (ABAC) with IAM policies, and encryption management with KMS keys. These are proactive security controls rather than fixes for existing vulnerabilities.
Diff
diff --git a/code-library/latest/ug/bash_2_dynamodb_code_examples.md b/code-library/latest/ug/bash_2_dynamodb_code_examples.md index 2b489b15d..d5ec44ee1 100644 --- a//code-library/latest/ug/bash_2_dynamodb_code_examples.md +++ b//code-library/latest/ug/bash_2_dynamodb_code_examples.md @@ -5 +5 @@ -BasicsActions +BasicsActionsScenarios @@ -16,0 +17,2 @@ _Actions_ are code excerpts from larger programs and must be run in context. Whi +_Scenarios_ are code examples that show you how to accomplish specific tasks by calling multiple functions within a service or combined with other AWS services. + @@ -24,0 +27,2 @@ Each example includes a link to the complete source code, where you can find ins + * Scenarios + @@ -3097,0 +3102,1099 @@ The utility functions used in this example. +## Scenarios + +The following code example shows how to work with advanced Global Secondary Index configurations. + + * Create a table with multiple GSIs. + + * Create a table with on-demand capacity and GSI. + + * Put items into a table with multiple GSIs. + + * Query multiple GSIs with different conditions. + + + + +**AWS CLI with Bash script** + + +Create a table with multiple GSIs. + + + # Create a table with multiple GSIs + aws dynamodb create-table \ + --table-name MusicLibrary \ + --attribute-definitions \ + AttributeName=Artist,AttributeType=S \ + AttributeName=SongTitle,AttributeType=S \ + AttributeName=AlbumTitle,AttributeType=S \ + AttributeName=Genre,AttributeType=S \ + AttributeName=Year,AttributeType=N \ + --key-schema \ + AttributeName=Artist,KeyType=HASH \ + AttributeName=SongTitle,KeyType=RANGE \ + --billing-mode PAY_PER_REQUEST \ + --global-secondary-indexes \ + "[ + { + \"IndexName\": \"AlbumIndex\", + \"KeySchema\": [{\"AttributeName\":\"AlbumTitle\",\"KeyType\":\"HASH\"}], + \"Projection\": {\"ProjectionType\":\"ALL\"} + }, + { + \"IndexName\": \"GenreYearIndex\", + \"KeySchema\": [ + {\"AttributeName\":\"Genre\",\"KeyType\":\"HASH\"}, + {\"AttributeName\":\"Year\",\"KeyType\":\"RANGE\"} + ], + \"Projection\": {\"ProjectionType\":\"INCLUDE\",\"NonKeyAttributes\":[\"Artist\",\"SongTitle\"]} + } + ]" + + + +Create a table with on-demand capacity and GSI. + + + # Create a table with on-demand capacity and GSI + aws dynamodb create-table \ + --table-name MusicOnDemand \ + --attribute-definitions \ + AttributeName=Artist,AttributeType=S \ + AttributeName=SongTitle,AttributeType=S \ + AttributeName=Genre,AttributeType=S \ + --key-schema \ + AttributeName=Artist,KeyType=HASH \ + AttributeName=SongTitle,KeyType=RANGE \ + --billing-mode PAY_PER_REQUEST \ + --global-secondary-indexes \ + "[ + { + \"IndexName\": \"GenreIndex\", + \"KeySchema\": [{\"AttributeName\":\"Genre\",\"KeyType\":\"HASH\"}], + \"Projection\": {\"ProjectionType\":\"ALL\"} + } + ]" + + + +Put items into a table with multiple GSIs. + + + # Add items to MusicLibrary table + aws dynamodb put-item \ + --table-name MusicLibrary \ + --item '{ + "Artist": {"S": "The Beatles"}, + "SongTitle": {"S": "Hey Jude"}, + "AlbumTitle": {"S": "Past Masters"}, + "Genre": {"S": "Rock"}, + "Year": {"N": "1968"} + }' + + aws dynamodb put-item \ + --table-name MusicLibrary \ + --item '{ + "Artist": {"S": "Miles Davis"}, + "SongTitle": {"S": "So What"}, + "AlbumTitle": {"S": "Kind of Blue"}, + "Genre": {"S": "Jazz"}, + "Year": {"N": "1959"} + }' + + + +Query items from a table with multiple GSIs. + + + # Query the AlbumIndex GSI + echo "Querying AlbumIndex GSI:" + aws dynamodb query \ + --table-name MusicLibrary \ + --index-name AlbumIndex \ + --key-condition-expression "AlbumTitle = :album" \ + --expression-attribute-values '{":album":{"S":"Kind of Blue"}}' + + # Query the GenreYearIndex GSI with a range condition + echo "Querying GenreYearIndex GSI with range condition:" + aws dynamodb query \ + --table-name MusicLibrary \ + --index-name GenreYearIndex \ + --key-condition-expression "Genre = :genre AND #yr > :year" \ + --expression-attribute-names '{"#yr": "Year"}' \ + --expression-attribute-values '{":genre":{"S":"Rock"},":year":{"N":"1965"}}' + + + + * For API details, see the following topics in _AWS CLI Command Reference_. + + * [CreateTable](https://docs.aws.amazon.com/goto/aws-cli/dynamodb-2012-08-10/CreateTable) + + * [PutItem](https://docs.aws.amazon.com/goto/aws-cli/dynamodb-2012-08-10/PutItem) + + * [Query](https://docs.aws.amazon.com/goto/aws-cli/dynamodb-2012-08-10/Query) + + + + +The following code example shows how to manage the complete lifecycle of Global Secondary Indexes. + + * Create a table with a Global Secondary Index. + + * Add a new GSI to an existing table. + + * Update (increase) GSI warm throughput. + + * Query data using GSIs. + + * Delete a GSI. + + + + +**AWS CLI with Bash script** + + +Create a table with a Global Secondary Index. + + + # Create a table with a GSI + aws dynamodb create-table \ + --table-name MusicCollection \ + --attribute-definitions \ + AttributeName=Artist,AttributeType=S \ + AttributeName=SongTitle,AttributeType=S \ + AttributeName=AlbumTitle,AttributeType=S \ + --key-schema \ + AttributeName=Artist,KeyType=HASH \ + AttributeName=SongTitle,KeyType=RANGE \ + --billing-mode PAY_PER_REQUEST \ + --global-secondary-indexes \ + "IndexName=AlbumIndex,\ + KeySchema=[{AttributeName=AlbumTitle,KeyType=HASH}],\ + Projection={ProjectionType=ALL}" + + + +Add a new (on-demand) GSI to an existing table. + + + # Add a new GSI to an existing table + aws dynamodb update-table \ + --table-name MusicCollection \ + --attribute-definitions \ + AttributeName=Genre,AttributeType=S \ + --global-secondary-index-updates \ + "[{\"Create\":{\"IndexName\":\"GenreIndex\",\