将s3存储桶从一个堆栈导入到另一个堆栈

时间:2019-12-10 12:42:08

标签: amazon-web-services amazon-s3 amazon-cloudformation

我已经使用cloud形成了具有deletepolicy保留的S3存储桶,我已经使用cloudformation的输出中的Export导出了创建的存储桶。 现在,我想使用import

在另一个堆栈中使用相同的s3存储桶

s3的云形成:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Creates an S3 bucket to be used for static content/website hosting.",
    "Parameters": {

        "AssetInsightId": {
            "Description": "Asset Insight ID",
            "Type": "String",
            "Default": "206153"
        },
        "ResourceOwner": {
            "Description": "tr:resource-owner",
            "Type": "String",
            "Default": "####"
        },
        "EnvironmentType": {
            "Description": "tr:environment-type",

            "Default": "preprod",
            "Type": "String",
            "AllowedValues": ["preprod", "prod"],
            "ConstraintDescription": "must specify preprod, prod."
        }
    },
    "Resources": {
        "S3Bucket": {
            "Type": "AWS::S3::Bucket",
            "DeletionPolicy": "Retain",
            "Properties": {

                "BucketName": {
                    "Fn::Sub": "a${AssetInsightId}-s3bucket-${EnvironmentType}"
                },
                "Tags": [{

                        "Key": "tr:application-asset-insight-id",
                        "Value": {
                            "Fn::Sub": "${AssetInsightId}"
                        }
                    }, {
                        "Key": "tr:environment-type",
                        "Value": {
                            "Fn::Sub": "${EnvironmentType}"
                        }
                    }
                ]
            }
        }
    },
    "Outputs": {

        "S3Bucket": {
            "Description": "Information about the value",
            "Description": "Name of the S3 Resource Bucket",
            "Value": "!Ref S3Bucket",
            "Export": {
                "Name": "ExportS3Bucket"
            }
        }
    }

}

云形成,以通过导入使用另一个模板创建的s3存储桶 第二个模板:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Creates an S3 apigateway to be used for static content/website hosting.",
    "Parameters": {

        "AssetInsightId": {
            "Description": "Asset Insight ID",
            "Type": "String",
            "Default": "206153"
        },
        "ResourceOwner": {
            "Description": "tr:resource-owner",
            "Type": "String",
            "Default": "swathi.koochi@thomsonreuters.com"
        },
        "EnvironmentType": {
            "Description": "tr:environment-type",

            "Default": "preprod",
            "Type": "String",
            "AllowedValues": ["preprod", "prod"],
            "ConstraintDescription": "must specify preprod, prod."
        },
        "endpointConfiguration": {
            "Description": "tr:endpoint-configuration",

            "Default": "REGIONAL",
            "Type": "String",
            "AllowedValues": ["REGIONAL", "EDGE"],
            "ConstraintDescription": "must specify REGIONAL, EDGE."
        }
    },
    "Resources": {

     "S3BucketImport": {
            "Type": "AWS::S3::Bucket",

            "Properties": {

                "BucketName": {"Fn::ImportValue" :  "ExportS3Bucket"}

            }
        },












        "APIGateWayRestResourceRestApi": {
            "Type": "AWS::ApiGateway::RestApi",
            "Properties": {
                "Name": "MyAPI",
                "Description": "API Gateway rest api with cloud formation",
                "EndpointConfiguration": {
                    "Types": [{
                            "Ref": "endpointConfiguration"
                        }
                    ]
                }
            }
        },
        "APIGateWayResource": {
            "Type": "AWS::ApiGateway::Resource",
            "Properties": {
                "RestApiId": {
                    "Ref": "APIGateWayRestResourceRestApi"
                },
                "ParentId": {
                    "Fn::GetAtt": ["APIGateWayRestResourceRestApi", "RootResourceId"]
                },
                "PathPart": "test"
            }

        },
        "APIGatewayPostMethod": {
            "Type": "AWS::ApiGateway::Method",
            "Properties": {
                "AuthorizationType": "NONE",
                "HttpMethod": "POST",

                "Integration": {
                    "Type": "AWS_PROXY",
                    "IntegrationHttpMethod": "POST",
                    "Uri": {
                        "Fn::Sub": "arn:aws:apigateway:us-east-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-2:861756181523:function:GreetingLambda/invocations"
                    }

                },
                "MethodResponses": [{
                        "ResponseModels": {
                            "application/json": {
                                "Ref": "PostMethodResponse"
                            }
                        },
                        "StatusCode": 200
                    }
                ],

                "ResourceId": {
                    "Ref": "APIGateWayResource"
                },

                "RestApiId": {
                    "Ref": "APIGateWayRestResourceRestApi"
                }
            }
        },
        "PostMethodResponse": {
            "Type": "AWS::ApiGateway::Model",
            "Properties": {
                "ContentType": "application/json",
                "Name": "PostMethodResponse",
                "RestApiId": {
                    "Ref": "APIGateWayRestResourceRestApi"
                },
                "Schema": {
                    "$schema": "http://json-schema.org/draft-04/schema#",
                    "title": "PostMethodResponse",
                    "type": "object",
                    "properties": {
                        "Email": {
                            "type": "string"
                        }
                    }
                }
            }
        },

        "RestApiDeployment": {
            "DependsOn": "APIGatewayPostMethod",
            "Type": "AWS::ApiGateway::Deployment",
            "Properties": {
                "RestApiId": {
                    "Ref": "APIGateWayRestResourceRestApi"
                }
            }
        },

        "RestAPIStage": {
            "Type": "AWS::ApiGateway::Stage",
            "Properties": {
                "DeploymentId": {
                    "Ref": "RestApiDeployment"
                },
                "MethodSettings": [{
                        "DataTraceEnabled": true,
                        "HttpMethod": "*",

                        "ResourcePath": "/*"
                    }
                ],
                "RestApiId": {
                    "Ref": "APIGateWayRestResourceRestApi"
                },
                "StageName": "Latest"
            }

        },
        "APIGateWayDomainName": {
            "Type": "AWS::ApiGateway::DomainName",
            "Properties": {
                "CertificateArn": {
                    "Ref": "myCertificate"
                },
                "DomainName": {
                    "Fn::Join": [".", [{
                                "Ref": "AssetInsightId"
                            }, {
                                "Ref": "EnvironmentType"
                            }, "api"]]

                },
                "EndpointConfiguration": {
                    "Types": [{
                            "Ref": "endpointConfiguration"
                        }
                    ]
                }

            }
        },
        "myCertificate": {
            "Type": "AWS::CertificateManager::Certificate",
            "Properties": {
                "DomainName": {
                    "Fn::Join": [".", [{
                                "Ref": "AssetInsightId"
                            }, {
                                "Ref": "EnvironmentType"
                            }, "api"]]

                }
            }
        }

    }
}

当我/我尝试使用“导入值”进行导入时,出现错误提示

  

S3BucketImport   CREATE_FAILED错误请求(服务:Amazon S3;状态代码:400;错误代码:400错误请求;请求ID:9387EBE0E472E559; S3扩展请求ID:o8EbE20IOoUgEMwXc7xVjuoyQT03L / nnQ7AsC94Ff1S / PkEuImeyclf1BxYeM>

1 个答案:

答案 0 :(得分:0)

@Jarmod正确指出,

在第一个模板中,使用{ "Ref" : ",S3Bucket" }

导出s3存储桶名称。

在第二个模板中,无需再次创建存储桶。如果要从资源中引用存储桶名称,则可以使用第一个模板的导出值。但是我看不到第二个模板中的任何资源都引用了S3存储桶名称。