首页 > 解决方案 > AWS S3 Cloudormation 更新堆栈失败

问题描述

我有一个用于 S3 的 cloudformation 模板,我正在尝试创建一次然后更新它。但是当我尝试更新它时,它会失败并显示错误消息<bucket_name> already exists in stack <arn:id>

    "S3Bucket": {
        "Type": "AWS::S3::Bucket",
        "DeletionPolicy": "Retain",
        "Properties": {
            "AccessControl": "BucketOwnerFullControl",
            "BucketName": {
                "Fn::Join": [
                    "-",
                    [
                        {
                            "Fn::GetAtt": [
                                "VPCInfo",
                                "VPCname"
                            ]
                        },
                        {
                            "Ref": "BucketName"
                        }
                    ]
                ]
            },
            "LoggingConfiguration": {
                "DestinationBucketName": {
                    "Fn::Join": [
                        "",
                        [
                            {
                                "Fn::GetAtt": [
                                    "VPCInfo",
                                    "VPCname"
                                ]
                            },
                            "-s3logs"
                        ]
                    ]
                },
                "LogFilePrefix": {
                    "Fn::Join": [
                        "-",
                        [
                            {
                                "Fn::GetAtt": [
                                    "VPCInfo",
                                    "VPCname"
                                ]
                            },
                            {
                                "Ref": "AWS::StackName"
                            }
                        ]
                    ]
                }
            },
            "VersioningConfiguration": {
                "Status": "Enabled"
            }
        }
    }

第二次尝试更新BucketName & TopicName作为参数传递的位置

    "S3Bucket": {
        "Type": "AWS::S3::Bucket",
        "DeletionPolicy": "Retain",
        "Properties": {
            "AccessControl": "BucketOwnerFullControl",
            "BucketName": {
                "Fn::Join": [
                    "-",
                    [
                        {
                            "Fn::GetAtt": [
                                "VPCInfo",
                                "VPCname"
                            ]
                        },
                        {
                            "Ref": "BucketName"
                        }
                    ]
                ]
            },
            "LoggingConfiguration": {
                "DestinationBucketName": {
                    "Fn::Join": [
                        "",
                        [
                            {
                                "Fn::GetAtt": [
                                    "VPCInfo",
                                    "VPCname"
                                ]
                            },
                            "-s3logs"
                        ]
                    ]
                },
                "LogFilePrefix": {
                    "Fn::Join": [
                        "-",
                        [
                            {
                                "Fn::GetAtt": [
                                    "VPCInfo",
                                    "VPCname"
                                ]
                            },
                            {
                                "Ref": "AWS::StackName"
                            }
                        ]
                    ]
                }
            },
            "NotificationConfiguration": {
                "TopicConfigurations": [
                    {
                        "Topic": {
                            "Fn::Join": [
                                "",
                                [
                                    "arn:aws:sns:",
                                    {
                                        "Ref": "AWS::Region"
                                    },
                                    ":",
                                    {
                                        "Ref": "AWS::AccountId"
                                    },
                                    ":function:",
                                    {
                                        "Ref": "TopicName"
                                    }
                                ]
                            ]
                        },
                        "Event": "s3:ObjectCreated:*",
                        "Filter": {
                            "S3Key": {
                                "Rules": [
                                    {
                                        "Name": "suffix",
                                        "Value": {
                                            "Ref": "FileSuffix"
                                        }
                                    }
                                ]
                            }
                        }
                    },
                    {
                        "Topic": {
                            "Fn::Join": [
                                "",
                                [
                                    "arn:aws:sns:",
                                    {
                                        "Ref": "AWS::Region"
                                    },
                                    ":",
                                    {
                                        "Ref": "AWS::AccountId"
                                    },
                                    ":function:",
                                    {
                                        "Ref": "TopicName"
                                    }
                                ]
                            ]
                        },
                        "Event": "s3:ObjectRemoved:*",
                        "Filter": {
                            "S3Key": {
                                "Rules": [
                                    {
                                        "Name": "suffix",
                                        "Value": {
                                            "Ref": "FileSuffix"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                ]
            },
            "VersioningConfiguration": {
                "Status": "Enabled"
            }
        }
    }

更新 S3 堆栈的正确方法是什么?我尝试做两次的原因是由于这个-> https://aws.amazon.com/premiumsupport/knowledge-center/unable-validate-destination-s3/

标签: amazon-web-servicesamazon-s3amazon-cloudformation

解决方案


我相信您正在删除 CFT 并重新创建它。您的问题是 "DeletionPolicy": "Retain",即使您删除了 CFT,它仍会保留 S3 存储桶。如果你更新现有的,你应该没问题。

如果您删除 CFT 或更改 "DeletionPolicy": "Retain"为,只需手动删除存储桶"DeletionPolicy": "Delete"


推荐阅读