我正在尝试创建一组依赖于DynamoDB的lambda函数。我编写了这样一个有效的SAM模板:
---
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Resources:
Get:
Type: AWS::Serverless::Function
Properties:
Handler: ./lambdas/get/main
Runtime: go1.x
Events:
PostEvent:
Type: Api
Properties:
Path: /
Method: get
Portfolios:
Type: AWS::DynamoDB::Table
TableName: "entities"
Properties:
AttributeDefinitions:
-
AttributeName: "Id"
AttributeType: "S"
-
AttributeName: "Name"
AttributeType: "S"
KeySchema:
-
AttributeName: "Id"
KeyType: "HASH"
-
AttributeName: "Name"
KeyType: "RANGE"
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
Tags:
- Key: foo
Value: bar
Outputs:
Endpoint:
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"但是,当我试图执行任何一个lambda时,它们都会在尝试调用putItem时超时。要确保lambdas能够与本地dynamodb对话,我需要做什么?
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
endpoint := "localhost:3000"
db := dynamodb.New(sess, &aws.Config{
Endpoint: &endpoint,
})
entity := &models.Entities{}
json.Unmarshal([]byte(event.Body), portfolio)
av, err := dynamodbattribute.MarshalMap(entity)
if err != nil {
fmt.Println("Got error marshalling:")
fmt.Println(err.Error())
os.Exit(1)
}
input := &dynamodb.PutItemInput{
Item: av,
TableName: aws.String("entities"),
}
_, err = db.PutItem(input) // Times out here
if err != nil {
fmt.Println("Got error calling PutItem:")
fmt.Println(err.Error())
return events.APIGatewayProxyResponse{StatusCode: 500}, nil
}如何确保我的表被正确创建?
发布于 2019-12-08 03:54:52
原来SAM将不会创建dynamodb表:https://github.com/awslabs/aws-sam-cli/issues/105
要做到这一点,您必须手动创建docker容器,并在SAM之外创建表。
发布于 2019-12-08 07:18:26
在我正在执行的AWS项目中,它有一个函数来创建所有必需的DynamoDB表、S3桶等等。
通常,我需要在集成测试中创建它们,因此提取一个通用函数来创建AWS依赖关系是很方便的。
https://stackoverflow.com/questions/59232254
复制相似问题