aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.71k stars 3.94k forks source link

Route53: Create PrivateHostedZone with just VpcId #24736

Open MintuJ opened 1 year ago

MintuJ commented 1 year ago

Describe the feature

Similar to Cloudformation, enable creation of HostedZones with just VpcId and VpcRegion

Use Case

Creating a PrivateHostedZone requires an IVpc today. If the PrivateHostedZone is in a different stack, the VPC object creation requires a Vpc.fromLookup call that creates a dependency between stack creation.

Eg. For a new service creation in a region:

  1. VPC creation in Stack1
  2. Run build in stack2 for cdk context to be populated
  3. Create PHZ Stack2

Proposed Solution

Enable PrivateHostedZone creation with just VpcId and region similar to the Cloudformation template https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-route53-hostedzone.html#cfn-route53-hostedzone-vpcs

This would help remove the dependency by just exporting the VpcId from the VPC stack and referring to it in the PrivateHostedZone stack.

Eg. For a new service creation in a region:

  1. VPC creation in Stack1
  2. Create PHZ Stack2

Other Information

No response

Acknowledgements

CDK version used

1.197.0

Environment details (OS name and version, etc.)

MacOs

pahud commented 1 year ago

Hi

You should be able to export and import the VPC cross-stack like this:

export class VpcStack extends Stack {
  readonly vpc: ec2.IVpc;
  constructor(scope: Construct, id: string, props: StackProps = {}) {
    super(scope, id, props)

    this.vpc = new ec2.Vpc(this, 'Vpc');
  }
}

export interface PHZStackProps extends StackProps {
  readonly vpc: ec2.IVpc;
}

export class PHZStack extends Stack {
  constructor(scope: Construct, id: string, props: PHZStackProps) {
    super(scope, id, props)

    new route53.PrivateHostedZone(this, 'PHZ', {
      vpc: props.vpc,
      zoneName: 'foo.com',
    })
  }
}

Can you elaborate more about your pain points?

github-actions[bot] commented 1 year ago

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

MintuJ commented 1 year ago

Is there a way for PHZStackProps.vpc be initialized if VpcStack is created in a defined either outside the CDK, or in a different CDK package and there is no way to pass VpcStack.vpc in props?

Our team has some tooling that requires each stack to be in different packages. We want to avoid Vpc.fromLookup since it creates a dependency between the stacks at build time. One option is to use L1 HostedZone construct CfnHostedZone, but we would prefer an L2 construct.

If constructor just requires VpcId which is a string, it can be exported from the VPCStack and referred in the PHZ stack.