Deploy AWS-CDK stack on other Regions

1.8k views Asked by At

Basically, i have a simple stack(Virginia) which will have resources like

  1. S3 Bucket(Data is there)
  2. Glue_Table
  3. Glue Job

Now, i want to deploy the same stack on Europe regions like eu-central-1.

when i try to deploy the aws-cdk-stack on eu-central-1, I am receiving this error

Bucket_Name already exists Exception

Command to deploy the stack is like cdk deploy <stack-name>

Goal

I want to deploy the existing stack into another region without deleting existing s3 bucket.

Can anyone suggest a solution for this?

2

There are 2 answers

0
maafk On

Based on the docs:

An Amazon S3 bucket name is globally unique, and the namespace is shared by all AWS accounts. 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

When creating an S3 bucket, the bucketName is optional. If you leave out the bucketName a unique bucket name will be generated for you.

The additional resources in your cdk project can take the bucket name via reference.

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...
    });
  }
}

0
AnonymousAlias On

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. 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.

Here is a typescript CDK example

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