AWS 'Bucket already exists' - 如何将现有资源 "migrate" 到 CloudFormation?

AWS 'Bucket already exists' - how to "migrate" existing resources to CloudFormation?

我们已经使用 terraform 手动创建了一些基础设施,包括一些 s3 存储桶。将来我想使用纯 CloudFormation 将基础架构定义为代码。

所以我创建了一个引用现有存储桶的 CloudFormation yaml 定义:

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  TheBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: my-existing-bucket-name

当我尝试应用它时,执行失败,出现 CloudFormation 堆栈事件:

The following resource(s) failed to update: [TheBucket].
12:33:47 UTC+0200   UPDATE_FAILED   AWS::S3::Bucket TheBucket
  my-existing-bucket-name already exists

如何在不重新创建资源的情况下使用 CloudFormation 开始管理 现有 资源?还是设计上不可能?

云形成模板的资源部分定义了云形成应创建哪些资源。尝试通过将现有资源定义为参数来引用现有资源。

您需要创建一个新的存储桶并将旧存储桶中的数据同步到新存储桶。我还没有看到使用修改现有 S3 存储桶的方法。

您应该可以使用“将资源导入堆栈”选项将其导入:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resource-import-existing-stack.html

如文档所述,您应该向堆栈中的现有资源添加 "DeletionPolicy": "Retain" 属性。