如何将 CloudFormation WaitHandle 传递给不同的 Cloudformation 堆栈?
How to pass CloudFormation's WaitHandle to a different Cloud Formation stack?
由于云形成堆栈中资源数量的限制,我们需要有多个堆栈,其中我们的主堆栈启动其他几个较小的子堆栈。我们在主堆栈中创建了等待条件,需要将其传递给某些子堆栈中的资源。目前,我们让整个子堆栈等待 WaitHandle,而不是子堆栈中的单个资源。但是,这会不必要地显着增加我们的启动时间。
为了解决这个问题,我尝试将 WaitHandle 作为参数传递给子堆栈,但我不确定参数的类型是什么。
我还听到我的同事说您可以尝试将 WaitHandle 的 URL 作为字符串传递,然后尝试在子堆栈中使用该 url 创建一个新的 WaitHandle,但我没有看到任何有关的文档。
我在 CloudFormation 中尝试做的事情是否可行?
是的,这是可能的。根据 AWS::CloudFormation::WaitConditionHandle 文档,为了将 WaitHandle URL 作为字符串传递给您的子堆栈,
When you reference the WaitConditionHandle
resource by using the Ref
function, AWS CloudFormation returns a presigned URL. You pass this URL to applications or scripts that are running on your Amazon EC2 instances to send signals to that URL.
实际上有两种不同的方法可以实现您的目标:
- 将
{"Ref": "MyWaitHandle"}
作为参数传递给您的子堆栈,然后让子堆栈中的 resource/s 包含此参数(例如,在 Fn::Sub
string) as the presigned URL to send a 'Wait Condition Signal' to, using an HTTP PUT request (e.g. with cURL
, as used in the Creating Wait Conditions in a Template 演练中使用 ${ParentWaitHandle}
) :
curl -X PUT -H 'Content-Type:' --data-binary '{"Status": "SUCCESS", "Reason": "Configuration Complete","UniqueId": "$UNIQUE_ID", "Data": "Application has completed configuration."}' "${ParentWaitHandle}"
- 仅将
WaitCondition
资源名称传递给子堆栈(通过 Ref
),并使用 SignalResource
CloudFormation 服务 API 调用向 WaitCondition
直接资源。这使您可以跳过处理单独的 WaitConditionHandle
资源,但需要向信令资源授予 cloudformation:SignalResource
IAM 权限才能进行 API 调用。例如,使用 AWS CLI:
aws cloudformation signal-resource \
--status SUCCESS \
--unique-id $(curl -s http://169.254.169.254/latest/meta-data/instance-id) \
--logical-resource-id ${ParentWaitCondition} \
--stack-name $PARENT_STACK \
--region ${AWS::Region} \
|| true
由于云形成堆栈中资源数量的限制,我们需要有多个堆栈,其中我们的主堆栈启动其他几个较小的子堆栈。我们在主堆栈中创建了等待条件,需要将其传递给某些子堆栈中的资源。目前,我们让整个子堆栈等待 WaitHandle,而不是子堆栈中的单个资源。但是,这会不必要地显着增加我们的启动时间。 为了解决这个问题,我尝试将 WaitHandle 作为参数传递给子堆栈,但我不确定参数的类型是什么。 我还听到我的同事说您可以尝试将 WaitHandle 的 URL 作为字符串传递,然后尝试在子堆栈中使用该 url 创建一个新的 WaitHandle,但我没有看到任何有关的文档。 我在 CloudFormation 中尝试做的事情是否可行?
是的,这是可能的。根据 AWS::CloudFormation::WaitConditionHandle 文档,为了将 WaitHandle URL 作为字符串传递给您的子堆栈,
When you reference the
WaitConditionHandle
resource by using theRef
function, AWS CloudFormation returns a presigned URL. You pass this URL to applications or scripts that are running on your Amazon EC2 instances to send signals to that URL.
实际上有两种不同的方法可以实现您的目标:
- 将
{"Ref": "MyWaitHandle"}
作为参数传递给您的子堆栈,然后让子堆栈中的 resource/s 包含此参数(例如,在Fn::Sub
string) as the presigned URL to send a 'Wait Condition Signal' to, using an HTTP PUT request (e.g. withcURL
, as used in the Creating Wait Conditions in a Template 演练中使用${ParentWaitHandle}
) :
curl -X PUT -H 'Content-Type:' --data-binary '{"Status": "SUCCESS", "Reason": "Configuration Complete","UniqueId": "$UNIQUE_ID", "Data": "Application has completed configuration."}' "${ParentWaitHandle}"
- 仅将
WaitCondition
资源名称传递给子堆栈(通过Ref
),并使用SignalResource
CloudFormation 服务 API 调用向WaitCondition
直接资源。这使您可以跳过处理单独的WaitConditionHandle
资源,但需要向信令资源授予cloudformation:SignalResource
IAM 权限才能进行 API 调用。例如,使用 AWS CLI:
aws cloudformation signal-resource \
--status SUCCESS \
--unique-id $(curl -s http://169.254.169.254/latest/meta-data/instance-id) \
--logical-resource-id ${ParentWaitCondition} \
--stack-name $PARENT_STACK \
--region ${AWS::Region} \
|| true