Aws-cdk: [core] availabilityZones with account and region specified not working in nested stacks

Created on 31 Dec 2019  Â·  14Comments  Â·  Source: aws/aws-cdk

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: Bug Report

@aws-cdaws-cloudformation bug contributiocore p1

Most helpful comment

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

All 14 comments

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.

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

@NetaNir is picking this up

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?

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

There isn't one. But I am getting the error
Nested stacks must be defined within scope of another non-nested stack
and I thought this was a work around for avoiding that.
I'm guessing not.

On Tue, 7 Jan 2020 at 14:50, Chris McKnight notifications@github.com
wrote:

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

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/aws/aws-cdk/issues/5594?email_source=notifications&email_token=AECIHJ7N4OES7D4UXPZAFXTQ4SJDTA5CNFSM4KBS4MI2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIJDLUA#issuecomment-571618768,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AECIHJ7U7JH7XQBWSP62YOTQ4SJDTANCNFSM4KBS4MIQ
.

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.

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.

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

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)

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

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

Awesome @eladb. I see 1.20.0 was released.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NukaCody picture NukaCody  Â·  3Comments

artyom-melnikov picture artyom-melnikov  Â·  3Comments

mirazmamun picture mirazmamun  Â·  3Comments

peterdeme picture peterdeme  Â·  3Comments

vgribok picture vgribok  Â·  3Comments