Skip to content

Commit 44092a2

Browse files
committed
Adding basic SaaS code samples
1 parent 9ed0108 commit 44092a2

File tree

4 files changed

+323
-1
lines changed

4 files changed

+323
-1
lines changed

.doc_gen/metadata/cloudfront_metadata.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,56 @@ cloudfront_CreateDistribution:
8080
- cloudfront.java2.createdistribution.main
8181
services:
8282
cloudfront: {CreateDistribution}
83+
cloudfront_CreateSaasResources:
84+
title: Create SaaS manager resources &AWS; SDK
85+
title_abbrev: Create a multi-tenant distribution and distribution tenant
86+
synopsis: Create a multi-tenant distribution and distribution tenant with various configurations.
87+
category: Scenarios
88+
languages:
89+
Java:
90+
versions:
91+
- sdk_version: 2
92+
github: javav2/example_code/cloudfront
93+
sdkguide:
94+
excerpts:
95+
- description: >-
96+
The following example demonstrates how to create a multi-tenant distribution with parameters and wildcard certificate
97+
snippet_tags:
98+
- cloudfront.java2.createmultitenantdistribution.import
99+
- cloudfront.java2.createmultitenantdistribution.main
100+
- description: >-
101+
The following example demonstrates how to create a distribution tenant associated with that template, including utilizing the parameter we declared above. Note that we don't need to add certificate info here because our domain is already covered by the parent template.
102+
snippet_tags:
103+
- cloudfront.java2.createdistributiontenant.import
104+
- cloudfront.java2.createdistributiontenant.title
105+
- cloudfront.java2.createdistributiontenant.nocert
106+
- cloudfront.java2.createdistributiontenant.closebrace
107+
- description: >-
108+
<para>If the viewer certificate was omitted from the parent template, you would need to add certificate info on the tenant(s) associated with it instead.</para>
109+
The following example demonstrates how to do so via an ACM certificate arn that covers the necessary domain for the tenant.
110+
snippet_tags:
111+
- cloudfront.java2.createdistributiontenant.import
112+
- cloudfront.java2.createdistributiontenant.title
113+
- cloudfront.java2.createdistributiontenant.withcert
114+
- cloudfront.java2.createdistributiontenant.closebrace
115+
- description: >-
116+
<para>The following example demonstrates how to do so with a CloudFront-hosted managed certificate request. This is ideal if you don't already have traffic towards your domain.</para>
117+
In this case, we create a ConnectionGroup to generate a RoutingEndpoint. Then we use that RoutingEndpoint to create DNS records which verify domain ownership and point to CloudFront. CloudFront will then automatically serve a token to validate domain ownership and create a managed certificate.
118+
snippet_tags:
119+
- cloudfront.java2.createdistributiontenant.import
120+
- cloudfront.java2.createdistributiontenant.title
121+
- cloudfront.java2.createdistributiontenant.cfhosted
122+
- cloudfront.java2.createdistributiontenant.closebrace
123+
- description: >-
124+
<para>The following example demonstrates how to do so with a self-hosted managed certificate request. This is ideal if you have traffic towards your domain and can't tolerate downtime during a migration.</para>
125+
At the end of this example, the Tenant will be created in a state awaiting domain validation and DNS setup. Follow steps [here](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/managed-cloudfront-certificates.html#complete-domain-ownership) to complete setup when you are ready to migrate traffic.
126+
snippet_tags:
127+
- cloudfront.java2.createdistributiontenant.import
128+
- cloudfront.java2.createdistributiontenant.title
129+
- cloudfront.java2.createdistributiontenant.selfhosted
130+
- cloudfront.java2.createdistributiontenant.closebrace
131+
services:
132+
cloudfront: {CreateDistribution, CreateDistributionTenant}
83133
cloudfront_CreateKeyGroup:
84134
languages:
85135
Java:

javav2/example_code/cloudfront/README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Code excerpts that show you how to call individual service functions.
4545
Code examples that show you how to accomplish a specific task by calling multiple
4646
functions within the same service.
4747

48+
- [Create a multi-tenant distribution and distribution tenant](src/main/java/com/example/cloudfront/CreateMultiTenantDistribution.java)
4849
- [Delete signing resources](src/main/java/com/example/cloudfront/DeleteSigningResources.java)
4950
- [Sign URLs and cookies](src/main/java/com/example/cloudfront/CreateCannedPolicyRequest.java)
5051

@@ -62,6 +63,18 @@ functions within the same service.
6263

6364

6465

66+
#### Create a multi-tenant distribution and distribution tenant
67+
68+
This example shows you how to Create a multi-tenant distribution and distribution tenant with various configurations.
69+
70+
71+
<!--custom.scenario_prereqs.cloudfront_CreateSaasResources.start-->
72+
<!--custom.scenario_prereqs.cloudfront_CreateSaasResources.end-->
73+
74+
75+
<!--custom.scenarios.cloudfront_CreateSaasResources.start-->
76+
<!--custom.scenarios.cloudfront_CreateSaasResources.end-->
77+
6578
#### Delete signing resources
6679

6780
This example shows you how to delete resources that are used to gain access to restricted content in an Amazon Simple Storage Service (Amazon S3) bucket.
@@ -112,4 +125,4 @@ in the `javav2` folder.
112125

113126
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
114127

115-
SPDX-License-Identifier: Apache-2.0
128+
SPDX-License-Identifier: Apache-2.0
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
.parameters(b2 -> b2
35+
.name("tenantName")
36+
.value("myTenant"))
37+
.enabled(true)
38+
.name("no-cert-tenant")
39+
);
40+
41+
final DistributionTenant distributionTenant = createResponse.distributionTenant();
42+
43+
// Then update the Route53 hosted zone to point your domain at the distribution tenant
44+
// We fetch the RoutingEndpoint to point to via the default connection group that was created for your tenant
45+
final GetConnectionGroupResponse fetchedConnectionGroup = cloudFrontClient.getConnectionGroup(builder -> builder
46+
.identifier(distributionTenant.connectionGroupId()));
47+
48+
route53Client.changeResourceRecordSets(builder -> builder
49+
.hostedZoneId(hostedZoneId)
50+
.changeBatch(b1 -> b1
51+
.comment("ChangeBatch comment")
52+
.changes(b2 -> b2
53+
.resourceRecordSet(b3 -> b3
54+
.name(domain)
55+
.type("CNAME")
56+
.ttl(300L)
57+
.resourceRecords(b4 -> b4
58+
.value(fetchedConnectionGroup.connectionGroup().routingEndpoint())))
59+
.action("CREATE"))
60+
));
61+
return distributionTenant;
62+
}
63+
// snippet-end:[cloudfront.java2.createdistributiontenant.nocert]
64+
65+
// snippet-start:[cloudfront.java2.createdistributiontenant.withcert]
66+
public static DistributionTenant createDistributionTenantWithCert(CloudFrontClient cloudFrontClient,
67+
Route53Client route53Client,
68+
String distributionId,
69+
String domain,
70+
String hostedZoneId,
71+
String certificateArn) {
72+
CreateDistributionTenantResponse createResponse = cloudFrontClient.createDistributionTenant(builder -> builder
73+
.distributionId(distributionId)
74+
.domains(b1 -> b1
75+
.domain(domain))
76+
.enabled(true)
77+
.name("tenant-with-cert")
78+
.parameters(b2 -> b2
79+
.name("tenantName")
80+
.value("myTenant"))
81+
.customizations(b3 -> b3
82+
.certificate(b4 -> b4
83+
.arn(certificateArn))) // NOTE: Cert must be in Us-East-1 and cover the domain provided in this request
84+
85+
);
86+
87+
final DistributionTenant distributionTenant = createResponse.distributionTenant();
88+
89+
// Then update the Route53 hosted zone to point your domain at the distribution tenant
90+
// We fetch the RoutingEndpoint to point to via the default connection group that was created for your tenant
91+
final GetConnectionGroupResponse fetchedConnectionGroup = cloudFrontClient.getConnectionGroup(builder -> builder
92+
.identifier(distributionTenant.connectionGroupId()));
93+
94+
route53Client.changeResourceRecordSets(builder -> builder
95+
.hostedZoneId(hostedZoneId)
96+
.changeBatch(b1 -> b1
97+
.comment("ChangeBatch comment")
98+
.changes(b2 -> b2
99+
.resourceRecordSet(b3 -> b3
100+
.name(domain)
101+
.type("CNAME")
102+
.ttl(300L)
103+
.resourceRecords(b4 -> b4
104+
.value(fetchedConnectionGroup.connectionGroup().routingEndpoint())))
105+
.action("CREATE"))
106+
));
107+
return distributionTenant;
108+
}
109+
// snippet-end:[cloudfront.java2.createdistributiontenant.withcert]
110+
111+
// snippet-start:[cloudfront.java2.createdistributiontenant.cfhosted]
112+
public static DistributionTenant createDistributionTenantCfHosted(CloudFrontClient cloudFrontClient,
113+
Route53Client route53Client,
114+
String distributionId,
115+
String domain,
116+
String hostedZoneId) {
117+
CreateConnectionGroupResponse createConnectionGroupResponse = cloudFrontClient.createConnectionGroup(builder -> builder
118+
.ipv6Enabled(true)
119+
.name("cf-hosted-connection-group")
120+
.enabled(true));
121+
122+
route53Client.changeResourceRecordSets(builder -> builder
123+
.hostedZoneId(hostedZoneId)
124+
.changeBatch(b1 -> b1
125+
.comment("cf-hosted domain validation record")
126+
.changes(b2 -> b2
127+
.resourceRecordSet(b3 -> b3
128+
.name(domain)
129+
.type(RRType.CNAME)
130+
.ttl(300L)
131+
.resourceRecords(b4 -> b4
132+
.value(createConnectionGroupResponse.connectionGroup().routingEndpoint())))
133+
.action("CREATE"))
134+
));
135+
136+
// Give the R53 record time to propagate, if it isn't being returned by servers yet, the following call will fail
137+
sleep(60000);
138+
139+
CreateDistributionTenantResponse createResponse = cloudFrontClient.createDistributionTenant(builder -> builder
140+
.distributionId(distributionId)
141+
.domains(b1 -> b1
142+
.domain(domain))
143+
.enabled(true)
144+
.name("cf-hosted-tenant")
145+
.parameters(b2 -> b2
146+
.name("tenantName")
147+
.value("myTenant"))
148+
.managedCertificateRequest(b3 -> b3
149+
.validationTokenHost(ValidationTokenHost.SELF_HOSTED)
150+
);
151+
152+
final DistributionTenant distributionTenant = createResponse.distributionTenant();
153+
}
154+
// snippet-end:[cloudfront.java2.createdistributiontenant.cfhosted]
155+
156+
// snippet-start:[cloudfront.java2.createdistributiontenant.selfhosted]
157+
public static DistributionTenant createDistributionTenantSelfHosted(CloudFrontClient cloudFrontClient,
158+
Route53Client route53Client,
159+
String distributionId,
160+
String domain,
161+
String hostedZoneId) {
162+
CreateDistributionTenantResponse createResponse = cloudFrontClient.createDistributionTenant(builder -> builder
163+
.distributionId(distributionId)
164+
.domains(b1 -> b1
165+
.domain(domain))
166+
.parameters(b2 -> b2
167+
.name("tenantName")
168+
.value("myTenant"))
169+
.enabled(true)
170+
.name("self-hosted-tenant")
171+
);
172+
173+
return createResponse.distributionTenant();
174+
}
175+
// snippet-end:[cloudfront.java2.createdistributiontenant.selfhosted]
176+
177+
// snippet-start:[cloudfront.java2.createdistributiontenant.closebrace]
178+
}
179+
// snippet-end:[cloudfront.java2.createdistributiontenant.closebrace]

0 commit comments

Comments
 (0)