In case it helps. OAIs are now deprecated in favor of Origin Access Controls (OAC). If you use an OAC, you still have to specify S3OriginConfig but you put an empty string for the OAI value. The following is a valid DistributionConfig for a non-website s3 bucket:
String bucketWebsiteEndpoint = String.format("%s.s3.%s.amazonaws.com", bucketName, bucketRegion.toLowerCase());
List<String> distributionAliases = prepareDistributionAliases(domainName, subDomainName);
// Create CloudFront distribution configuration
DistributionConfig distributionConfig = new DistributionConfig()
.withAliases(new Aliases().withItems(distributionAliases).withQuantity(distributionAliases.size()))
.withCallerReference(String.valueOf(System.currentTimeMillis())) // Unique reference
.withComment("CloudFront distribution for " + domainName)
.withDefaultCacheBehavior(new DefaultCacheBehavior()
.withTargetOriginId(bucketWebsiteEndpoint)
.withViewerProtocolPolicy(ViewerProtocolPolicy.AllowAll)
.withAllowedMethods(
new AllowedMethods()
.withQuantity(2)
.withItems(Method.GET, Method.HEAD)
.withCachedMethods(
new CachedMethods()
.withQuantity(2)
.withItems(Method.GET, Method.HEAD)
)
)
.withForwardedValues(
new ForwardedValues()
.withQueryString(true)
.withCookies(new CookiePreference().withForward("none")))
.withViewerProtocolPolicy(ViewerProtocolPolicy.RedirectToHttps)
.withMinTTL(0L)
.withDefaultTTL(86400L) // 1 day
.withMaxTTL(31536000L) // 1 year
)
.withEnabled(true)
.withPriceClass(PriceClass.PriceClass_All)
.withViewerCertificate(
new ViewerCertificate()
.withACMCertificateArn(certificateArn)
.withMinimumProtocolVersion(MinimumProtocolVersion.TLSv12_2021)
.withSSLSupportMethod(SSLSupportMethod.SniOnly))
.withOrigins(new Origins()
.withItems(
new Origin()
.withId(bucketWebsiteEndpoint)
.withDomainName(bucketWebsiteEndpoint)
.withConnectionAttempts(3)
.withConnectionTimeout(10)
.withS3OriginConfig(new S3OriginConfig().withOriginAccessIdentity(""))
.withOriginAccessControlId(accessControlId)
) // Use the bucket's endpoint
.withQuantity(1));
CreateDistributionRequest createDistributionRequest = new CreateDistributionRequest()
.withDistributionConfig(distributionConfig);
// Create CloudFront distribution
CreateDistributionResult createDistributionResult = cloudFrontClient.createDistribution(createDistributionRequest);
And this is how you would create the OAC:
String bucketWebsiteEndpoint = String.format("%s.s3.%s.amazonaws.com", bucketName, bucketRegion.toLowerCase());
// create origin access control
CreateOriginAccessControlRequest createOriginAccessControlRequest = new CreateOriginAccessControlRequest()
.withOriginAccessControlConfig(
new OriginAccessControlConfig()
.withOriginAccessControlOriginType(OriginAccessControlOriginTypes.S3)
.withName(bucketWebsiteEndpoint)
.withDescription("Origin access control for " + bucketWebsiteEndpoint)
.withSigningBehavior(OriginAccessControlSigningBehaviors.Always)
.withSigningProtocol(OriginAccessControlSigningProtocols.Sigv4)
);
CreateOriginAccessControlResult result = cloudFrontClient.createOriginAccessControl(createOriginAccessControlRequest);