【问题标题】:How can I configure AWS SAM to use existing database?如何配置 AWS SAM 以使用现有数据库?
【发布时间】:2021-03-29 09:07:46
【问题描述】:

在我能找到的所有示例中,SAM 模板似乎创建了一个新的 DynamoDB 表。如何将其配置为指向现有表?

【问题讨论】:

    标签: amazon-web-services aws-sam


    【解决方案1】:

    由于资源已经存在,您可以硬编码表的 ARN,您通常会通过 CloudFormation 逻辑名称引用这些表(如果它们是由 CloudFormation 创建的)。

    例如,如果您授予对名为 Example 的表的扫描权限,则可以创建一个参数:

    Parameters:
      ExampleTableArn:
        Description: Example DynamoDB table ARN
        Type: String
        Default: arn:aws:dynamodb:us-west-2:xxxxxxxxxxxx:table/Example
    

    然后在您的 Lambda 策略中:

    Policies:
      Version: '2012-10-17'
      Statement:
      - Effect: Allow
        Action:
        - 'dynamodb:Scan'
        Resource: {Ref: ExampleTableArn}
    

    【讨论】:

      【解决方案2】:

      如果您使用Policy template list中的策略模板,则无需设置表ARN。

      template.yaml

      # ====================================
      # TODO: SETUP PARAMETERS
      # ====================================
      
      Parameters:
        ExistingTable:
          Type: String
          Default: example-table
          Description: (Required) The name of existing DynamoDB
          MinLength: 3
          MaxLength: 50
          AllowedPattern: ^[A-Za-z_-]+$
          ConstraintDescription: "Required. Can be characters, hyphen, and underscore only. No numbers or special characters allowed."
      
      
      # ====================================
      # TODO: SETUP FUNCTIONS
      # ====================================
      
        OnConnectFunction:
          Type: AWS::Serverless::Function
          Properties:
            Handler: src/lambdas/connect.handler
            MemorySize: 256
            Policies:
              - DynamoDBCrudPolicy:
                  TableName: !Ref ExistingTable
      

      说明:

      在这个template.yaml中,我设置了参数ExistingTable来允许输入现有的表名。在函数中,我使用了DynamoDBCrudPolicy,它允许在现有表上创建、检索、更新和删除。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-01
        • 2021-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-16
        • 2019-05-23
        • 1970-01-01
        相关资源
        最近更新 更多