Skip to content

Commit 76d887d

Browse files
committed
Adding basic SaaS code samples
1 parent 9ed0108 commit 76d887d

3 files changed

Lines changed: 298 additions & 0 deletions

File tree

.doc_gen/metadata/cloudfront_metadata.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,54 @@ cloudfront_CreateDistribution:
8080
- cloudfront.java2.createdistribution.main
8181
services:
8282
cloudfront: {CreateDistribution}
83+
cloudfront_CreateSaasResources:
84+
title: Create a SaaS resource stack &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+
The following example demonstrates how to do so with a cloudfront hosted managed certificate request.
117+
snippet_tags:
118+
- cloudfront.java2.createdistributiontenant.import
119+
- cloudfront.java2.createdistributiontenant.title
120+
- cloudfront.java2.createdistributiontenant.cfhosted
121+
- cloudfront.java2.createdistributiontenant.closebrace
122+
- description: >-
123+
The following example demonstrates how to do so with a self-hosted managed certificate request.
124+
snippet_tags:
125+
- cloudfront.java2.createdistributiontenant.import
126+
- cloudfront.java2.createdistributiontenant.title
127+
- cloudfront.java2.createdistributiontenant.selfhosted
128+
- cloudfront.java2.createdistributiontenant.closebrace
129+
services:
130+
cloudfront: {CreateDistribution, CreateDistributionTenant}
83131
cloudfront_CreateKeyGroup:
84132
languages:
85133
Java:
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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]
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.createmultitenantdistribution.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.createmultitenantdistribution.import]
20+
21+
// snippet-start:[cloudfront.java2.createmultitenantdistribution.main]
22+
public class CreateMultiTenantDistribution {
23+
public static Distribution CreateMultiTenantDistribution(CloudFrontClient cloudFrontClient,
24+
S3Client s3Client,
25+
final String bucketName,
26+
final String certificateArn) {
27+
// fetch the origin info if necessary
28+
final String region = s3Client.headBucket(b -> b.bucket(bucketName)).sdkHttpResponse().headers()
29+
.get("x-amz-bucket-region").get(0);
30+
final String originDomain = bucketName + ".s3." + region + ".amazonaws.com";
31+
String originId = originDomain; // Use the originDomain value for the originId.
32+
33+
CreateDistributionResponse createDistResponse = cloudFrontClient.createDistribution(builder -> builder
34+
.distributionConfig(b1 -> b1
35+
.httpVersion(HttpVersion.HTTP2)
36+
.enabled(true)
37+
.comment("Template Distribution with cert built with java")
38+
.connectionMode(ConnectionMode.TENANT_ONLY)
39+
.callerReference(Instant.now().toString())
40+
.viewerCertificate(certBuilder -> certBuilder
41+
.acmCertificateArn(certificateArn)
42+
.sslSupportMethod(SSLSupportMethod.SNI_ONLY))
43+
.origins(b2 -> b2
44+
.quantity(1)
45+
.items(b3 -> b3
46+
.domainName(originDomain)
47+
.id(originId)
48+
.originPath("/{{region}}")
49+
.s3OriginConfig(builder4 -> builder4
50+
.originAccessIdentity(
51+
""))))
52+
.tenantConfig(b5 -> b5
53+
.parameterDefinitions(b6 -> b6
54+
.name("region")
55+
.definition(b7 -> b7
56+
.stringSchema(b8 -> b8
57+
.comment("region value")
58+
.defaultValue("us-west-2")
59+
.required(false)))))
60+
.defaultCacheBehavior(b2 -> b2
61+
.viewerProtocolPolicy(ViewerProtocolPolicy.ALLOW_ALL)
62+
.targetOriginId(originId)
63+
.cachePolicyId("658327ea-f89d-4fab-a63d-7e88639e58f6") // Cache Optimized Policy
64+
.allowedMethods(b4 -> b4
65+
.quantity(2)
66+
.items(Method.HEAD, Method.GET)))
67+
));
68+
69+
final Distribution distribution = createDistResponse.distribution();
70+
try (CloudFrontWaiter cfWaiter = CloudFrontWaiter.builder().client(cloudFrontClient).build()) {
71+
ResponseOrException<GetDistributionResponse> responseOrException = cfWaiter
72+
.waitUntilDistributionDeployed(builder -> builder.id(distribution.id()))
73+
.matched();
74+
responseOrException.response()
75+
.orElseThrow(() -> new RuntimeException("Distribution not created"));
76+
}
77+
return distribution;
78+
}
79+
}
80+
// snippet-end:[cloudfront.java2.createmultitenantdistribution.main]

0 commit comments

Comments
 (0)