2017-03-02 7 views
2

CloudFormationを使用してDynamoDBテーブルにアイテムを挿入する方法はありますか? このコードの似たようなものdocCloudFormationによるDynamoDBテーブルのPutItem

テンプレートのパラメータでは、ユーザーに値を設定する可能性があるため、これらの値をテーブルに挿入する必要があります。

+0

いいえありません。私は適切な質問を考えていますが、なぜあなたはしたいですか? – Robo

+0

AWSが発行しているこの[1](https://s3.amazonaws.com/solutions-reference/ebs-snapshot-scheduler/latest/ebs-snapshot-scheduler.pdf)に似たソリューションに取り組んでいます。 @Roboカスタムリソースを使用しましたが、リソースが作成されていないという問題があり、進行中の作成を継続的に示しています – Somar

+0

これを達成する唯一の方法はカスタムリソースです。任意のAPI呼び出し。 –

答えて

4

これを達成する方法は、カスタムリソースを利用することです。

ここでは、このタスクにインラインラムダを使用する雲のテンプレートです。

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 
+0

Python 2.7で機能コードseparetlyを提供していただけますか? – Somar

+0

saldyいいえ、私はPythonやPythonでlambdasをコーディングする方法を知らない。 –

関連する問題