AWS Builder Center: Learn, Build and Connect with builders in the AWS community
AWS Builder Center is the official home for builders on AWS. Share and read what others are working on, follow people who inspire you, explore training and workshops, and find tools to support what you're building.
如何删除 CloudFormation 中卡在 DELETE_FAILED 状态或 DELETE_IN_PROGRESS 状态的 Lambda 支持的自定义资源?
我的 AWS Lambda 支持的自定义资源在 AWS CloudFormation 中卡在 DELETE_FAILED 状态或 DELETE_IN_PROGRESS 状态。我想删除我的自定义资源。
解决方法
根据您的资源状态选择以下解决方案之一。
删除卡在 DELETE\ _FAILED 状态的自定义资源
如果您尝试删除包含 Lambda 支持的自定义资源的堆栈,而该资源不具备处理删除请求的逻辑,则会收到错误消息。如果您的自定义资源包含不正确的删除逻辑,您还可能会收到以下错误消息:"CloudFormation did not receive a response from your Custom Resource. Please check your logs for requestId [652961ca-f24e-42e0-8f74-3f4846ba282d]. If you use the Python cfn-response module, you might need to update your Lambda function code so that CloudFormation can attach the updated version."
堆栈的状态更改为 DELETE_FAILED,然后您会收到以下错误消息:"Custom Resource failed to stabilize in expected time."
要删除您的堆栈,请完成以下步骤:
- 打开 CloudFormation 控制台。
- 选择包含卡在 DELETE\ _FAILED 状态的自定义资源的堆栈。
- 选择 Actions(操作),然后选择 Delete Stack(删除堆栈)。
- 在提供要保留的资源列表的弹出窗口中,选择卡滞在 DELETE_FAILED 状态的自定义资源。然后,选择 Delete(删除)。
- 选择 Actions(操作),然后选择 Delete Stack(删除堆栈)。
堆栈的状态会更改为 DELETE_COMPLETE。
**注意:**您的自定义资源不是物理资源,因此您不必在堆栈删除后清理自定义资源。
删除卡在 DELETE_IN_PROGRESS 状态的自定义资源
当您的堆栈删除自定义资源并开展 DELETE_IN_PROGRESS 状态时,您的堆栈可能没有处理删除请求的逻辑。或者,您的堆栈可能包含不正确的删除逻辑。
要强制删除堆栈,必须手动发送 SUCCESS 信号。该信号需要 ResponseURL 和 RequestId 值,这些值包含在从 CloudFormation 发送到 Lambda 的事件中。
-
在您的 CloudFormation 模板中,确定您的自定义资源发送请求时所处的 Lambda 函数的名称。您可以从 AWS::CloudFormation::CustomResource 或 Custom::String 资源的 ServiceToken 属性中找到函数名称。
例如:
MyCustomResource: Type: "Custom::PingTester" Properties: ServiceToken: !Sub | arn:aws:lambda:us-east-1:111122223333:function:awsexamplelambdafunction -
打开 Lambda 控制台。
-
在导航窗格中,选择 Functions(函数),然后选择您在步骤 1 中确定的函数。例如,awsexamplelambdafunction 是前面代码示例中的函数名称。
-
验证您的 Lambda 函数是否将事件详细信息记录到名为 /aws/lambda/function_name 的 Amazon CloudWatch 日志组。
逻辑示例:exports.handler = function(event, context) { console.log("REQUEST RECEIVED:\n" + JSON.stringify(event)); ... }注意:如果您的 Lambda 函数不包含将事件打印到 CloudWatch 日志的逻辑,则无法获取 ResponseURL 和 RequestId 值。必须有这些值才能发送手动信号。等待大约一小时,让 CloudFormation 堆栈超时并进入 DELETE_FAILED 状态。然后,完成删除卡在 DELETE_FAILED 状态中的自定义资源部分中的步骤,以删除您的堆栈。
-
选择 Monitoring(监控)选项卡,然后选择 View logs in CloudWatch(在 CloudWatch 中查看日志)。
-
在 Amazon CloudWatch 控制台中,选择最新的日志。
**注意:**只有当您的函数有权访问 Amazon CloudWatch 日志进行日志流传输时,您才能查看 CloudWatch 日志。
-
在最新的日志中,确定将 RequestType 设置为 Delete 的事件,然后复制 RequestId、ResponseURL、StackId、LogicalResourceId 和 PhysicalResourceId 的值。例如:
Received event: { "RequestType": "Delete", "ServiceToken": "arn:aws:lambda:us-east-1:111122223333:function:awsexamplelambdafunction", "ResponseURL": "https://cloudformation-custom-resource-response-useast1.s3.us-east-1.amazonaws.com/arn%3Aaws%3Acloudformation%3Aus-east-1%3A111122223333%3Astack/awsexamplecloudformation/33ad60e0-5f25-11e9-a734-0aa6b80efab2%7CMyCustomResource%7Ce2fc8f5c-0391-4a65-a645-7c695646739?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20170313T0212304Z&X-Amz-SignedHeaders=host&X-Amz-Expires=7200&X-Amz-Credential=QWERTYUIOLASDFGBHNZCV%2F20190415%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=dgvg36bh23mk44nj454bjb54689bg43r8v011uerehiubrjrug5689ghg94hb", "StackId": "arn:aws:cloudformation:us-east-1:111122223333:stack/awsexamplecloudformation/33ad60e0-5f25-11e9-a734-0aa6b80efab2", "RequestId": "e2fc8f5c-0391-4a65-a645-7c695646739", "LogicalResourceId": "MyCustomResource", "PhysicalResourceId": "test-MyCustomResource-1URTEVUHSKSKDFF", "ResourceType": "Custom::PingTester" -
要将响应对象的 SUCCESS 响应信号发送到删除请求,请在本地命令行界面中运行以下命令。请务必包括您在步骤 7 中复制的值。
curl -H "Content-Type: ''" -X PUT -d "{\"Status\": \"SUCCESS\",\"PhysicalResourceId\": \"test-CloudWatchtrigger-1URTEVUHSKSKDFF\",\"StackId\": \"arn:aws:cloudformation:us-west-2:111122223333:stack/CRStack/5105e4b0-4a29-11ef-8c42-067744df1edb\",\"RequestId\": \"dca731ad-18ad-496c-8c77-459d904fd36b\",\"LogicalResourceId\": \"MyCustomResource\"}" "https://cloudformation-custom-resource-response-uswest2.s3-us-west-2.amazonaws.com/arn%3Aaws%3Acloudformation%3Aus-west-2%3A111122223333%3Astack/CRStack/5105e4b0-4a29-11ef-8c42-067744df1edb%7CCustomResource%7Cdca731ad-18ad-496c-8c77-459d904fd36b?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20240725T020141Z&X-Amz-SignedHeaders=host&X-Amz-Expires=7200&X-Amz-Credential=AKIA54RCMT6SJPHCTM4H%2F20240725%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Signature=3b1567adb5ec9415526251a26bac3a6c861ac77aa090a3ada0a4a4d109a7273d"
在 CloudFormation 堆栈事件中,您的自定义资源的状态更改为 DELETE_COMPLETE。
相关信息
- 语言
- 中文 (简体)

