AWS DynamoDB Adapter
Need help? Contact us:
The AWS DynamoDB adapter is a simple component which can be used to interact with AWS DynamoDB tables and their items. The adapter provides functionality to get, put, update, delete, and query items in a table.
This component assumes an AWS IAM user has been set up with the correct permissions to interact with the DynamoDB table(s).
How to use it:
See Create a ComponentPreview if this is your first time!
Enter your AWS IAM user’s AccessKey and SecretKey, as well as the DynamoDB’s Region. If you already have a DynamoDB table, fill in the TableName too. AWS documentation for creating AccessKey - SecretKey pairs
Using the information provided, the component will create a DynamoDB adapter object that can be used to interact with your table.
If you didn’t create a DynamoDB table yet, you can do so via the UI or via the sample code in the Translator. Uncomment the code to create the table, set live = true, let the annotations run once, then re-comment the code or set live = false to ensure you don’t attempt to create the table multiple times.
-- Create our adapter
local Configs = component.fields()
local DDB = DYNAMODBclient{
AccessKey = Configs.AccessKey,
SecretKey = Configs.SecretKey,
Region = Configs.Region
}
-- Optionally uncomment the below to create the table programmatically
-- Make sure to re-comment the code afterwards to avoid creating multiple tables
--[[local Create = {
TableName = 'Patients',
AttributeDefinitions = {{
AttributeName = "ID",
AttributeType = "S"}
},
KeySchema = {{
AttributeName = "ID",
KeyType = "HASH"}
},
BillingMode = 'PAY_PER_REQUEST'
}
-- Set live = true to run the operation once, then set back to false
DDB:custom{action = 'CreateTable',data=json.serialize{data=Create},live=false}]]
Use the provided sample data to put or update sample json data into your DynamoDB table. You can add samples via the Samples folder. Set live=true to enable the interaction from the Translator.
Get the item (if it exists) from the table:
-- Parse the inbound data
local PatientData = json.parse{data=Data}
-- Check if the item already exists
local Status, Item = D:getItem{
table_name = Configs.TableName,
key = PatientData.id,
live = true
}Now update the item if it exists or create it and put it into the table if it doesn’t:
-- Create or update the item
if next(Item) then
-- Update the item
D:updateItem{
table_name = Configs.TableName,
key = PatientData.id,
set = {
Active = true,
Facility = 'Hospital',
},
live = true
}
else
-- Create an item
D:putItem{
table_name = Configs.TableName,
item = {
ID = PatientData.id,
FullName = PatientData.name,
DOB = PatientData.dob,
Address = {
Street = PatientData.address,
City = PatientData.city,
Zip = PatientData.zip
}
},
live = true
}
endYou can also play around with the deleteItem and query methods using the built-in help details.