简体   繁体   English

如何从 CloudFormation 中的另一个堆栈导入现有的 S3 存储桶?

[英]How to import existing S3 bucket from another stack in CloudFormation?

I'd like to import an existing S3 bucket that is created in another stack.我想导入在另一个堆栈中创建的现有 S3 存储桶。 I tried to do it using two CF templates below but when I try to create a consumer stack it leads to a CREATE_FAILED event with an "already exists" error message.我尝试使用下面的两个 CF 模板来做到这一点,但是当我尝试创建消费者堆栈时,它会导致 CREATE_FAILED 事件并显示“已存在”错误消息。

Producer template:生产者模板:

"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Main stack that creates S3 bucket.",
"Resources": {
"myS3Bucket" : {
      "Type" : "AWS::S3::Bucket",
      "DeletionPolicy" : "Retain",
      "Properties": {"BucketName": "bucket-name" }
    }
},
"Outputs": {
    "BucketId": {
        "Description": "Bucket ID",
        "Value": { "Ref": "myS3Bucket" },
        "Export": { "Name": { "Fn::Sub": "${AWS::StackName}-BUCKETID" }}
        }
    }
} 

And this is a consumer stack template:这是一个消费者堆栈模板:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "AWS CloudFormation Sample Template.",
    "Resources" : { 
        "S3ImportedBucket" : {  
            "Type" : "AWS::S3::Bucket",
            "Properties" : {
                "BucketName" : { "Fn::ImportValue" : "main-BUCKETID"} 
            }
        }
    }
}

What should be changed in the templates to make it work?应该在模板中更改什么才能使其工作?

tl;dr : the concept of outputs is right but you are using it in a wrong way tl; dr输出的概念是正确的,但您以错误的方式使用它

explanation In cloudformation templates, you are posting解释在 cloudformation 模板中,您正在发布

Both create a resource of s3 bucket type with the same name so whatever runs first will create the s3 bucket两者都创建了一个具有相同名称的 s3 存储桶类型的资源,因此首先运行的将创建 s3 存储桶

you can check here for reference你可以在这里查看以供参考

however, the output can be used as a reference in other templates like if you are uploading something from lambda so you can set that bucket as env var and use it inside the lambda (just an example)但是,输出可以用作其他模板中的参考,例如如果您从 lambda 上传某些内容,那么您可以将该存储桶设置为 env var 并在 lambda 中使用它(只是一个示例)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 CloudFormation 将堆栈导出的现有 S3 存储桶导入另一个堆栈 YAML - How to import an existing S3 bucket exported by a stack, into another stack via CloudFormation YAML CloudFormation 为现有 s3 存储桶添加触发器 - CloudFormation add trigger for existing s3 bucket AWS - 使用 CloudFormation 将数据从一个 S3 存储桶移动到另一个存储桶 - AWS - Moving data from one S3 bucket to another with CloudFormation 将 s3 存储桶从一个堆栈导入到另一个堆栈 - Import s3 bucket from one stack to other stack Cloudformation 脚本堆栈构建使用现有 s3 存储桶而不是创建新存储桶(错误:s3-bucket-name 已存在) - Cloudformation script stack building use existing s3 bucket instead of creating new (Error: s3-bucket-name already exists) 如何有条件地在 cloudformation 中创建 s3 存储桶? - How to conditionally create s3 bucket in cloudformation? 使用 CloudFormation 在现有 AWS S3 存储桶中创建文本文件 - Create a text file in an existing AWS S3 bucket using CloudFormation AWS CloudFormation:为现有 S3 存储桶配置复制 - AWS CloudFormation: Configuring replication for an existing S3 bucket AWS:如何通过 CloudFormation 更新现有的 S3 存储桶策略? - AWS: How to update an existing S3 bucket-policy via CloudFormation? 如何使用 cloudformation 从 S3 ARN 获取 S3 存储桶名称 - How to get S3 bucket name from S3 ARN using cloudformation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM