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+ import software .amazon .awssdk .core .internal .waiters .ResponseOrException ;
8+ import software .amazon .awssdk .services .cloudfront .CloudFrontClient ;
9+ import software .amazon .awssdk .services .cloudfront .model .CreateDistributionResponse ;
10+ import software .amazon .awssdk .services .cloudfront .model .Distribution ;
11+ import software .amazon .awssdk .services .cloudfront .model .GetDistributionResponse ;
12+ import software .amazon .awssdk .services .cloudfront .model .ItemSelection ;
13+ import software .amazon .awssdk .services .cloudfront .model .Method ;
14+ import software .amazon .awssdk .services .cloudfront .model .ViewerProtocolPolicy ;
15+ import software .amazon .awssdk .services .cloudfront .waiters .CloudFrontWaiter ;
16+ import software .amazon .awssdk .services .s3 .S3Client ;
17+
18+ import java .time .Instant ;
19+ // snippet-end:[cloudfront.java2.createdistributiontenant.import]
20+
21+ // snippet-start:[cloudfront.java2.createdistributiontenant.title]
22+ public class CreateMultiTenantDistribution {
23+ // snippet-end:[cloudfront.java2.createdistributiontenant.title]
24+ // snippet-start:[cloudfront.java2.createdistributiontenant.nocert]
25+ public static DistributionTenant createDistributionTenantNoCert (CloudFrontClient cloudFrontClient ,
26+ Route53Client route53Client ,
27+ String distributionId ,
28+ String domain ,
29+ String hostedZoneId ) {
30+ CreateDistributionTenantResponse createResponse = cloudFrontClient .createDistributionTenant (builder -> builder
31+ .distributionId (distributionId )
32+ .domains (b1 -> b1
33+ .domain (domain ))
34+ .enabled (true )
35+ .name ("no-cert-tenant" )
36+ );
37+
38+ final DistributionTenant distributionTenant = createResponse .distributionTenant ();
39+
40+ final GetConnectionGroupResponse fetchedConnectionGroup = cloudFrontClient .getConnectionGroup (builder -> builder
41+ .identifier (distributionTenant .connectionGroupId ()));
42+
43+ route53Client .changeResourceRecordSets (builder -> builder
44+ .hostedZoneId (hostedZoneId )
45+ .changeBatch (b1 -> b1
46+ .comment ("ChangeBatch comment" )
47+ .changes (b2 -> b2
48+ .resourceRecordSet (b3 -> b3
49+ .name (domain )
50+ .type ("CNAME" )
51+ .ttl (300L )
52+ .resourceRecords (b4 -> b4
53+ .value (fetchedConnectionGroup .connectionGroup ().routingEndpoint ())))
54+ .action ("CREATE" ))
55+ ));
56+ return distributionTenant ;
57+ }
58+ // snippet-end:[cloudfront.java2.createdistributiontenant.nocert]
59+
60+ // snippet-start:[cloudfront.java2.createdistributiontenant.withcert]
61+ public static DistributionTenant createDistributionTenantWithCert (CloudFrontClient cloudFrontClient ,
62+ Route53Client route53Client ,
63+ String distributionId ,
64+ String domain ,
65+ String hostedZoneId ) {
66+ CreateDistributionTenantResponse createResponse = cloudFrontClient .createDistributionTenant (builder -> builder
67+ .distributionId (distributionId )
68+ .domains (b1 -> b1
69+ .domain (domain ))
70+ .enabled (true )
71+ .name ("no-cert-tenant" )
72+ );
73+
74+ final DistributionTenant distributionTenant = createResponse .distributionTenant ();
75+
76+ final GetConnectionGroupResponse fetchedConnectionGroup = cloudFrontClient .getConnectionGroup (builder -> builder
77+ .identifier (distributionTenant .connectionGroupId ()));
78+
79+ route53Client .changeResourceRecordSets (builder -> builder
80+ .hostedZoneId (hostedZoneId )
81+ .changeBatch (b1 -> b1
82+ .comment ("ChangeBatch comment" )
83+ .changes (b2 -> b2
84+ .resourceRecordSet (b3 -> b3
85+ .name (domain )
86+ .type ("CNAME" )
87+ .ttl (300L )
88+ .resourceRecords (b4 -> b4
89+ .value (fetchedConnectionGroup .connectionGroup ().routingEndpoint ())))
90+ .action ("CREATE" ))
91+ ));
92+ return distributionTenant ;
93+ }
94+ // snippet-end:[cloudfront.java2.createdistributiontenant.withcert]
95+
96+ // snippet-start:[cloudfront.java2.createdistributiontenant.cfhosted]
97+ public static DistributionTenant createDistributionTenantCfHosted (CloudFrontClient cloudFrontClient ,
98+ Route53Client route53Client ,
99+ String distributionId ,
100+ String domain ,
101+ String hostedZoneId ) {
102+ CreateDistributionTenantResponse createResponse = cloudFrontClient .createDistributionTenant (builder -> builder
103+ .distributionId (distributionId )
104+ .domains (b1 -> b1
105+ .domain (domain ))
106+ .enabled (true )
107+ .name ("no-cert-tenant" )
108+ );
109+
110+ final DistributionTenant distributionTenant = createResponse .distributionTenant ();
111+
112+ final GetConnectionGroupResponse fetchedConnectionGroup = cloudFrontClient .getConnectionGroup (builder -> builder
113+ .identifier (distributionTenant .connectionGroupId ()));
114+
115+ route53Client .changeResourceRecordSets (builder -> builder
116+ .hostedZoneId (hostedZoneId )
117+ .changeBatch (b1 -> b1
118+ .comment ("ChangeBatch comment" )
119+ .changes (b2 -> b2
120+ .resourceRecordSet (b3 -> b3
121+ .name (domain )
122+ .type ("CNAME" )
123+ .ttl (300L )
124+ .resourceRecords (b4 -> b4
125+ .value (fetchedConnectionGroup .connectionGroup ().routingEndpoint ())))
126+ .action ("CREATE" ))
127+ ));
128+ return distributionTenant ;
129+ }
130+ // snippet-end:[cloudfront.java2.createdistributiontenant.cfhosted]
131+
132+ // snippet-start:[cloudfront.java2.createdistributiontenant.selfhosted]
133+ public static DistributionTenant createDistributionTenantSelfHosted (CloudFrontClient cloudFrontClient ,
134+ Route53Client route53Client ,
135+ String distributionId ,
136+ String domain ,
137+ String hostedZoneId ) {
138+ CreateDistributionTenantResponse createResponse = cloudFrontClient .createDistributionTenant (builder -> builder
139+ .distributionId (distributionId )
140+ .domains (b1 -> b1
141+ .domain (domain ))
142+ .enabled (true )
143+ .name ("no-cert-tenant" )
144+ );
145+
146+ final DistributionTenant distributionTenant = createResponse .distributionTenant ();
147+
148+ final GetConnectionGroupResponse fetchedConnectionGroup = cloudFrontClient .getConnectionGroup (builder -> builder
149+ .identifier (distributionTenant .connectionGroupId ()));
150+
151+ route53Client .changeResourceRecordSets (builder -> builder
152+ .hostedZoneId (hostedZoneId )
153+ .changeBatch (b1 -> b1
154+ .comment ("ChangeBatch comment" )
155+ .changes (b2 -> b2
156+ .resourceRecordSet (b3 -> b3
157+ .name (domain )
158+ .type ("CNAME" )
159+ .ttl (300L )
160+ .resourceRecords (b4 -> b4
161+ .value (fetchedConnectionGroup .connectionGroup ().routingEndpoint ())))
162+ .action ("CREATE" ))
163+ ));
164+ return distributionTenant ;
165+ }
166+ // snippet-end:[cloudfront.java2.createdistributiontenant.selfhosted]
167+
168+ // snippet-start:[cloudfront.java2.createdistributiontenant.closebrace]
169+ }
170+ // snippet-end:[cloudfront.java2.createdistributiontenant.closebrace]
0 commit comments