You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs(cloudfront): add migration guide to modern distribution api (#18835)
Add guide for migrating from CloudFrontWebDistribution to Distribution
----
*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Copy file name to clipboardExpand all lines: packages/@aws-cdk/aws-cloudfront/README.md
+198-1
Original file line number
Diff line number
Diff line change
@@ -91,7 +91,7 @@ your domain name, and provide one (or more) domain names from the certificate fo
91
91
92
92
The certificate must be present in the AWS Certificate Manager (ACM) service in the US East (N. Virginia) region; the certificate
93
93
may either be created by ACM, or created elsewhere and imported into ACM. When a certificate is used, the distribution will support HTTPS connections
94
-
from SNI only and a minimum protocol version of TLSv1.2_2021 if the '@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021' feature flag is set, and TLSv1.2_2019 otherwise.
94
+
from SNI only and a minimum protocol version of TLSv1.2_2021 if the `@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021` feature flag is set, and TLSv1.2_2019 otherwise.
95
95
96
96
```ts
97
97
// To use your own domain name in a Distribution, you must associate a certificate
@@ -539,6 +539,203 @@ const distribution = cloudfront.Distribution.fromDistributionAttributes(this, 'I
539
539
});
540
540
```
541
541
542
+
## Migrating from the original CloudFrontWebDistribution to the newer Distribution construct
543
+
544
+
It's possible to migrate a distribution from the original to the modern API.
545
+
The changes necessary are the following:
546
+
547
+
### The Distribution
548
+
549
+
Replace `new CloudFrontWebDistribution` with `new Distribution`. Some
|`originConfigs`|`defaultBehavior`; use `additionalBehaviors` if necessary |
555
+
|`viewerCertificate`|`certificate`; use `domainNames` for aliases |
556
+
|`errorConfigurations`|`errorResponses`|
557
+
|`loggingConfig`|`enableLogging`; configure with `logBucket``logFilePrefix` and `logIncludesCookies`|
558
+
|`viewerProtocolPolicy`| removed; set on each behavior instead. default changed from `REDIRECT_TO_HTTPS` to `ALLOW_ALL`|
559
+
560
+
After switching constructs, you need to maintain the same logical ID for the underlying [CfnDistribution](https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-cloudfront.CfnDistribution.html) if you wish to avoid the deletion and recreation of your distribution.
561
+
To do this, use [escape hatches](https://docs.aws.amazon.com/cdk/v2/guide/cfn_layer.html) to override the logical ID created by the new Distribution construct with the logical ID created by the old construct.
The modern API makes use of the [CloudFront Origins](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront_origins-readme.html) module to easily configure your origin. Replace your origin configuration with the relevant CloudFront Origins class. For example, here's a behavior with an S3 origin:
const distribution =newcloudfront.Distribution(this, 'MyCfWebDistribution', {
604
+
defaultBehavior: {
605
+
origin: neworigins.S3Origin(sourceBucket) // This class automatically creates an Origin Access Identity
606
+
},
607
+
});
608
+
```
609
+
610
+
In the original API all behaviors are defined in the `originConfigs` property. The new API is optimized for a single origin and behavior, so the default behavior and additional behaviors will be defined separately.
const distribution =newcloudfront.Distribution(this, 'MyCfWebDistribution', {
687
+
defaultBehavior: {
688
+
origin: neworigins.S3Origin(sourceBucket),
689
+
},
690
+
domainNames: ['MYALIAS'],
691
+
certificate: certificate,
692
+
});
693
+
```
694
+
695
+
IAM certificates aren't directly supported by the new API, but can be easily configured through [escape hatches](https://docs.aws.amazon.com/cdk/v2/guide/cfn_layer.html)
A number of default settings have changed on the new API when creating a new distribution, behavior, and origin.
736
+
After making the major changes needed for the migration, run `cdk diff` to see what settings have changed.
737
+
If no changes are desired during migration, you will at the least be able to use [escape hatches](https://docs.aws.amazon.com/cdk/v2/guide/cfn_layer.html) to override what the CDK synthesizes, if you can't change the properties directly.
738
+
542
739
## CloudFrontWebDistribution API
543
740
544
741
> The `CloudFrontWebDistribution` construct is the original construct written for working with CloudFront distributions.
0 commit comments