2

aws-sdkAWS.IotData({ endpoint: url })関数を使用して、私のラムダリソースの一部をAWS IOTエンドポイントにプッシュすることができます。ここで、endpointは必須パラメータです。CloudFormationテンプレート内からAWS IOTエンドポイントURLを取得する方法は?

今、エンドポイントURLを環境変数でラムダに渡します。しかし、SAM/CFテンプレートに入れると、IOTエンドポイントのURLを取得する方法が見つからないので、単純に!Refとすることができます。

AWS resource type referenceでブラウズIOTエンドポイントに対応するリソースが見つかりませんでした。

IOTエンドポイントのみを以下のスクリーンショットのように、AWSコンソール(有効/無効)を経由して、手動でプロビジョニングすることができますように思え:IOTのプロビジョニングを管理している方法について何かアドバイス

IOT Endpoint AWS Console

少なくともSAM/CFテンプレート内からIOT URLを読み取るには、aws-cliでスクリプトを書く必要はありませんか?

答えて

1

IoTエンドポイントに関連する唯一のAPIコールがDescribeEndpointであるため、IoTエンドポイントをプロビジョニングすることはできません。

あなたができることは、ラムダ支援CloudFormationカスタムリソースを作成することです。ラムダ関数はDescribeEndpointコールを実行し(ラムダのランタイムに応じて、選択したAWS SDKを使用して)、他のCloudFormationリソースが消費できるようにエンドポイントのURLを返します。

ラムダが支援するカスタムリソースの例は、http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources-lambda.htmlです。

1

CloudFormationカスタムリソースのソリューションに興味がある人は、単純なラムダと、他のCFスタックにIOTエンドポイントアドレスを提供するCFテンプレートを書きました。

template.yaml

AWSTemplateFormatVersion: '2010-09-09' 
Transform: 'AWS::Serverless-2016-10-31' 
Resources: 
    IotEndpointProvider: 
    Type: 'AWS::Serverless::Function' 
    Properties: 
     FunctionName: IotEndpointProvider 
     Handler: iotEndpointProvider.handler 
     Runtime: nodejs6.10 
     CodeUri: . 
     MemorySize: 128 
     Timeout: 3 
     Policies: 
     - Version: '2012-10-17' 
      Statement: 
      - Effect: Allow 
      Action: 
       - iot:DescribeEndpoint 
      Resource: 
       - '*' 
    IotEndpoint: 
    Type: 'Custom::IotEndpoint' 
    Properties: 
     ServiceToken: !GetAtt IotEndpointProvider.Arn 
     Runtime: nodejs6.10 
     CodeUri: . 
     MemorySize: 128 
     Timeout: 3 
Outputs: 
    IotEndpointAddress: 
    Value: !GetAtt IotEndpoint.IotEndpointAddress 
    Export: 
     Name: IotEndpointAddress 

iotEndpointProvider.js

var aws = require("aws-sdk"); 

exports.handler = function(event, context) { 
    console.log("REQUEST RECEIVED:\n" + JSON.stringify(event)); 

    // For Delete requests, immediately send a SUCCESS response. 
    if (event.RequestType == "Delete") { 
     sendResponse(event, context, "SUCCESS"); 
     return; 
    } 

    const iot = new aws.Iot(); 
    iot.describeEndpoint({}, (err, data) => { 
    let responseData, responseStatus; 
     if (err) { 
      responseStatus = "FAILED"; 
      responseData = { Error: "describeEndpoint call failed" }; 
      console.log(responseData.Error + ":\n", err); 
     } else { 
      responseStatus = "SUCCESS"; 
      responseData = { IotEndpointAddress: data.endpointAddress }; 
      console.log('response data: ' + JSON.stringify(responseData)); 
     } 

     sendResponse(event, context, responseStatus, responseData); 
    }); 
}; 

// Send response to the pre-signed S3 URL 
function sendResponse(event, context, responseStatus, responseData) { 

    var responseBody = JSON.stringify({ 
     Status: responseStatus, 
     Reason: "See the details in CloudWatch Log Stream: " + context.logStreamName, 
     PhysicalResourceId: context.logStreamName, 
     StackId: event.StackId, 
     RequestId: event.RequestId, 
     LogicalResourceId: event.LogicalResourceId, 
     Data: responseData 
    }); 

    console.log("RESPONSE BODY:\n", responseBody); 

    var https = require("https"); 
    var url = require("url"); 

    var parsedUrl = url.parse(event.ResponseURL); 
    var options = { 
     hostname: parsedUrl.hostname, 
     port: 443, 
     path: parsedUrl.path, 
     method: "PUT", 
     headers: { 
      "content-type": "", 
      "content-length": responseBody.length 
     } 
    }; 

    console.log("SENDING RESPONSE...\n"); 

    var request = https.request(options, function(response) { 
     console.log("STATUS: " + response.statusCode); 
     console.log("HEADERS: " + JSON.stringify(response.headers)); 
     // Tell AWS Lambda that the function execution is done 
     context.done(); 
    }); 

    request.on("error", function(error) { 
     console.log("sendResponse Error:" + error); 
     // Tell AWS Lambda that the function execution is done 
     context.done(); 
    }); 

    // write data to request body 
    request.write(responseBody); 
    request.end(); 
} 
関連する問題