【问题标题】:yaml SAM local with dynamo db带有发电机数据库的 yaml SAM 本地
【发布时间】:2019-12-08 15:18:26
【问题描述】:

我正在尝试创建一组依赖于 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/"

但是当我尝试执行任何 lambdas 时,它们会在尝试调用 putItem 时超时。我需要做些什么来确保我的 lambda 可以与我的本地 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
    }

我如何确保我的表被正确创建?

【问题讨论】:

    标签: amazon-web-services aws-lambda amazon-dynamodb amazon-dynamodb-local


    【解决方案1】:

    原来 SAM local 不会创建 dynamodb 表:https://github.com/awslabs/aws-sam-cli/issues/105

    为此,您必须手动创建 docker 容器并在 SAM 之外创建表。

    【讨论】:

      【解决方案2】:

      在我正在做的一个 AWS SAM 项目中,它被证明具有创建所有必需的 DynamoDB 表、S3 存储桶等的功能。

      通常,我需要在集成测试中创建它们,因此提取一个通用函数来创建 AWS 依赖项很方便。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-19
        • 1970-01-01
        • 1970-01-01
        • 2014-05-06
        • 1970-01-01
        • 2016-07-22
        • 2020-11-06
        • 2013-08-03
        相关资源
        最近更新 更多