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

[core] availabilityZones with account and region specified not working in nested stacks #5594

Closed
cmckni3 opened this issue Dec 31, 2019 · 14 comments · Fixed by #5638
Closed
Assignees
Labels
@aws-cdk/aws-cloudformation Related to AWS CloudFormation bug This issue is a bug. contribution/core This is a PR that came from AWS. p1

Comments

@cmckni3
Copy link
Contributor

cmckni3 commented Dec 31, 2019

Availability zones are not resolved correctly for VPCs created in nested stacks. The value is not resolved and the dummyValue is returned. In the case of availabilityZones, the dummy value is ['dummy1a', 'dummy1b', 'dummy1c'].

Reproduction Steps

export class MyNestedStack extends cfn.NestedStack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.NestedStackProps) {
    super(scope, id, props);

    new ec2.Vpc(this, 'VPCA');
  }
}

Error Log

From CloudFormation:

Value (dummy1a) for parameter availabilityZone is invalid. 

Environment

  • CLI Version : 1.19.0
  • Framework Version: 1.19.0
  • OS : macOS
  • Language : TypeScript

Other


This is 🐛 Bug Report

@cmckni3 cmckni3 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 31, 2019
@cmckni3
Copy link
Contributor Author

cmckni3 commented Dec 31, 2019

I tracked the issue down to reportMissingContext. I noticed this method does not invoke the context providers in nested stacks. I confirmed my suspicion by replacing the method with the following code:

reportMissingContext(report) {
  if (this.nested) {
    this.nestedStackParent.reportMissingContext(report);
  } else {
    this._missingContext.push(report);
  }
}

I think there's a bigger issue the code above won't solve but I will leave that to the CDK team.

@cmckni3
Copy link
Contributor Author

cmckni3 commented Dec 31, 2019

For now, I was able to work around the issue by placing the following code into my NestedStack.

cdk.ContextProvider.getValue(scope, {
  provider: cxapi.AVAILABILITY_ZONE_PROVIDER,
  dummyValue: ['dummy1a', 'dummy1b', 'dummy1c'],
}).value;

N.B. it is pulling the context values from the parent stack using scope

@eladb eladb self-assigned this Dec 31, 2019
@eladb eladb added p1 @aws-cdk/aws-cloudformation Related to AWS CloudFormation contribution/core This is a PR that came from AWS. labels Dec 31, 2019
@eladb eladb assigned NetaNir and unassigned eladb Dec 31, 2019
@eladb
Copy link
Contributor

eladb commented Dec 31, 2019

@NetaNir is picking this up

@SomayaB SomayaB removed the needs-triage This issue or PR still needs to be triaged. label Jan 2, 2020
eladb pushed a commit that referenced this issue Jan 5, 2020
Report missing context when context is not available.
Prior to this change the synthesize method return before reporting missing context when invoked on a nested stacks.
fixes #5594
@davidsteed
Copy link

I tried doing as advised above:

class MyNestedStack extends cfn.NestedStack {
constructor(scope: Construct, id: string, props?: cfn.NestedStackProps) {
super(scope, id, props);

    ContextProvider.getValue(scope, {
        provider: cxapi.AVAILABILITY_ZONE_PROVIDER,
        dummyValue: ['dummy1a', 'dummy1b', 'dummy1c'],
    }).value;

    new s3.Bucket(this, 'NestedBucket');
}

}

export class ParentStack extends Stack {
constructor(scope: Construct, id: string, props: MultistackProps) {
super(scope, id, props);

    new MyNestedStack(scope, 'Nested1');

}

}
but still have the problem. Am I doing something wrong?

@cmckni3
Copy link
Contributor Author

cmckni3 commented Jan 7, 2020

@davidsteed I don’t see a vpc or any other resource that would need AZs in your stack.

@davidsteed
Copy link

davidsteed commented Jan 7, 2020 via email

@davidsteed
Copy link

What I am actually trying to do is register certificates in a stack in us-east-1 and use them in another stack in another region.

@cmckni3
Copy link
Contributor Author

cmckni3 commented Jan 7, 2020

I think that error is caused by new MyNestedStack(scope, 'Nested1');

It's usually new MyNestedStack(this, 'Nested1');

I believe scope in this case is actually the cdk App which isn't a Stack.

@eladb
Copy link
Contributor

eladb commented Jan 7, 2020

This is supposed to be fixed in the next release (soon!)

@davidsteed
Copy link

new MyNestedStack(this, 'Nested1'); appears to work. The example https://github.com/aws/aws-cdk/tree/master/packages/%40aws-cdk/aws-cloudformation needs to be changed to reflect this. It also does not compile -which made me suspicious anyway:
constructor(scope: Construct, id: string, props: cfn.NestedStackProps)
must be changed to constructor(scope: Construct, id: string, props?: cfn.NestedStackProps)

@cmckni3
Copy link
Contributor Author

cmckni3 commented Jan 8, 2020

Right, you would need to pass in props to the nested stack or specify it as optional in TypeScript as you have done with constructor(scope: Construct, id: string, props?: cfn.NestedStackProps) (?: in TypeScript means optional for parameters/object properties).

@cmckni3
Copy link
Contributor Author

cmckni3 commented Jan 8, 2020

It sounds like you are trying to use a resource from a different stack in another region?

What I am actually trying to do is register certificates in a stack in us-east-1 and use them in another stack in another region.

Notes about certificates:

  1. If it's for ELB, the certificate and ELB must be in the same region.
  2. If it's CloudFront, the certificate must be in us-east-1

https://docs.aws.amazon.com/acm/latest/userguide/acm-regions.html

@cmckni3
Copy link
Contributor Author

cmckni3 commented Jan 8, 2020

Awesome @eladb. I see 1.20.0 was released.

@cmckni3
Copy link
Contributor Author

cmckni3 commented May 21, 2020

For posterity:

This works perfectly now without my workaround. I tried 1.40.0 and it works great. Also, nice to see the Name tag added automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-cloudformation Related to AWS CloudFormation bug This issue is a bug. contribution/core This is a PR that came from AWS. p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants