Skip to content

Commit

Permalink
feat(route53): fromPublicHostedZoneAttributes method with zoneName (#…
Browse files Browse the repository at this point in the history
…19771)

fixes #18700

----

### All Submissions:

* [ ] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)?
	* [ ] Did you use `cdk-integ` to deploy the infrastructure and generate the snapshot (i.e. `cdk-integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
peterwoodworth committed Apr 11, 2022
1 parent 4fd515a commit 7867dc4
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 1 deletion.
22 changes: 22 additions & 0 deletions packages/@aws-cdk/aws-certificatemanager/test/certificate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,28 @@ describe('CertificateValidation.fromDns', () => {
});
});

test('with an imported hosted zone', () => {
const stack = new Stack();

const exampleCom = route53.PublicHostedZone.fromHostedZoneId(stack, 'ExampleCom', 'sampleid');

new Certificate(stack, 'Certificate', {
domainName: 'test.example.com',
validation: CertificateValidation.fromDns(exampleCom),
});

Template.fromStack(stack).hasResourceProperties('AWS::CertificateManager::Certificate', {
DomainName: 'test.example.com',
DomainValidationOptions: [
{
DomainName: 'test.example.com',
HostedZoneId: 'sampleid',
},
],
ValidationMethod: 'DNS',
});
});

test('with hosted zone and a wildcard name', () => {
const stack = new Stack();

Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-elasticloadbalancingv2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"@aws-cdk/aws-ec2": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-lambda": "0.0.0",
"@aws-cdk/aws-route53": "0.0.0",
"@aws-cdk/aws-s3": "0.0.0",
"@aws-cdk/cloud-assembly-schema": "0.0.0",
"@aws-cdk/core": "0.0.0",
Expand All @@ -107,6 +108,7 @@
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-lambda": "0.0.0",
"@aws-cdk/aws-s3": "0.0.0",
"@aws-cdk/aws-route53": "0.0.0",
"@aws-cdk/cloud-assembly-schema": "0.0.0",
"@aws-cdk/core": "0.0.0",
"@aws-cdk/cx-api": "0.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Match, Template } from '@aws-cdk/assertions';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as route53 from '@aws-cdk/aws-route53';
import * as s3 from '@aws-cdk/aws-s3';
import { testFutureBehavior } from '@aws-cdk/cdk-build-tools/lib/feature-flag';
import * as cdk from '@aws-cdk/core';
Expand Down Expand Up @@ -50,6 +51,30 @@ describe('tests', () => {
});
});

test('VpcEndpointService with Domain Name imported from public hosted zone', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Vpc');
const nlb = new elbv2.NetworkLoadBalancer(stack, 'Nlb', { vpc });
const endpointService = new ec2.VpcEndpointService(stack, 'EndpointService', { vpcEndpointServiceLoadBalancers: [nlb] });

// WHEN
const importedPHZ = route53.PublicHostedZone.fromHostedZoneAttributes(stack, 'MyPHZ', {
hostedZoneId: 'sampleid',
zoneName: 'MyZone',
});
new route53.VpcEndpointServiceDomainName(stack, 'EndpointServiceDomainName', {
endpointService,
domainName: 'MyDomain',
publicHostedZone: importedPHZ,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Route53::RecordSet', {
HostedZoneId: 'sampleid',
});
});

test('Attributes', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down
12 changes: 12 additions & 0 deletions packages/@aws-cdk/aws-route53/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ you know the ID and the retrieval for the `zoneName` is undesirable.
const zone = route53.HostedZone.fromHostedZoneId(this, 'MyZone', 'ZOJJZC49E0EPZ');
```

You can import a Public Hosted Zone as well with the similar `PubicHostedZone.fromPublicHostedZoneId` and `PubicHostedZone.fromPublicHostedZoneAttributes` methods:

```ts
const zoneFromAttributes = route53.PublicHostedZone.fromPublicHostedZoneAttributes(this, 'MyZone', {
zoneName: 'example.com',
hostedZoneId: 'ZOJJZC49E0EPZ',
});

// Does not know zoneName
const zoneFromId = route53.PublicHostedZone.fromPublicHostedZoneId(this, 'MyZone', 'ZOJJZC49E0EPZ');
```

## VPC Endpoint Service Private DNS

When you create a VPC endpoint service, AWS generates endpoint-specific DNS hostnames that consumers use to communicate with the service.
Expand Down
5 changes: 5 additions & 0 deletions packages/@aws-cdk/aws-route53/lib/hosted-zone-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@ export interface HostedZoneAttributes {
*/
readonly zoneName: string;
}

/**
* Reference to a public hosted zone
*/
export interface PublicHostedZoneAttributes extends HostedZoneAttributes { }
22 changes: 21 additions & 1 deletion packages/@aws-cdk/aws-route53/lib/hosted-zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import { ContextProvider, Duration, Lazy, Resource, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { HostedZoneProviderProps } from './hosted-zone-provider';
import { HostedZoneAttributes, IHostedZone } from './hosted-zone-ref';
import { HostedZoneAttributes, IHostedZone, PublicHostedZoneAttributes } from './hosted-zone-ref';
import { CaaAmazonRecord, ZoneDelegationRecord } from './record-set';
import { CfnHostedZone } from './route53.generated';
import { makeHostedZoneArn, validateZoneName } from './util';
Expand Down Expand Up @@ -237,6 +237,26 @@ export class PublicHostedZone extends HostedZone implements IPublicHostedZone {
return new Import(scope, id);
}

/**
* Imports a public hosted zone from another stack.
*
* Use when both hosted zone ID and hosted zone name are known.
*
* @param scope the parent Construct for this Construct
* @param id the logical name of this Construct
* @param attrs the PublicHostedZoneAttributes (hosted zone ID and hosted zone name)
*/
public static fromPublicHostedZoneAttributes(scope: Construct, id: string, attrs: PublicHostedZoneAttributes): IHostedZone {
class Import extends Resource implements IHostedZone {
public readonly hostedZoneId = attrs.hostedZoneId;
public readonly zoneName = attrs.zoneName;
public get hostedZoneArn(): string {
return makeHostedZoneArn(this, this.hostedZoneId);
}
}
return new Import(scope, id);
}

/**
* Role for cross account zone delegation
*/
Expand Down

0 comments on commit 7867dc4

Please sign in to comment.