in src/AWS.Deploy.Recipes/CdkTemplates/BlazorWasm/Generated/Recipe.cs [70:204]
private void ConfigureCloudFrontDistribution(Configuration settings)
{
if (ContentS3Bucket == null)
throw new InvalidOperationException($"{nameof(ContentS3Bucket)} has not been set. The {nameof(ConfigureS3ContentBucket)} method should be called before {nameof(ConfigureCloudFrontDistribution)}");
var distributionProps = new DistributionProps
{
DefaultBehavior = new BehaviorOptions
{
Origin = new S3Origin(ContentS3Bucket, new S3OriginProps())
},
DefaultRootObject = settings.IndexDocument,
EnableIpv6 = settings.EnableIpv6,
HttpVersion = settings.MaxHttpVersion,
PriceClass = settings.PriceClass
};
var errorResponses = new List<ErrorResponse>();
if (!string.IsNullOrEmpty(settings.ErrorDocument))
{
errorResponses.Add(
new ErrorResponse
{
ResponsePagePath = settings.ErrorDocument
}
);
}
if (settings.Redirect404ToRoot)
{
errorResponses.Add(
new ErrorResponse
{
HttpStatus = 404,
ResponseHttpStatus = 200,
ResponsePagePath = "/"
}
);
// Since S3 returns back an access denied for objects that don't exist to CloudFront treat 403 as 404 not found.
errorResponses.Add(
new ErrorResponse
{
HttpStatus = 403,
ResponseHttpStatus = 200,
ResponsePagePath = "/"
}
);
}
if (errorResponses.Any())
{
distributionProps.ErrorResponses = errorResponses.ToArray();
}
if(settings.AccessLogging?.Enable == true)
{
distributionProps.EnableLogging = true;
if(settings.AccessLogging.CreateLoggingS3Bucket)
{
var loggingBucket = new Bucket(this, nameof(AccessLoggingBucket), InvokeCustomizeCDKPropsEvent(nameof(AccessLoggingBucket), this, new BucketProps
{
RemovalPolicy = RemovalPolicy.RETAIN,
AccessControl = BucketAccessControl.LOG_DELIVERY_WRITE
}));
distributionProps.LogBucket = loggingBucket;
new CfnOutput(this, "S3AccessLoggingBucket", new CfnOutputProps
{
Description = "S3 bucket storing access logs. Bucket and logs will be retained after deployment is deleted.",
Value = distributionProps.LogBucket.BucketName
});
}
else if(!string.IsNullOrEmpty(settings.AccessLogging.ExistingS3LoggingBucket))
{
distributionProps.LogBucket = Bucket.FromBucketName(this, nameof(AccessLoggingBucket), settings.AccessLogging.ExistingS3LoggingBucket);
}
if(!string.IsNullOrEmpty(settings.AccessLogging.LoggingS3KeyPrefix))
{
distributionProps.LogFilePrefix = settings.AccessLogging.LoggingS3KeyPrefix;
}
distributionProps.LogIncludesCookies = settings.AccessLogging.LogIncludesCookies;
}
if(!string.IsNullOrEmpty(settings.WebAclId))
{
distributionProps.WebAclId = settings.WebAclId;
}
CloudFrontDistribution = new Distribution(this, nameof(CloudFrontDistribution), InvokeCustomizeCDKPropsEvent(nameof(CloudFrontDistribution), this, distributionProps));
if (settings.BackendApi?.Enable == true)
{
var backendApiUri = new Uri(settings.BackendApi.Uri);
var httpOriginProps = new HttpOriginProps
{
OriginPath = backendApiUri.PathAndQuery
};
if (string.Equals("https", backendApiUri.Scheme, StringComparison.OrdinalIgnoreCase))
{
httpOriginProps.ProtocolPolicy = OriginProtocolPolicy.HTTPS_ONLY;
httpOriginProps.HttpsPort = backendApiUri.Port;
}
else
{
httpOriginProps.ProtocolPolicy = OriginProtocolPolicy.HTTP_ONLY;
httpOriginProps.HttpPort = backendApiUri.Port;
}
var httpOrigin = new HttpOrigin(backendApiUri.Host, InvokeCustomizeCDKPropsEvent(nameof(BackendRestApiHttpOrigin), this, httpOriginProps));
// Since this is a backend API where the business logic for the Blazor app caching must be disabled.
var addBehavorOptions = new AddBehaviorOptions
{
AllowedMethods = AllowedMethods.ALLOW_ALL,
CachePolicy = CachePolicy.CACHING_DISABLED
};
CloudFrontDistribution.AddBehavior(settings.BackendApi.ResourcePathPattern, httpOrigin, InvokeCustomizeCDKPropsEvent(nameof(BackendRestApiCacheBehavior), this, addBehavorOptions));
}
new CfnOutput(this, "EndpointURL", new CfnOutputProps
{
Description = "Endpoint to access application",
Value = $"https://{CloudFrontDistribution.DomainName}/"
});
}