aws / aws/aws-cdk

cloudfront: CachePolicy is not supported when deployed in China

Open
#31,033 6 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-cloudfront bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

AWS China does not support CachePolicy, but a default value is assigned in the cdk, causing the deployment to fail. #13584

I use the way I override `cachePolicyId=underfined`, which works for `defaultCacheBehavior`, but because I need to use multiple Behaviors. `cacheBehaviors` return is a LazyAny type and can't be overwritten, which makes it impossible to circumvent aws restrictions. I hope you can fix this bug, thank you very much.

### Expected Behavior

The new CloudFront distribution can be deployed to AWS China regions.

### Current Behavior

> 5:38:40 PM | UPDATE_FAILED | AWS::CloudFront::Distribution | cloudfrontcloudfrontdistributionAC735BF7
Resource handler returned message: "Invalid request provided: The parameter CachePolicyId can't be set for this region. (Service: CloudFront, Status Code: 400, Request ID: 654c85b7-0854-446d-b7f2-1a1e49452bff)" (RequestToken:
a2525e84-fff0-fa4b-bfe9-db7e90db4eeb, HandlerErrorCode: InvalidRequest)
>
> ❌ sandbox-cn-north-1 failed: Error: The stack named sandbox-cn-north-1 failed to deploy: UPDATE_ROLLBACK_COMPLETE: Resource handler returned message: "Invalid request provided: The parameter CachePolicyId can't be set for this region. (Service: CloudFront, Status Code: 400, Request ID: 654c85b7-0854-446d-b7f2-1a1e49452bff)" (RequestToken: a2525e84-fff0-fa4b-bfe9-db7e90db4eeb, HandlerErrorCode: InvalidRequest)
at FullCloudFormationDeployment.monitorDeployment (/usr/local/lib/node_modules/aws-cdk/lib/index.js:431:10615)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Object.deployStack2 [as deployStack] (/usr/local/lib/node_modules/aws-cdk/lib/index.js:434:196750)
at async /usr/local/lib/node_modules/aws-cdk/lib/index.js:434:178719

### Reproduction Steps

```typescript
this.distribution = new Distribution(this, `cloudfront-distribution`, {
domainNames: [rootDomain],
defaultBehavior: {
origin: new S3Origin(bucket, { originPath }),
allowedMethods: AllowedMethods.ALLOW_GET_HEAD,
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
cachedMethods: CachedMethods.CACHE_GET_HEAD,
responseHeadersPolicy,
},
additionalBehaviors: {
"/test/*": {
origin: new S3Origin(bucket),
allowedMethods: AllowedMethods.ALLOW_GET_HEAD,
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
cachedMethods: CachedMethods.CACHE_GET_HEAD,
responseHeadersPolicy,
},
},
defaultRootObject: "index.html",
httpVersion: HttpVersion.HTTP2,
enableIpv6: false,
enableLogging: true,
});
```

### Possible Solution

_No response_

### Additional Information/Context

Because the cacheBehaviors type is LazyAny, it is not rewrite successfully, resulting in the same error

```typescript
const cfn = this.distribution.node.findChild("Resource") as CfnDistribution;
const distributionConfig = cfn.distributionConfig as CfnDistribution.DistributionConfigProperty;

cfn.distributionConfig = {
...cfn.distributionConfig,
defaultCacheBehavior: {
...distributionConfig.defaultCacheBehavior,
cachePolicyId: undefined,
forwardedValues: { queryString: false },
defaultTtl: cdk.Duration.days(1).toSeconds(),
minTtl: cdk.Duration.hours(1).toSeconds(),
maxTtl: cdk.Duration.days(365).toSeconds(),
},
// cacheBehaviors: Array.isArray(distributionConfig.cacheBehaviors)
// ? distributionConfig.cacheBehaviors.map((behavior) => ({
// ...behavior,
// cachePolicyId: undefined,
// forwardedValues: { queryString: false },
// defaultTtl: cdk.Duration.days(1).toSeconds(),
// minTtl: cdk.Duration.hours(1).toSeconds(),
// maxTtl: cdk.Duration.days(365).toSeconds(),
// })) : distributionConfig.cacheBehaviors, // TODO: cacheBehaviors type is LazyAny
};
```

### CDK CLI Version

2.123.0

### Framework Version

_No response_

### Node.js Version

v18.14.1

### OS

mac

### Language

TypeScript

### Language Version

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start at the CloudFront Distribution construct and its CfnDistribution synthesis path, focusing on defaultBehavior and additionalBehaviors/cacheBehaviors and the LazyAny value. Reproduce the TypeScript example or inspect the synthesized distribution configuration, then verify that a China-region deployment no longer receives CachePolicyId for unsupported behaviors.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.