Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stacks are getting deployed in Alphabetical order #1635

Closed
Black742 opened this issue Jan 29, 2019 · 5 comments
Closed

Stacks are getting deployed in Alphabetical order #1635

Black742 opened this issue Jan 29, 2019 · 5 comments
Assignees
Labels
closing-soon This issue will automatically close in 4 days unless further comments are made.

Comments

@Black742
Copy link

I have noticed the Stacks are getting deployed in Alphabetical order on the latest version(0.22.0) which is not happened on the old version(0.14.0 - Which I used before the latest version).

With the below example, was expecting BCdkStack should be deployed before ACdkStack as i have order in my stack definition.
But on the V0.22.0, the stacks are getting deployed as Alphabetical order which breaking the deployment.

Have attached screenshot which shows the cdk deploy behavior for two different versions.

Please suggest is it a expected behavior on the latest version, if so let me know how to define dependency between stacks.

#!/usr/bin/env node
import s3 = require('@aws-cdk/aws-s3');
import cdk = require('@aws-cdk/cdk');
import cloudtrail = require('@aws-cdk/aws-cloudtrail')

class ACdkStack extends cdk.Stack {
  constructor(scope: cdk.App, name: string, props?: cdk.StackProps) {
    super(scope, name,props);
    let thisOjb: any = this
    const bucket =  s3.Bucket.import(thisOjb, 'Bucket', {
        bucketArn: 'arn:aws:s3:::testing202020asd'
    });

    new cloudtrail.CfnTrail(this, 'Trail', {
      includeGlobalServiceEvents: true,
      isMultiRegionTrail: true,
      trailName: "testing",
      isLogging: true,
      s3BucketName: bucket.bucketName,
      s3KeyPrefix: 'cloudtrail'

  });
}
}

class BCdkStack extends cdk.Stack {
    constructor(scope: cdk.App, name: string, props?: cdk.StackProps) {
      super(scope, name,props);
      let thisOjb: any = this
      let bucket = new s3.Bucket(thisOjb, 'BucketRef', {
        bucketName: "testing202020asd"
    });
    }
  }

const app = new cdk.App();


new BCdkStack(app, 'BCdkStack');
new ACdkStack(app, 'ACdkStack');

app.run();

Version 0.22.0
cdk-error-v0 22 0
Version 0.14.0
cdk-v0 14 0

@RomainMuller
Copy link
Contributor

RomainMuller commented Jan 30, 2019

The CDK now understands dependencies between the stacks and will honor them (#1436, #1511) -- this conscious ordering decision had a side effect of causing your stack's previous "definition order" to be lost there.

Your issue in essence here is that the way you're doing the Bucket.import call does not allow for the CDK to find the dependency out.

At the same time as the dependency order deployment, a change was introduced to allow for transparent cross-stack references (#1436). You could hence change your code to something like:

#!/usr/bin/env node
import s3 = require('@aws-cdk/aws-s3');
import cdk = require('@aws-cdk/cdk');
import cloudtrail = require('@aws-cdk/aws-cloudtrail')

interface ACdkStackProps extends cdk.StackProps {
  bucket: s3.IBucket;
}

class ACdkStack extends cdk.Stack {
  constructor(scope: cdk.App, name: string, props: ACdkStackProps) {
    super(scope, name, props);
    new cloudtrail.CfnTrail(this, 'Trail', {
      includeGlobalServiceEvents: true,
      isMultiRegionTrail: true,
      trailName: "testing",
      isLogging: true,
      s3BucketName: props.bucket.bucketName,
      s3KeyPrefix: 'cloudtrail'
    });
  }
}

class BCdkStack extends cdk.Stack {
  public readonly bucket: s3.IBucket;

  constructor(scope: cdk.App, name: string, props?: cdk.StackProps) {
    super(scope, name, props);
    this.bucket = new s3.Bucket(this, 'BucketRef', {
      bucketName: "testing202020asd"
    });
  }
}

const app = new cdk.App();

const bStack = new BCdkStack(app, 'BCdkStack');
new ACdkStack(app, 'ACdkStack', { bucket: bStack.bucket });

app.run();

@RomainMuller RomainMuller added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Jan 30, 2019
@RomainMuller RomainMuller self-assigned this Jan 30, 2019
@Black742
Copy link
Author

Black742 commented Jan 31, 2019

@RomainMuller Thanks for the clarification. The previous example had s3 bucket created through cdk so created dependencies between stack but how do i make order between two stacks which has custom resource type or external templates include by using cdk.include options.

Please refer the below example, I need to deploy BCdkStack first before ACdkStack. So, how do i define dependency here?

#!/usr/bin/env node
import s3 = require('@aws-cdk/aws-s3');
import cdk = require('@aws-cdk/cdk');
import cloudtrail = require('@aws-cdk/aws-cloudtrail')


class ACdkStack extends cdk.Stack {
  constructor(scope: cdk.App, name: string,  props?: cdk.StackProps) {
    super(scope, name, props);
     new cloudformation.CustomResource(this, 'Id', {
        lambdaProvider: new lambda.SingletonFunction(this, 'permissionBopundaryAttachmentLambda', {
            uuid: 'a7c1cf66-b17f-4240-8922-be5396d58040',
            code: code,
            role: "role-ref:,
            handler: 'test.main',
            timeout: 120,
            runtime: lambda.Runtime.Python36
        }),
        properties: lambdaProbs
    });
  }
}

class BCdkStack extends cdk.Stack {

  constructor(scope: cdk.App, name: string, props?: cdk.StackProps) {
    super(scope, name, props);
    let template: any =yaml.safeLoad(fs.readFileSync(path.join(__dirname, SPEC_FILE_LOCATION), 'utf8'));
    new cdk.Include(this, 'IncludeConfigTemplate', { template });
  }
}

const app = new cdk.App();

new BCdkStack(app, 'BCdkStack');
new ACdkStack(app, 'ACdkStack');

app.run();

@Black742
Copy link
Author

Black742 commented Feb 3, 2019

@RomainMuller How do i make dependency between two stacks created through Custom resource and CDK include feature. Please advise me on this.

@rix0rrr
Copy link
Contributor

rix0rrr commented Feb 4, 2019

You can do the following:

stack1.addDependency(stack2);

Stack2 will deploy before stack1 after doing that.

@rix0rrr rix0rrr closed this as completed Feb 4, 2019
@KhushbooKaul2002
Copy link

Can someone please give an example of how this is done . Stack1.addDependency(stack2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closing-soon This issue will automatically close in 4 days unless further comments are made.
Projects
None yet
Development

No branches or pull requests

4 participants