Question: How to access DynamoDB from EC2?

Answered by Rafal Wilinski
Last updated: October 23rd, 2026
Answer
Attach an IAM role to the EC2 instance and use an AWS SDK or AWS CLI to call the regional DynamoDB endpoint. The SDK's default credential chain retrieves temporary credentials from the instance role; you do not need to store access keys on the machine.
Create a role trusted by EC2, grant only the DynamoDB actions and table or index ARNs the application needs, and attach it through an instance profile. Configure the application for the table's Region.
import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DynamoDBDocumentClient, GetCommand } from "@aws-sdk/lib-dynamodb"; const ddb = DynamoDBDocumentClient.from( new DynamoDBClient({ region: "us-east-1" }) ); const { Item } = await ddb.send(new GetCommand({ TableName: "Orders", Key: { pk: "ORDER#42" }, }));
This example assumes a String partition key named pk. Ensure outbound HTTPS can reach DynamoDB. In a private subnet, a DynamoDB gateway endpoint provides a route without a NAT gateway; its route tables and endpoint policy must allow the request. An interface endpoint has different network and security-group requirements.
If access fails, check the instance identity, Region, IAM policy, endpoint policy and table resource policy. See DynamoDB access control and VPC connectivity.
Source: AWS EC2 IAM roles.
Other Common DynamoDB FAQ (with Answers)
- How do I create an ID in DynamoDB?
- Can DynamoDB store documents?
- Is DynamoDB a memory store?
- How do you handle empty strings in DynamoDB?
- Should you make a new DynamoDB client for each request?
- What are the naming conventions in DynamoDB?
- What is the maximum number of partitions in DynamoDB?
- Does DynamoDB support Multi-AZ?
- How to count rows in DynamoDB?
- Does DynamoDB have read replicas?
- Is DynamoDB HIPAA compliant?
- Is a DynamoDB table Region-specific?
- Is DynamoDB PaaS or SaaS?
- Can a DynamoDB range key be a GSI key?
- How do you store Japanese characters in DynamoDB?
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today