2017-03-02 41 views
2

有沒有什麼辦法可以把物品在使用CloudFormation一個DynamoDB表? 東西相似的代碼在這個docPutItem在DynamoDB表通過CloudFormation

在我給用戶,使這些值的可能性模板的參數,然後我需要將這些值插入到表。

+0

不,沒有。我認爲適當的問題是你爲什麼想要? – Robo

+0

我正在研究類似於AWS發佈的[one]解決方案(https://s3.amazonaws.com/solutions-reference/ebs-snapshot-scheduler/latest/ebs-snapshot-scheduler.pdf)。 @Robo我使用了一個自定義的資源,但是我有一個問題,那就是資源沒有創建,它顯示正在進行創建 – Somar

+0

自定義資源是實現這一目標的唯一方法,因爲cloudformation無法爲您的資源提供數據或執行任意的API調用。 –

回答

4

實現,這將是利用自定義資源這個問題的方法。

這裏,它利用內嵌的λ完成這個任務,Cloudformation模板。

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中單獨提供函數代碼? – Somar

+0

saldy不,我不知道任何python或如何在python中編寫lambda表達式。 –

相關問題