简体   繁体   English

在其他区域部署 AWS-CDK 堆栈

[英]Deploy AWS-CDK stack on other Regions

Basically, i have a simple stack( Virginia ) which will have resources like基本上,我有一个简单的堆栈( Virginia ),其中将有资源,如

  1. S3 Bucket(Data is there) S3 存储桶(有数据)
  2. Glue_Table Glue_Table
  3. Glue Job胶水作业

Now, i want to deploy the same stack on Europe regions like eu-central-1 .现在,我想在eu-central-1Europe地区部署相同的堆栈。

when i try to deploy the aws-cdk-stack on eu-central-1 , I am receiving this error当我尝试在eu-central-1上部署 aws-cdk-stack 时,我收到此错误

Bucket_Name already exists Exception

Command to deploy the stack is like cdk deploy <stack-name>部署堆栈的命令类似于cdk deploy <stack-name>

Goal目标

I want to deploy the existing stack into another region without deleting existing s3 bucket.我想在不删除现有 s3 存储桶的情况下将现有堆栈部署到另一个区域。

Can anyone suggest a solution for this?任何人都可以为此提出解决方案吗?

Based on the docs :基于文档

An Amazon S3 bucket name is globally unique, and the namespace is shared by all AWS accounts. Amazon S3 存储桶名称是全球唯一的,并且命名空间由所有 AWS 账户共享。 This means that after a bucket is created, the name of that bucket cannot be used by another AWS account in any AWS Region until the bucket is deleted这意味着创建存储桶后,该存储桶的名称不能被任何 AWS 区域中的另一个 AWS 账户使用,直到该存储桶被删除

When creating an S3 bucket, the bucketName is optional.创建 S3 存储桶时, bucketName是可选的。 If you leave out the bucketName a unique bucket name will be generated for you.如果您遗漏了bucketName ,将为您生成一个唯一的存储桶名称。

The additional resources in your cdk project can take the bucket name via reference. cdk 项目中的其他资源可以通过引用获取存储桶名称。

Example:例子:

import * as cdk from '@aws-cdk/core';
import * as s3 from '@aws-cdk/aws-s3';
import * as glue from '@aws-cdk/aws-glue';


export class MyStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // Generate a random bucket name that starts with your cdk stack name
    const bucket = new s3.Bucket(this, 'MyBucket');

    const database = new glue.Database(this, 'MyDatabase', {
      databaseName: 'my_database'
    });

    new glue.Table(this, 'MyTable', {
      database: database,
      bucket: bucket,  // Takes details from the bucket created
      s3Prefix: 'my-table/'
      // other table details...
    });
  }
}

I would append the region name onto the end of the bucket, this can be done easily as AWS provide an env var for this already.我会将 append 区域名称放在存储桶的末尾,这可以很容易地完成,因为 AWS 已经为此提供了一个环境变量。 So in your stack when naming the bucket apppend something like to the end of it Stack.of(this).region then the bucket name will be unique.因此,在您的堆栈中,当命名存储桶时,请在其末尾添加类似Stack.of(this).region的内容,然后存储桶名称将是唯一的。

Here is a typescript CDK example这是一个 typescript CDK 示例

const bucket: Bucket = new Bucket(this, "S3_bucket_name", {
      bucketName: "S3_bucket_name_" + Stack.of(this).region,
    });
    

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM