我想重新创建在 AWS CloudFormation 堆栈之外删除的资源。
简短描述
当资源的逻辑 ID 从模板中删除时,CloudFormation 会将该资源解释为已删除。当您在模板中更改资源的逻辑 ID 时,它会启动替换更新。此更新将替换已删除的 CloudFormation 资源。
**注意:**确保更新对旧逻辑 ID 的所有引用。
解决方法
按照以下步骤恢复在 CloudFormation 堆栈之外删除的资源。
-
确定堆栈模板中被带外删除的资源的逻辑 ID。
**注意:**逻辑 ID 不同于物理 ID,后者是资源名称。
在以下示例中,Amazon Simple Storage Service (Amazon S3) 存储桶在导出操作中引用了其逻辑 ID。示例模板中的 S3Bucket 资源在 CloudFormation 的堆栈操作之外被删除。
Resources:
S3Bucket: #Logical ID of the Resource
Type: AWS::S3::Bucket
Outputs:
BucketName:
Value: !Ref S3Bucket
Export:
Name: PrimaryBucket
-
为已删除的资源(例如 S3NewBucket)分配新的逻辑 ID。
Resources:
S3NewBucket: #The changed Logical ID of the Resource
Type: AWS::S3::Bucket
Outputs:
BucketName:
Value: !Ref S3Bucket
Export:
Name: PrimaryBucket
-
查找旧逻辑 ID 的所有引用并将其替换为新的逻辑 ID。
Resources:
S3NewBucket: #The changed logical ID of the resource
Type: AWS::S3::Bucket
Outputs:
BucketName:
Value: !Ref S3NewBucket #Reference has been updated to point towards the new logical ID
Export:
Name: PrimaryBucket
-
使用编辑后的模板更新 CloudFormation 堆栈。此更新会重新创建已删除的资源。
相关信息
更新堆栈资源的行为