Skip to content

Latest commit

 

History

History
314 lines (193 loc) · 10.9 KB

File metadata and controls

314 lines (193 loc) · 10.9 KB

DynamoDB code examples for the SDK for Python

Overview

Shows how to use the AWS SDK for Python (Boto3) to work with Amazon DynamoDB.

DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability.

⚠ Important

  • Running this code might result in charges to your AWS account. For more details, see AWS Pricing and Free Tier.
  • 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.
  • This code is not tested in every AWS Region. For more information, see AWS Regional Services.

Code examples

Prerequisites

For prerequisites, see the README 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

Get started

Basics

Code examples that show you how to perform the essential operations within a service.

Single actions

Code excerpts that show you how to call individual service functions.

Scenarios

Code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Run the examples

Instructions

Hello DynamoDB

This example shows you how to get started using DynamoDB.

python hello/hello_dynamodb.py

Learn the basics

This example shows you how to do the following:

  • Create a table that can hold movie data.
  • Put, get, and update a single movie in the table.
  • Write movie data to the table from a sample JSON file.
  • Query for movies that were released in a given year.
  • Scan for movies that were released in a range of years.
  • Delete a movie from the table, then delete the table.

Start the example by running the following at a command prompt:

python GettingStarted/scenario_getting_started_movies.py

Accelerate reads with DynamoDB Accelerator

This example shows you how to do the following:

  • Create and write data to a table with both the DynamoDB Accelerator and SDK clients.
  • Get, query, and scan the table with both clients and compare their performance.

Start the example by running the following at a command prompt:

python TryDax/01-create-table.py

To run the scripts with the DAX client, you must run them on an Amazon Elastic Compute Cloud (Amazon EC2) instance within your virtual private cloud (VPC). This process is described in the Python sample application tutorial in the
Developing with the DAX Client section of the Amazon DynamoDB Developer Guide.

The tutorial shows you how to set up the following additional resources:

  • A VPC from Amazon Virtual Private Cloud (Amazon VPC)
  • A DAX cluster set up in your VPC
  • An EC2 instance running in your VPC with the following installed:
    • Python 3.9 or later
    • Boto3 1.11.10 or later
    • Amazon DAX Client for Python 1.1.7 or later

On an EC2 instance, run the get item, query, and scan test scripts with the DAX client by specifying a DAX cluster endpoint as the first positional argument. To run the query test script with the DAX client, run the following from a command prompt window. Start the example by running the following at a command prompt:

python 04-query-test.py YOUR-CLUSTER-NAME.111111.clustercfg.dax.usw2.cache.amazonaws.com:8111

Create a REST API to track COVID-19 data

This example shows you how to create a REST API that simulates a system to track daily cases of COVID-19 in the United States, using fictional data.

Create a messenger application

This example shows you how to create an AWS Step Functions messenger application that retrieves message records from a database table.

Create a web application to track DynamoDB data

This example shows you how to create a web application that tracks work items in an Amazon DynamoDB table and uses Amazon Simple Email Service (Amazon SES) (Amazon SES) to send reports.

Create a websocket chat application

This example shows you how to create a chat application that is served by a websocket API built on Amazon API Gateway.

Query a table by using batches of PartiQL statements

This example shows you how to do the following:

  • Get a batch of items by running multiple SELECT statements.
  • Add a batch of items by running multiple INSERT statements.
  • Update a batch of items by running multiple UPDATE statements.
  • Delete a batch of items by running multiple DELETE statements.

Start the example by running the following at a command prompt:

python partiql/scenario_partiql_batch.py

Query a table using PartiQL

This example shows you how to do the following:

  • Get an item by running a SELECT statement.
  • Add an item by running an INSERT statement.
  • Update an item by running an UPDATE statement.
  • Delete an item by running a DELETE statement.

Start the example by running the following at a command prompt:

python partiql/scenario_partiql_single.py

Use API Gateway to invoke a Lambda function

This example shows you how to create an AWS Lambda function invoked by Amazon API Gateway.

Use scheduled events to invoke a Lambda function

This example shows you how to create an AWS Lambda function invoked by an Amazon EventBridge scheduled event.

Tests

⚠ Running tests might result in charges to your AWS account.

To find instructions for running these tests, see the README in the python folder.

Additional resources


Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0