Hola como puedo conectar mi app de SAM
a dynamoDB
que tengo corriendo y funcionando dentro de docker
, puedo conectarme con cualquier cliente incluido serverless
pero SAM
la M****a
esta no puede
he intentado de estas 2 formas
sam local invoke getAllItemsFunction
y
sam local start-api
el resultado es el mismo, o que mas me falta?, esa cosa me tiene loco ya una semana, solo encuentro guias con dynamodb
ya desde AWS
o no hay forma de trabajarlo solo localmente?
ERROR Invoke Error {"errorType":"UnknownEndpoint","errorMessage":"Inaccessible host: `dynamodb-local' at port `undefined'. This service may not be available in the `localhost' region.","code":"UnknownEndpoint","message":"Inaccessible host: `dynamodb-local' at port `undefined'. This service may not be available in the `localhost' region.","region":"localhost","hostname":"dynamodb-local","retryable":true,"originalError":{"errorType":"NetworkingError","errorMessage":"getaddrinfo ENOTFOUND dynamodb-local","code":"NetworkingError","message":"getaddrinfo ENOTFOUND dynamodb-local","errno":-3008,"syscall":"getaddrinfo","hostname":"dynamodb-local","region":"localhost","retryable":true,"time":"2022-07-04T22:48:27.668Z","stack":["Error: getaddrinfo ENOTFOUND dynamodb-local","
lambda
const tableName = process.env.SAMPLE_TABLE;
const { DynamoDB } = require("aws-sdk");
const docClient = new DynamoDB({
endpoint: "http://dynamodb-local:8000",
//endpoint: "http://dynamodb:8000",
//endpoint: "http://localhost:8000",
//endpoint: "http://127.0.0.1:8000",
region: "localhost",
});
exports.getAllItemsHandler = async (event) => {
var params = {
TableName: tableName,
};
const data = await docClient.scan(params).promise();
const items = data.Items;
const response = {
statusCode: 200,
body: JSON.stringify(items),
};
return response;
};
template.yml
AWSTemplateFormatVersion: 2010-09-09
Description: >-
demoapp
Transform:
- AWS::Serverless-2016-10-31
Globals:
Function:
Tracing: Active
Resources:
getAllItemsFunction:
Type: AWS::Serverless::Function
Properties:
Handler: src/handlers/get-all-items.getAllItemsHandler
Runtime: nodejs14.x
Architectures:
- x86_64
MemorySize: 128
Timeout: 100
Description: A simple example includes a HTTP get method to get all items from a DynamoDB table.
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref SampleTable
Environment:
Variables:
SAMPLE_TABLE: !Ref SampleTable
Events:
Api:
Type: Api
Properties:
Path: /
Method: GET
SampleTable:
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey:
Name: id
Type: String
ProvisionedThroughput:
ReadCapacityUnits: 2
WriteCapacityUnits: 2
Outputs:
WebEndpoint:
Description: "API Gateway endpoint URL for Prod stage"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
dynamo-docker
version: '3.8'
services:
dynamodb-local:
command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ./data"
image: "amazon/dynamodb-local:latest"
container_name: dynamodb-local
ports:
- "8000:8000"
volumes:
- "./docker/dynamodb:/home/dynamodblocal/data"
working_dir: /home/dynamodblocal