1+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+ // SPDX-License-Identifier: Apache-2.0
3+ 4+ package com .example .cloudfront ;
5+ 6+ // snippet-start:[cloudfront.java2.createdistributiontenant.import]
7+ 8+ import software .amazon .awssdk .services .cloudfront .CloudFrontClient ;
9+ import software .amazon .awssdk .services .cloudfront .model .CreateConnectionGroupResponse ;
10+ import software .amazon .awssdk .services .cloudfront .model .CreateDistributionTenantResponse ;
11+ import software .amazon .awssdk .services .cloudfront .model .DistributionTenant ;
12+ import software .amazon .awssdk .services .cloudfront .model .GetConnectionGroupResponse ;
13+ import software .amazon .awssdk .services .cloudfront .model .ValidationTokenHost ;
14+ import software .amazon .awssdk .services .route53 .Route53Client ;
15+ import software .amazon .awssdk .services .route53 .model .RRType ;
16+ 17+ import java .time .Instant ;
18+ // snippet-end:[cloudfront.java2.createdistributiontenant.import]
19+ 20+ // snippet-start:[cloudfront.java2.createdistributiontenant.title]
21+ public class CreateDistributionTenant {
22+ // snippet-end:[cloudfront.java2.createdistributiontenant.title]
23+ // snippet-start:[cloudfront.java2.createdistributiontenant.nocert]
24+ public static DistributionTenant createDistributionTenantNoCert (CloudFrontClient cloudFrontClient ,
25+ Route53Client route53Client ,
26+ String distributionId ,
27+ String domain ,
28+ String hostedZoneId ) {
29+ CreateDistributionTenantResponse createResponse = cloudFrontClient .createDistributionTenant (builder -> builder
30+ .distributionId (distributionId )
31+ .domains (b1 -> b1
32+ .domain (domain ))
33+ .parameters (b2 -> b2
34+ .name ("tenantName" )
35+ .value ("myTenant" ))
36+ .enabled (false )
37+ .name ("no-cert-tenant" )
38+ );
39+ 40+ final DistributionTenant distributionTenant = createResponse .distributionTenant ();
41+ 42+ // Then update the Route53 hosted zone to point your domain at the distribution tenant
43+ // We fetch the RoutingEndpoint to point to via the default connection group that was created for your tenant
44+ final GetConnectionGroupResponse fetchedConnectionGroup = cloudFrontClient .getConnectionGroup (builder -> builder
45+ .identifier (distributionTenant .connectionGroupId ()));
46+ 47+ route53Client .changeResourceRecordSets (builder -> builder
48+ .hostedZoneId (hostedZoneId )
49+ .changeBatch (b1 -> b1
50+ .comment ("ChangeBatch comment" )
51+ .changes (b2 -> b2
52+ .resourceRecordSet (b3 -> b3
53+ .name (domain )
54+ .type ("CNAME" )
55+ .ttl (300L )
56+ .resourceRecords (b4 -> b4
57+ .value (fetchedConnectionGroup .connectionGroup ().routingEndpoint ())))
58+ .action ("CREATE" ))
59+ ));
60+ return distributionTenant ;
61+ }
62+ // snippet-end:[cloudfront.java2.createdistributiontenant.nocert]
63+ 64+ // snippet-start:[cloudfront.java2.createdistributiontenant.withcert]
65+ public static DistributionTenant createDistributionTenantWithCert (CloudFrontClient cloudFrontClient ,
66+ Route53Client route53Client ,
67+ String distributionId ,
68+ String domain ,
69+ String hostedZoneId ,
70+ String certificateArn ) {
71+ CreateDistributionTenantResponse createResponse = cloudFrontClient .createDistributionTenant (builder -> builder
72+ .distributionId (distributionId )
73+ .domains (b1 -> b1
74+ .domain (domain ))
75+ .enabled (false )
76+ .name ("tenant-with-cert" )
77+ .parameters (b2 -> b2
78+ .name ("tenantName" )
79+ .value ("myTenant" ))
80+ .customizations (b3 -> b3
81+ .certificate (b4 -> b4
82+ .arn (certificateArn ))) // NOTE: Cert must be in Us-East-1 and cover the domain provided in this request
83+ 84+ );
85+ 86+ final DistributionTenant distributionTenant = createResponse .distributionTenant ();
87+ 88+ // Then update the Route53 hosted zone to point your domain at the distribution tenant
89+ // We fetch the RoutingEndpoint to point to via the default connection group that was created for your tenant
90+ final GetConnectionGroupResponse fetchedConnectionGroup = cloudFrontClient .getConnectionGroup (builder -> builder
91+ .identifier (distributionTenant .connectionGroupId ()));
92+ 93+ route53Client .changeResourceRecordSets (builder -> builder
94+ .hostedZoneId (hostedZoneId )
95+ .changeBatch (b1 -> b1
96+ .comment ("ChangeBatch comment" )
97+ .changes (b2 -> b2
98+ .resourceRecordSet (b3 -> b3
99+ .name (domain )
100+ .type ("CNAME" )
101+ .ttl (300L )
102+ .resourceRecords (b4 -> b4
103+ .value (fetchedConnectionGroup .connectionGroup ().routingEndpoint ())))
104+ .action ("CREATE" ))
105+ ));
106+ return distributionTenant ;
107+ }
108+ // snippet-end:[cloudfront.java2.createdistributiontenant.withcert]
109+ 110+ // snippet-start:[cloudfront.java2.createdistributiontenant.cfhosted]
111+ public static DistributionTenant createDistributionTenantCfHosted (CloudFrontClient cloudFrontClient ,
112+ Route53Client route53Client ,
113+ String distributionId ,
114+ String domain ,
115+ String hostedZoneId ) throws InterruptedException {
116+ CreateConnectionGroupResponse createConnectionGroupResponse = cloudFrontClient .createConnectionGroup (builder -> builder
117+ .ipv6Enabled (true )
118+ .name ("cf-hosted-connection-group" )
119+ .enabled (true ));
120+ 121+ route53Client .changeResourceRecordSets (builder -> builder
122+ .hostedZoneId (hostedZoneId )
123+ .changeBatch (b1 -> b1
124+ .comment ("cf-hosted domain validation record" )
125+ .changes (b2 -> b2
126+ .resourceRecordSet (b3 -> b3
127+ .name (domain )
128+ .type (RRType .CNAME )
129+ .ttl (300L )
130+ .resourceRecords (b4 -> b4
131+ .value (createConnectionGroupResponse .connectionGroup ().routingEndpoint ())))
132+ .action ("CREATE" ))
133+ ));
134+ 135+ // Give the R53 record time to propagate, if it isn't being returned by servers yet, the following call will fail
136+ Thread .sleep (60000 );
137+ 138+ CreateDistributionTenantResponse createResponse = cloudFrontClient .createDistributionTenant (builder -> builder
139+ .distributionId (distributionId )
140+ .domains (b1 -> b1
141+ .domain (domain ))
142+ .connectionGroupId (createConnectionGroupResponse .connectionGroup ().id ())
143+ .enabled (false )
144+ .name ("cf-hosted-tenant" )
145+ .parameters (b2 -> b2
146+ .name ("tenantName" )
147+ .value ("myTenant" ))
148+ .managedCertificateRequest (b3 -> b3
149+ .validationTokenHost (ValidationTokenHost .CLOUDFRONT )
150+ )
151+ );
152+ 153+ return createResponse .distributionTenant ();
154+ }
155+ // snippet-end:[cloudfront.java2.createdistributiontenant.cfhosted]
156+ 157+ // snippet-start:[cloudfront.java2.createdistributiontenant.selfhosted]
158+ public static DistributionTenant createDistributionTenantSelfHosted (CloudFrontClient cloudFrontClient ,
159+ String distributionId ,
160+ String domain ) {
161+ CreateDistributionTenantResponse createResponse = cloudFrontClient .createDistributionTenant (builder -> builder
162+ .distributionId (distributionId )
163+ .domains (b1 -> b1
164+ .domain (domain ))
165+ .parameters (b2 -> b2
166+ .name ("tenantName" )
167+ .value ("myTenant" ))
168+ .enabled (false )
169+ .name ("self-hosted-tenant" )
170+ .managedCertificateRequest (b3 -> b3
171+ .validationTokenHost (ValidationTokenHost .SELF_HOSTED )
172+ .primaryDomainName (domain )
173+ )
174+ );
175+ 176+ return createResponse .distributionTenant ();
177+ }
178+ // snippet-end:[cloudfront.java2.createdistributiontenant.selfhosted]
179+ 180+ // snippet-start:[cloudfront.java2.createdistributiontenant.closebrace]
181+ }
182+ // snippet-end:[cloudfront.java2.createdistributiontenant.closebrace]
0 commit comments