Skip to content

Commit 7867dc4

Browse files
feat(route53): fromPublicHostedZoneAttributes method with zoneName (#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*
1 parent 4fd515a commit 7867dc4

File tree

6 files changed

+87
-1
lines changed

6 files changed

+87
-1
lines changed

packages/@aws-cdk/aws-certificatemanager/test/certificate.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,28 @@ describe('CertificateValidation.fromDns', () => {
184184
});
185185
});
186186

187+
test('with an imported hosted zone', () => {
188+
const stack = new Stack();
189+
190+
const exampleCom = route53.PublicHostedZone.fromHostedZoneId(stack, 'ExampleCom', 'sampleid');
191+
192+
new Certificate(stack, 'Certificate', {
193+
domainName: 'test.example.com',
194+
validation: CertificateValidation.fromDns(exampleCom),
195+
});
196+
197+
Template.fromStack(stack).hasResourceProperties('AWS::CertificateManager::Certificate', {
198+
DomainName: 'test.example.com',
199+
DomainValidationOptions: [
200+
{
201+
DomainName: 'test.example.com',
202+
HostedZoneId: 'sampleid',
203+
},
204+
],
205+
ValidationMethod: 'DNS',
206+
});
207+
});
208+
187209
test('with hosted zone and a wildcard name', () => {
188210
const stack = new Stack();
189211

packages/@aws-cdk/aws-elasticloadbalancingv2/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"@aws-cdk/aws-ec2": "0.0.0",
9393
"@aws-cdk/aws-iam": "0.0.0",
9494
"@aws-cdk/aws-lambda": "0.0.0",
95+
"@aws-cdk/aws-route53": "0.0.0",
9596
"@aws-cdk/aws-s3": "0.0.0",
9697
"@aws-cdk/cloud-assembly-schema": "0.0.0",
9798
"@aws-cdk/core": "0.0.0",
@@ -107,6 +108,7 @@
107108
"@aws-cdk/aws-iam": "0.0.0",
108109
"@aws-cdk/aws-lambda": "0.0.0",
109110
"@aws-cdk/aws-s3": "0.0.0",
111+
"@aws-cdk/aws-route53": "0.0.0",
110112
"@aws-cdk/cloud-assembly-schema": "0.0.0",
111113
"@aws-cdk/core": "0.0.0",
112114
"@aws-cdk/cx-api": "0.0.0",

packages/@aws-cdk/aws-elasticloadbalancingv2/test/nlb/load-balancer.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Match, Template } from '@aws-cdk/assertions';
22
import * as ec2 from '@aws-cdk/aws-ec2';
3+
import * as route53 from '@aws-cdk/aws-route53';
34
import * as s3 from '@aws-cdk/aws-s3';
45
import { testFutureBehavior } from '@aws-cdk/cdk-build-tools/lib/feature-flag';
56
import * as cdk from '@aws-cdk/core';
@@ -50,6 +51,30 @@ describe('tests', () => {
5051
});
5152
});
5253

54+
test('VpcEndpointService with Domain Name imported from public hosted zone', () => {
55+
// GIVEN
56+
const stack = new cdk.Stack();
57+
const vpc = new ec2.Vpc(stack, 'Vpc');
58+
const nlb = new elbv2.NetworkLoadBalancer(stack, 'Nlb', { vpc });
59+
const endpointService = new ec2.VpcEndpointService(stack, 'EndpointService', { vpcEndpointServiceLoadBalancers: [nlb] });
60+
61+
// WHEN
62+
const importedPHZ = route53.PublicHostedZone.fromHostedZoneAttributes(stack, 'MyPHZ', {
63+
hostedZoneId: 'sampleid',
64+
zoneName: 'MyZone',
65+
});
66+
new route53.VpcEndpointServiceDomainName(stack, 'EndpointServiceDomainName', {
67+
endpointService,
68+
domainName: 'MyDomain',
69+
publicHostedZone: importedPHZ,
70+
});
71+
72+
// THEN
73+
Template.fromStack(stack).hasResourceProperties('AWS::Route53::RecordSet', {
74+
HostedZoneId: 'sampleid',
75+
});
76+
});
77+
5378
test('Attributes', () => {
5479
// GIVEN
5580
const stack = new cdk.Stack();

packages/@aws-cdk/aws-route53/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,18 @@ you know the ID and the retrieval for the `zoneName` is undesirable.
210210
const zone = route53.HostedZone.fromHostedZoneId(this, 'MyZone', 'ZOJJZC49E0EPZ');
211211
```
212212

213+
You can import a Public Hosted Zone as well with the similar `PubicHostedZone.fromPublicHostedZoneId` and `PubicHostedZone.fromPublicHostedZoneAttributes` methods:
214+
215+
```ts
216+
const zoneFromAttributes = route53.PublicHostedZone.fromPublicHostedZoneAttributes(this, 'MyZone', {
217+
zoneName: 'example.com',
218+
hostedZoneId: 'ZOJJZC49E0EPZ',
219+
});
220+
221+
// Does not know zoneName
222+
const zoneFromId = route53.PublicHostedZone.fromPublicHostedZoneId(this, 'MyZone', 'ZOJJZC49E0EPZ');
223+
```
224+
213225
## VPC Endpoint Service Private DNS
214226

215227
When you create a VPC endpoint service, AWS generates endpoint-specific DNS hostnames that consumers use to communicate with the service.

packages/@aws-cdk/aws-route53/lib/hosted-zone-ref.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,8 @@ export interface HostedZoneAttributes {
4848
*/
4949
readonly zoneName: string;
5050
}
51+
52+
/**
53+
* Reference to a public hosted zone
54+
*/
55+
export interface PublicHostedZoneAttributes extends HostedZoneAttributes { }

packages/@aws-cdk/aws-route53/lib/hosted-zone.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as cxschema from '@aws-cdk/cloud-assembly-schema';
44
import { ContextProvider, Duration, Lazy, Resource, Stack } from '@aws-cdk/core';
55
import { Construct } from 'constructs';
66
import { HostedZoneProviderProps } from './hosted-zone-provider';
7-
import { HostedZoneAttributes, IHostedZone } from './hosted-zone-ref';
7+
import { HostedZoneAttributes, IHostedZone, PublicHostedZoneAttributes } from './hosted-zone-ref';
88
import { CaaAmazonRecord, ZoneDelegationRecord } from './record-set';
99
import { CfnHostedZone } from './route53.generated';
1010
import { makeHostedZoneArn, validateZoneName } from './util';
@@ -237,6 +237,26 @@ export class PublicHostedZone extends HostedZone implements IPublicHostedZone {
237237
return new Import(scope, id);
238238
}
239239

240+
/**
241+
* Imports a public hosted zone from another stack.
242+
*
243+
* Use when both hosted zone ID and hosted zone name are known.
244+
*
245+
* @param scope the parent Construct for this Construct
246+
* @param id the logical name of this Construct
247+
* @param attrs the PublicHostedZoneAttributes (hosted zone ID and hosted zone name)
248+
*/
249+
public static fromPublicHostedZoneAttributes(scope: Construct, id: string, attrs: PublicHostedZoneAttributes): IHostedZone {
250+
class Import extends Resource implements IHostedZone {
251+
public readonly hostedZoneId = attrs.hostedZoneId;
252+
public readonly zoneName = attrs.zoneName;
253+
public get hostedZoneArn(): string {
254+
return makeHostedZoneArn(this, this.hostedZoneId);
255+
}
256+
}
257+
return new Import(scope, id);
258+
}
259+
240260
/**
241261
* Role for cross account zone delegation
242262
*/

0 commit comments

Comments
 (0)