AWS SDK for Go Code Examples for Amazon DynamoDB
Purpose
These examples demonstrates how to perform several DynamoDB operations.
Prerequisites
You must have an AWS account, and have your default credentials and AWS Region
configured as described in
Configuring the AWS SDK for Go
in the AWS SDK for Go Developer Guide.
Running the code
CreateTable/CreateTable.go
This example creates a DynamoDB table.
go run CreateTable.go -t TABLE
- TABLE is the name of the table.
The unit test accepts a similar value in config.json.
The table has two attributes:
- Year is an integer.
- Title is a string.
CreateTableItem/CreateTableItem.go
Creates a new item in a DynamoDB table.
go run CreateTableItem -d TABLE -y YEAR -t TITLE -r RATING
- TABLE is the name of the table.
- YEAR is the year that the movie was released.
- TITLE is the title of the movie.
- RATING is the rating, from 0.0 to 10.0, of the movie.
The unit test accepts similar values in config.json.
DeleteItem/DeleteItem.go
This example deletes an item from a DynamoDB table.
go run DeleteTable.go -t TABLE -m MOVIE -y YEAR
- TABLE is the name of the table containing the item to delete.
- MOVIE is the name of the movie item to delete.
- YEAR is when the movie was released.
The unit test mocks the DynamoDB service and the DeleteItem
function.
GetItem/GetItem.go
This example retrieves an item from a DynamoDB table.
go run GetItem.go -t TABLE -n NAME -y YEAR
- TABLE is the name of the table.
- NAME is the name of the movie.
- YEAR is when the movie was released.
The unit test mocks the DynamoDB service and GetItem
function.
ListTables/ListTables.go
This example lists your DynamoDB tables.
go run ListTables.go [-l LIMIT]
- LIMIT is how many tables to show.
The default is 100.
If this value is less than zero,
it is set to 10.
The unit test accepts a similar value in config.json.
LoadTableItems/LoadTableItems.go
This example adds items from a JSON file to a table.
go run LoadTableItems.go -j JSON-FILE -d TABLE
- JSON-FILE is the name of the JSON file containing the items to load into the table.
- TABLE is the name of the table.
The unit test accepts similar values in config.json.
ScanItems/ScanItems.go
This example uses the Expression Builder package to scan a table for items that fit the criteria.
go run ScanItems.go -t TABLE -r RATING -y YEAR
- TABLE is the name of the table.
- RATING is the minimum rating, from 0.0 to 1.0, given to the movies to retrieve.
- YEAR is the year when the movies were released.
The unit test mocks the DynamoDB service and Scan
function.
UpdateItem/UpdateItem.go
This example updates the year and rating of a movie in a table.
go run UpdateItem.go -t TABLE -m MOVIE -y YEAR -r RATING
- TABLE is the name of the table.
- MOVIE is the name of the movie.
- YEAR is the year the movie was released.
- RATING is the rating, from 0.0 to 1.0.
The unit test accepts similar values from config.json.
Notes
- We recommend that you grant this code least privilege,
or at most the minimum permissions required to perform the task.
For more information, see
Grant Least Privilege
in the AWS Identity and Access Management User Guide.
- This code has not been tested in all AWS Regions.
Some AWS services are available only in specific
Regions.
- Running this code might result in charges to your AWS account.
Running the unit tests
Unit tests should delete any resources they create.
However, they might result in charges to your
AWS account.
To run a unit test, enter:
go test
You should see something like the following,
where PATH is the path to the folder containing the Go files:
PASS
ok PATH 6.593s
If you want to see any log messages, enter:
go test -test.v
You should see some additional log messages.
The last two lines should be similar to the previous output shown.
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0