[英]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.