Ballerina Amazon DynamoDB Connector
Amazon DynamoDB is a fully managed, serverless, key-value NoSQL database designed to run high-performance applications at any scale. DynamoDB offers built-in security, continuous backups, automated multi-region replication, in-memory caching, and data export tools.
The connector provides the capability to programatically handle AWS DynamoDB related operations.
For more information, go to the module(s).
Set up DynamoDB credentials
To invoke the DynamoDB REST API, you need AWS credentials. Below is a step-by-step guide on how to obtain these credentials:
- Create an AWS Account:
- If you don't already have an AWS account, you need to create one. Go to the AWS Management Console, click on "Create an AWS Account," and follow the instructions.
- Access the AWS Identity and Access Management (IAM) Console:
- Once logged into the AWS Management Console, go to the IAM console by selecting "Services" and then choosing "IAM" under the "Security, Identity, & Compliance" section.
- Create an IAM User:
- In the IAM console, navigate to "Users" and click on "Add user."
- Enter a username, and under "Select AWS access type," choose "Programmatic access."
- Click through the permissions setup, attaching policies that grant access to DynamoDB if you have specific requirements.
- Review the details and click "Create user."
- Access Key ID and Secret Access Key:
- Once the user is created, you will see a success message. Take note of the "Access key ID" and "Secret access key" displayed on the confirmation screen. These credentials are needed to authenticate your requests.
- Securely Store Credentials:
- Download the CSV file containing the credentials, or copy the "Access key ID" and "Secret access key" to a secure location. This information is sensitive and should be handled with care.
- Use the Credentials in Your Application:
- In your application, use the obtained "Access key ID" and "Secret access key" to authenticate requests to the DynamoDB REST API.
Quickstart
Note: Ensure you follow the prerequisites to get the credentials to be used.
To use the dynamodb
connector in your Ballerina application, modify the .bal
file as follows:
Step 1: Import the connector
Import the ballerinax/aws.dynamodb
package into your Ballerina project.
import ballerinax/aws.dynamodb;
Step 2: Instantiate a new connector
Create a dynamodb:ConnectionConfig
with the obtained access key id and secret access key to initialize the connector with it.
dynamodb:ConnectionConfig amazonDynamodbConfig = {
awsCredentials: {
accessKeyId: "ACCESS_KEY_ID",
secretAccessKey: "SECRET_ACCESS_KEY"
},
region: "REGION"
};
dynamodb:Client amazonDynamodbClient = check new (amazonDynamodbConfig);
Step 3: Invoke the connector operation
Now, utilize the available connector operations.
dynamodb:CreateTableInput createTableInput = {
tableName: "HighScores",
attributeDefinitions: [
{attributeName: "GameID", attributeType: "S"},
{attributeName: "Score", attributeType: "N"}
],
keySchema: [
{attributeName: "GameID", keyType: "HASH"},
{attributeName: "Score", keyType: "RANGE"}
],
provisionedThroughput: {
readCapacityUnits: 5,
writeCapacityUnits: 5
}
};
_ = check amazonDynamodbClient->createTable(createTableInput);
Examples
The dynamodb
connector provides practical examples illustrating usage in various scenarios. Explore these examples, covering use cases like creating, reading, updating, deleting data from tables.
- Maintain a game score dashboard Manage a mobile gaming application dashboard that tracks high scores for different games.
For comprehensive information about the connector's functionality, configuration, and usage in Ballerina programs, refer to the dynamodb
connector's reference guide in Ballerina Central.
Issues and projects
The Issues and Projects tabs are disabled for this repository as this is part of the Ballerina library. To report bugs, request new features, start new discussions, view project boards, etc., visit the Ballerina library parent repository.
This repository only contains the source code for the package.
Build from the source
Prerequisites
-
Download and install Java SE Development Kit (JDK) version 17. You can download it from either of the following sources:
Note: After installation, remember to set the
JAVA_HOME
environment variable to the directory where JDK was installed. -
Download and install Ballerina Swan Lake.
-
Download and install Docker.
Note: Ensure that the Docker daemon is running before executing any tests.
Build options
Execute the commands below to build from the source.
-
To build the package:
./gradlew clean build
-
To run the tests:
./gradlew clean test
-
To build the without the tests:
./gradlew clean build -x test
-
To debug package with a remote debugger:
./gradlew clean build -Pdebug=<port>
-
To debug with the Ballerina language:
./gradlew clean build -PbalJavaDebug=<port>
-
Publish the generated artifacts to the local Ballerina Central repository:
./gradlew clean build -PpublishToLocalCentral=true
-
Publish the generated artifacts to the Ballerina Central repository:
./gradlew clean build -PpublishToCentral=true
Contribute to Ballerina
As an open-source project, Ballerina welcomes contributions from the community.
For more information, go to the contribution guidelines.
Code of conduct
All the contributors are encouraged to read the Ballerina Code of Conduct.
Useful links
- For more information go to the
aws.dynamodb
package. - For example demonstrations of the usage, go to Ballerina By Examples.
- Chat live with us via our Discord server.
- Post all technical questions on Stack Overflow with the #ballerina tag.