Amazon 支援SNS的自訂資源 - AWS CloudFormation

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

Amazon 支援SNS的自訂資源

下列主題說明如何使用指定 CloudFormation 傳送請求的 Amazon SNS 主題的服務權杖來設定自訂資源。您還可以學習由於自訂資源堆疊建立、更新和刪除而傳送和接收的事件和訊息順序。

使用自訂資源和 AmazonSNS,您可以啟用案例,例如將新資源新增至堆疊,以及將動態資料插入堆疊。例如,當您建立堆疊時, CloudFormation 可以將Create請求傳送至由 Amazon EC2 執行個體上執行的應用程式監控的主題。Amazon SNS 通知會觸發應用程式執行其他佈建任務,例如擷取允許列出的彈性 IP 地址集區。完成之後,應用程式會傳送回應 (以及任何輸出資料),通知 CloudFormation 要繼續進行堆疊作業。

當您指定 Amazon SNS 主題做為自訂資源的目標時, CloudFormation 會在涉及自訂資源的堆疊操作期間將訊息傳送到指定的SNS主題。若要處理這些訊息並執行必要的動作,您必須訂閱該SNS主題的支援端點。

如需自訂資源及其運作方式的簡介,請參閱自訂資源。如需 Amazon SNS 及其運作方式的相關資訊,請參閱 Amazon 簡單通知服務開發人員指南

使用 Amazon 創SNS建自定義資源

步驟 1:建立堆疊

  1. 範本開發人員會建立包含自訂資源的 CloudFormation 堆疊。

    在以下範本範例中,我們Custom::SeleniumTester針對具有邏輯 ID 的自訂資源使用自訂資源類型名稱MySeleniumTest。自訂資源類型名稱必須是英數字元,長度上限為 60 個字元。

    自訂資源類型會使用服務 Token、選用的提供者特定內容,以及自訂資源提供者定義的選用Fn::GetAtt屬性來宣告。這些屬性可用來將資訊從範本開發人員傳遞到自訂資源提供者 (反之亦然)。服務令牌指定了資源提供者已配置的 Amazon SNS 主題。

    { "AWSTemplateFormatVersion" : "2010-09-09", "Resources" : { "MySeleniumTest" : { "Type": "Custom::SeleniumTester", "Version" : "1.0", "Properties" : { "ServiceToken": "arn:aws:sns:us-west-2:123456789012:CRTest", "seleniumTester" : "SeleniumTest()", "endpoints" : [ "http://mysite.com", "http://myecommercesite.com/", "http://search.mysite.com" ], "frequencyOfTestsPerHour" : [ "3", "2", "4" ] } } }, "Outputs" : { "topItem" : { "Value" : { "Fn::GetAtt" : ["MySeleniumTest", "resultsPage"] } }, "numRespondents" : { "Value" : { "Fn::GetAtt" : ["MySeleniumTest", "lastUpdate"] } } } }
    注意

    在提供者回應期間,自訂資源提供者會傳回與Fn::GetAtt存取之資料的名稱和值 CloudFormation。如果custom resource provider是第三方,則範本開發人員必須從自訂資源提供者取得這些傳回值的名稱。

  2. CloudFormation 傳送 Amazon SNS 通知給資源提供者,其中包"RequestType" : "Create"含堆疊的相關資訊、來自堆疊範本的自訂資源屬性,以及用URL於回應的 S3。

    用來傳送通知的SNS主題內嵌在ServiceToken屬性的範本中。為避免使用硬式編碼的值,範本開發人員可以使用範本參數,在啟動堆疊時輸入值。

    以下範例顯示自訂資源 Create 請求,其中包括使用 Custom::SeleniumTesterLogicalResourceId 所建立的自訂資源類型名稱 MySeleniumTester

    { "RequestType" : "Create", "ResponseURL" : "http://pre-signed-S3-url-for-response", "StackId" : "arn:aws:cloudformation:us-west-2:123456789012:stack/mystack/5b918d10-cd98-11ea-90d5-0a9cd3354c10", "RequestId" : "unique id for this create request", "ResourceType" : "Custom::SeleniumTester", "LogicalResourceId" : "MySeleniumTester", "ResourceProperties" : { "seleniumTester" : "SeleniumTest()", "endpoints" : [ "http://mysite.com", "http://myecommercesite.com/", "http://search.mysite.com" ], "frequencyOfTestsPerHour" : [ "3", "2", "4" ] } }

    如需有關要求之要Create求物件的詳細資訊,請參閱建立主題。

  3. 自訂資源提供者會處理範本開發人員傳送的資料,並判斷 Create 請求是否成功。然後,資源提供者會使用傳URL CloudFormation 送的 S3 傳送SUCCESS或的回應FAILED

    根据響應類型,系統會預期響應字段不同 CloudFormation。如需有關特定要求類型之回應欄位的資訊,請參閱自訂資源請求類型本節中該要求類型的說明文件。

    在回應建立或更新請求時,custom resource provider可傳回回應 Data 欄位中的資料元素。這些是名稱值對;名稱對應至 Fn::GetAtt 屬性,其與堆疊範本中的自訂資源搭配使用。是範本開發人員以屬性名稱在資源上呼叫 Fn::GetAtt 時傳回的資料。

    以下為自訂資源回應的範例:

    { "Status" : "SUCCESS", "PhysicalResourceId" : "Tester1", "StackId" : "arn:aws:cloudformation:us-west-2:123456789012:stack/mystack/5b918d10-cd98-11ea-90d5-0a9cd3354c10", "RequestId" : "unique id for this create request", "LogicalResourceId" : "MySeleniumTester", "Data" : { "resultsPage" : "http://www.myexampledomain/test-results/guid", "lastUpdate" : "2012-11-14T03:30Z" } }

    如需有關Create要求之回應物件的詳細資訊,請參閱建立主題。

    StackIdRequestId 以及 LogicalResourceId 欄位必須從請求逐字複製。

  4. CloudFormation 聲明堆棧狀態為CREATE_COMPLETECREATE_FAILED。如果已成功建立堆疊,範本開發人員可透過 Fn::GetAtt 來存取利用所建立自訂資源的輸出值。

    例如,示範的自訂資源範本會使用 Fn::GetAtt 將資源輸出複製到堆疊輸出中:

    "Outputs" : { "topItem" : { "Value" : { "Fn::GetAtt" : ["MySeleniumTest", "resultsPage"] } }, "numRespondents" : { "Value" : { "Fn::GetAtt" : ["MySeleniumTest", "lastUpdate"] } } }

步驟 2:更新堆疊

若要更新現有堆疊,您必須提交範本,以指定堆疊中資源屬性的更新,如下列範例所示。 CloudFormation僅更新範本中已指定變更的資源。如需詳細資訊,請參閱瞭解堆疊資源的更新行為

您可以更新需要替換基礎實體資源的自訂資源。當您更新 CloudFormation 範本中的自訂資源時, CloudFormation 會將更新要求傳送至該自訂資源。若需替換自訂資源,新的自訂資源必須以新的實體 ID 傳送回應。當 CloudFormation收到回應時,它會比較新舊自訂資源PhysicalResourceId之間的比較。如果它們不同,請將更新 CloudFormation 識別為替換,並將刪除請求發送到舊資源,如中所示步驟 3:刪除堆疊

注意

如果您沒有對自訂資源進行變更,則不 CloudFormation 會在堆疊更新期間傳送要求給該資源。

  1. 範本開發人員可啟動包含自訂資源的堆疊更新。在更新期間,範本開發人員可以指定堆疊範本中的新 Properties (屬性)。

    以下是使用自訂資源類型的堆疊範本 Update 範例:

    { "AWSTemplateFormatVersion" : "2010-09-09", "Resources" : { "MySeleniumTest" : { "Type": "Custom::SeleniumTester", "Version" : "1.0", "Properties" : { "ServiceToken": "arn:aws:sns:us-west-2:123456789012:CRTest", "seleniumTester" : "SeleniumTest()", "endpoints" : [ "http://mysite.com", "http://myecommercesite.com/", "http://search.mysite.com", "http://mynewsite.com" ], "frequencyOfTestsPerHour" : [ "3", "2", "4", "3" ] } } }, "Outputs" : { "topItem" : { "Value" : { "Fn::GetAtt" : ["MySeleniumTest", "resultsPage"] } }, "numRespondents" : { "Value" : { "Fn::GetAtt" : ["MySeleniumTest", "lastUpdate"] } } } }
  2. CloudFormation 傳送 Amazon SNS 通知給資源提供者,其中包"RequestType" : "Update"含與Create呼叫類似的資訊,但OldResourceProperties欄位包含舊資源屬性,並 ResourceProperties 包含更新的 (如果有的話) 資源屬性。

    以下是 Update 請求範例:

    { "RequestType" : "Update", "ResponseURL" : "http://pre-signed-S3-url-for-response", "StackId" : "arn:aws:cloudformation:us-west-2:123456789012:stack/mystack/5b918d10-cd98-11ea-90d5-0a9cd3354c10", "RequestId" : "uniqueid for this update request", "LogicalResourceId" : "MySeleniumTester", "ResourceType" : "Custom::SeleniumTester", "PhysicalResourceId" : "Tester1", "ResourceProperties" : { "seleniumTester" : "SeleniumTest()", "endpoints" : [ "http://mysite.com", "http://myecommercesite.com/", "http://search.mysite.com", "http://mynewsite.com" ], "frequencyOfTestsPerHour" : [ "3", "2", "4", "3" ] }, "OldResourceProperties" : { "seleniumTester" : "SeleniumTest()", "endpoints" : [ "http://mysite.com", "http://myecommercesite.com/", "http://search.mysite.com" ], "frequencyOfTestsPerHour" : [ "3", "2", "4" ] } }

    如需有關要求之要Update求物件的詳細資訊,請參閱更新主題。

  3. 自訂資源提供者會處理由傳送的資料 CloudFormation。自訂資源會執行更新,並將SUCCESS或的回應傳送FAILED至 S3 URL。 CloudFormation 然後比較舊PhysicalResourceIDs的和新的自定義資源。如果它們不同,請 CloudFormation 識別更新需要替換,並將刪除請求發送到舊資源。以下範例示範custom resource provider對 Update 請求的回應。

    { "Status" : "SUCCESS", "StackId" : "arn:aws:cloudformation:us-west-2:123456789012:stack/mystack/5b918d10-cd98-11ea-90d5-0a9cd3354c10", "RequestId" : "uniqueid for this update request", "LogicalResourceId" : "MySeleniumTester", "PhysicalResourceId" : "Tester2" }

    如需有關Update要求之回應物件的詳細資訊,請參閱更新主題。

    StackIdRequestId 以及 LogicalResourceId 欄位必須從請求逐字複製。

  4. CloudFormation 聲明堆棧狀態為UPDATE_COMPLETEUPDATE_FAILED。如果更新失敗,即會復原堆疊。如果已成功更新堆疊,範本開發人員即可使用 Fn::GetAtt 來存取已建立之自訂資源的任何新輸出值。

步驟 3:刪除堆疊

  1. 範本開發人員會刪除包含自訂資源的堆疊。 CloudFormation獲取堆棧模板中指定的當前屬性以及SNS主題,並準備向自定義資源提供者發出請求。

  2. CloudFormation 傳送 Amazon SNS 通知給資源提供者,其中包"RequestType" : "Delete"含堆疊的最新資訊、堆疊範本中的自訂資源屬性,以及用URL於回應的 S3。

    每當您刪除堆疊或進行移除或取代自訂資源的更新時,請 CloudFormation 比較新舊自訂資源PhysicalResourceId之間的比較。如果它們不同,請將更新 CloudFormation 識別為取代項目,並傳送舊資源 (OldPhysicalResource) 的刪除要求,如下列Delete要求範例所示。

    { "RequestType" : "Delete", "ResponseURL" : "http://pre-signed-S3-url-for-response", "StackId" : "arn:aws:cloudformation:us-west-2:123456789012:stack/mystack/5b918d10-cd98-11ea-90d5-0a9cd3354c10", "RequestId" : "unique id for this delete request", "ResourceType" : "Custom::SeleniumTester", "LogicalResourceId" : "MySeleniumTester", "PhysicalResourceId" : "Tester1", "ResourceProperties" : { "seleniumTester" : "SeleniumTest()", "endpoints" : [ "http://mysite.com", "http://myecommercesite.com/", "http://search.mysite.com", "http://mynewsite.com" ], "frequencyOfTestsPerHour" : [ "3", "2", "4", "3" ] } }

    如需有關要求之要Delete求物件的詳細資訊,請參閱Delete主題。

    DescribeStackResourceDescribeStackResources 以及 ListStackResources 會顯示使用者定義的名稱 (如有指定)。

  3. 自訂資源提供者會處理由傳送的資料, CloudFormation 並判斷Delete要求是否成功。然後,資源提供者會使用傳URL CloudFormation 送的 S3 傳送SUCCESS或的回應FAILED。若要成功刪除含自訂資源的堆疊,custom resource provider 必須成功回應刪除請求。

    以下為 custom resource provider對 Delete 請求的回應範例:

    { "Status" : "SUCCESS", "StackId" : "arn:aws:cloudformation:us-west-2:123456789012:stack/mystack/5b918d10-cd98-11ea-90d5-0a9cd3354c10", "RequestId" : "unique id for this delete request", "LogicalResourceId" : "MySeleniumTester", "PhysicalResourceId" : "Tester1" }

    如需有關Delete要求之回應物件的詳細資訊,請參閱Delete主題。

    StackIdRequestId 以及 LogicalResourceId 欄位必須從請求逐字複製。

  4. CloudFormation 聲明堆棧狀態為DELETE_COMPLETEDELETE_FAILED