【问题标题】:PutItem in DynamoDB table by CloudFormationCloudFormation 将项目放入 DynamoDB 表中
【发布时间】:2017-07-21 22:36:29
【问题描述】:

有没有办法使用 CloudFormation 将项目放入 DynamoDB 表中? 类似于doc中的代码

在模板的参数中,我让用户可以放置值,然后我需要将这些值插入到表格中。

【问题讨论】:

  • 不,没有。我认为正确的问题是您为什么要这样做?
  • 我正在研究与 AWS 发布的 one 类似的解决方案。 @Robo我使用了一个自定义资源,但我有一个资源没有创建的问题,它不断显示创建过程中
  • 自定义资源是实现此目的的唯一方法,因为 cloudformation 无法为您的资源提供数据或执行任意 API 调用。
  • @jens 您能否在回答中提供更多详细信息。

标签: amazon-web-services amazon-cloudformation


【解决方案1】:

实现这一点的方法是为此使用自定义资源。

这里是一个 Cloudformation 模板,它利用内联 Lambda 来完成此任务。

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  LambdaRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - lambda.amazonaws.com
          Action:
          - sts:AssumeRole
      Path: "/"
      Policies:
        - PolicyName: dynamodbAccessRole
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
            - Effect: Allow
              Action:
              - dynamodb:*
              Resource: "*"
            - Effect: Allow
              Action:
              - logs:*
              Resource: "*"
  InitFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        ZipFile: >
          const AWS = require("aws-sdk");
          const response = require("cfn-response");
          const docClient = new AWS.DynamoDB.DocumentClient();
          exports.handler = function(event, context) {
              console.log(JSON.stringify(event,null,2));
              var params = {
                TableName: event.ResourceProperties.DynamoTableName,
                Item:{
                    "id": "abc123"
                }
            };
          docClient.put(params, function(err, data) { if (err) {
            response.send(event, context, "FAILED", {});
          } else {
            response.send(event, context, "SUCCESS", {});
          }
          });
          };
      Handler: index.handler
      Role:
        Fn::GetAtt: [ LambdaRole , "Arn" ]
      Runtime: nodejs4.3
      Timeout: 60
  DynamoDB:
    Type: AWS::DynamoDB::Table
    Properties:
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: 1
        WriteCapacityUnits: 1
  InitializeDynamoDB:
    Type: Custom::InitFunction
    DependsOn: DynamoDB
    Properties:
      ServiceToken:
         Fn::GetAtt: [ InitFunction , "Arn" ]
      DynamoTableName:
        Ref: DynamoDB

【讨论】:

  • 能否请您单独提供Python 2.7中的功能代码?
  • saldy 不,我不知道任何 python 或如何在 python 中编写 lambdas。
  • 在提供的示例中,是什么触发了函数运行?我已经成功使用它,但它似乎只运行一次。如果我修改种子数据并重新部署,它不会运行该函数,因此不会填充我的新种子数据。
  • 请注意,nodejs4.3 现已停止使用。使用nodejs10.x
  • 请注意,内联字符限制为 4096,AWS 希望您将脚本放在 S3 上并在那里引用它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-06
  • 1970-01-01
  • 2021-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多