Amazon Simple Notification Service-backed Custom Resources
When you associate an Amazon SNS topic with a custom resource, you use Amazon SNS
notifications to
trigger custom provisioning logic. With custom resources and Amazon SNS, you can enable
scenarios
such as adding new resources to a stack and injecting dynamic data into a stack. For
example,
when you create a stack, AWS CloudFormation can send a create request to a topic that's
monitored by an application that's running on an Amazon Elastic Compute Cloud instance.
The Amazon SNS notification
triggers the application to carry out additional provisioning tasks, such as retrieve
a pool
of white-listed Elastic IPs. After it's done, the application sends a response (and
any output
data) that notifies AWS CloudFormation to proceed with the stack operation.
Walkthrough: Using Amazon Simple Notification Service to Create Custom Resources
This walkthrough will step through the custom resource process, explaining the sequence of events and messages sent and received as a result of custom resource stack creation, updates, and deletion.
Step 1: Stack Creation
-
The template developer creates an AWS CloudFormation stack that contains a custom resource; in the template example below, we use the custom resource type name
Custom::SeleniumTesterfor the custom resourceMySeleniumTest.The custom resource type is declared with a service token, optional provider-specific properties, and optional
Fn::GetAttattributes that are defined by the custom resource provider. These properties and attributes can be used to pass information from the template developer to the custom resource provider and vice-versa. Custom resource type names must be alphanumeric and can have a maximum length of 60 characters.The following example shows a template that has both custom properties and return attributes:
Copy{ "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"] } } } }Note
The names and values of the data accessed with
are returned by the custom resource provider during the provider's response to AWS CloudFormation. If the custom resource provider is a third-party, then the template developer must obtain the names of these return values from the custom resource provider.Fn::GetAtt -
AWS CloudFormation sends an Amazon SNS notification to the resource provider with a
"RequestType" : "Create"that contains information about the stack, the custom resource properties from the stack template, and an S3 URL for the response.The SNS topic that is used to send the notification is embedded in the template in the
ServiceTokenproperty. To avoid using a hard-coded value, a template developer can use a template parameter so that the value is entered at the time the stack is launched.The following example shows a custom resource
Createrequest which includes a custom resource type name,Custom::SeleniumTester, created with aLogicalResourceIdofMySeleniumTester:Copy{ "RequestType" : "Create", "ResponseURL" : "http://pre-signed-S3-url-for-response", "StackId" : "arn:aws:cloudformation:us-west-2:123456789012:stack/stack-name/guid", "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" ] } } -
The custom resource provider processes the data sent by the template developer and determines whether the
Createrequest was successful. The resource provider then uses the S3 URL sent by AWS CloudFormation to send a response of eitherSUCCESSorFAILED.Depending on the response type, different response fields will be expected by AWS CloudFormation. Refer to the Responses section in the reference topic for the RequestType that is being processed.
In response to a create or update request, the custom resource provider can return data elements in the Data field of the response. These are name/value pairs, and the names correspond to the
attributes used with the custom resource in the stack template. The values are the data that is returned when the template developer callsFn::GetAtton the resource with the attribute name.Fn::GetAttThe following is an example of a custom resource response:
Copy{ "Status" : "SUCCESS", "PhysicalResourceId" : "Tester1", "StackId" : "arn:aws:cloudformation:us-west-2:123456789012:stack/stack-name/guid", "RequestId" : "unique id for this create request", "LogicalResourceId" : "MySeleniumTester", "Data" : { "resultsPage" : "http://www.myexampledomain/test-results/guid", "lastUpdate" : "2012-11-14T03:30Z", } }The
StackId,RequestId, andLogicalResourceIdfields must be copied verbatim from the request. -
AWS CloudFormation declares the stack status as
CREATE_COMPLETEorCREATE_FAILED. If the stack was successfully created, the template developer can use the output values of the created custom resource by accessing them withFn::GetAtt.For example, the custom resource template used for illustration used
to copy resource outputs into the stack outputs:Fn::GetAttCopy"Outputs" : { "topItem" : { "Value" : { "Fn::GetAtt" : ["MySeleniumTest", "resultsPage"] } }, "numRespondents" : { "Value" : { "Fn::GetAtt" : ["MySeleniumTest", "lastUpdate"] } } }
For detailed information about the request and response objects involved in
Create requests, see Create in the Custom Resource
Reference.
Step 2: Stack Updates
To update an existing stack, you must submit a template that specifies updates for the properties of resources in the stack, as shown in the example below. AWS CloudFormation updates only the resources that have changes specified in the template. For more information about updating stacks, see AWS CloudFormation Stacks Updates.
You can update custom resources that require a replacement of the underlying physical
resource. When you update a custom resource in an AWS CloudFormation template, AWS
CloudFormation sends an update request to that custom resource. If a custom resource
requires a replacement, the new custom resource must send a response with the new
physical ID. When AWS CloudFormation receives the response, it compares the PhysicalResourceId between the old and new custom resources. If they are different, AWS CloudFormation
recognizes the update as a replacement and sends a delete request to the old resource,
as shown in Step 3: Stack Deletion.
Note
If you didn't make changes to the custom resource, AWS CloudFormation won't send requests to it during a stack update.
-
The template developer initiates an update to the stack that contains a custom resource. During an update, the template developer can specify new Properties in the stack template.
The following is an example of an
Updateto the stack template using a custom resource type:Copy{ "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"] } } } } -
AWS CloudFormation sends an Amazon SNS notification to the resource provider with a
"RequestType" : "Update"that contains similar information to theCreatecall, except that theOldResourcePropertiesfield contains the old resource properties, and ResourceProperties contains the updated (if any) resource properties.The following is an example of an
Updaterequest:Copy{ "RequestType" : "Update", "ResponseURL" : "http://pre-signed-S3-url-for-response", "StackId" : "arn:aws:cloudformation:us-west-2:123456789012:stack/stack-name/guid", "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" ] } } -
The custom resource provider processes the data sent by AWS CloudFormation. The custom resource performs the update and sends a response of either
SUCCESSorFAILEDto the S3 URL. AWS CloudFormation then compares thePhysicalResourceIDsof old and new custom resources. If they are different, AWS CloudFormation recognizes that the update requires a replacement and sends a delete request to the old resource. The following example demonstrates the custom resource provider response to anUpdaterequest.Copy{ "Status" : "SUCCESS", "StackId" : "arn:aws:cloudformation:us-west-2:123456789012:stack/stack-name/guid", "RequestId" : "uniqueid for this update request", "LogicalResourceId" : "MySeleniumTester", "PhysicalResourceId" : "Tester2" }The
StackId,RequestId, andLogicalResourceIdfields must be copied verbatim from the request. -
AWS CloudFormation declares the stack status as
UPDATE_COMPLETEorUPDATE_FAILED. If the update fails, the stack rolls back. If the stack was successfully updated, the template developer can access any new output values of the created custom resource with.Fn::GetAtt
For detailed information about the request and response objects involved in
Update requests, see Update in the Custom Resource
Reference.
Step 3: Stack Deletion
-
The template developer deletes a stack that contains a custom resource. AWS CloudFormation gets the current properties specified in the stack template along with the SNS topic, and prepares to make a request to the custom resource provider.
-
AWS CloudFormation sends an Amazon SNS notification to the resource provider with a
"RequestType" : "Delete"that contains current information about the stack, the custom resource properties from the stack template, and an S3 URL for the response.Whenever you delete a stack or make an update that removes or replaces the custom resource, AWS CloudFormation compares the
PhysicalResourceIdbetween the old and new custom resources. If they are different, AWS CloudFormation recognizes the update as a replacement and sends a delete request for the old resource (OldPhysicalResource), as shown in the following example of aDeleterequest.Copy{ "RequestType" : "Delete", "ResponseURL" : "http://pre-signed-S3-url-for-response", "StackId" : "arn:aws:cloudformation:us-west-2:123456789012:stack/stack-name/guid", "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" ] } }DescribeStackResource,DescribeStackResources, andListStackResourcesdisplay the user-defined name if it has been specified. -
The custom resource provider processes the data sent by AWS CloudFormation and determines whether the
Deleterequest was successful. The resource provider then uses the S3 URL sent by AWS CloudFormation to send a response of eitherSUCCESSorFAILED. To successfully delete a stack with a custom resource, the custom resource provider must respond successfully to a delete request.The following is an example of a custom resource provider response to a
Deleterequest:Copy{ "Status" : "SUCCESS", "StackId" : "arn:aws:cloudformation:us-west-2:123456789012:stack/stack-name/guid", "RequestId" : "unique id for this delete request", "LogicalResourceId" : "MySeleniumTester", "PhysicalResourceId" : "Tester1" }The
StackId,RequestId, andLogicalResourceIdfields must be copied verbatim from the request. -
AWS CloudFormation declares the stack status as
DELETE_COMPLETEorDELETE_FAILED.
For detailed information about the request and response objects involved in
Delete requests, see Delete in the Custom Resource Reference.

