Skip to content

(aws-cdk-lib.aws_cloudfront): CachePolicy TTL value from SSM parameter not resolved correctly #25795

Closed
@enpatrik

Description

@enpatrik

Describe the bug

CloudFront CachePolicy min/max/default TTL settings does not work with tokenized values (like SSM parameter).

Expected Behavior

The CachePolicy settings to be set with the value from the SSM parameter.

Current Behavior

The SSM parameter references are not resolved correctly.

Reproduction Steps

Example1 (only using SSM parameter for defaultTtl):

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as ssm from 'aws-cdk-lib/aws-ssm';
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';

export class BugStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const defaultTtl = cdk.Token.asNumber(ssm.StringParameter.valueForStringParameter(this,'/Default'))

    new cloudfront.CachePolicy(this, 'CachePolicy', {
      defaultTtl: cdk.Duration.seconds(defaultTtl),
    })
  }
}

Result1:

"CachePolicy": {
 "Type": "AWS::CloudFront::CachePolicy",
 "Properties": {
  "CachePolicyConfig": {
   "DefaultTTL": 0, // Should reference SSM parameter
   "MaxTTL": 31536000,
   "MinTTL": 0,
   // ...

Example2 (using SSM parameters for all ttl values):

const minTtl = cdk.Token.asNumber(ssm.StringParameter.valueForStringParameter(this,'/Min'))
const maxTtl = cdk.Token.asNumber(ssm.StringParameter.valueForStringParameter(this,'/Max'))
const defaultTtl = cdk.Token.asNumber(ssm.StringParameter.valueForStringParameter(this,'/Default'))

new cloudfront.CachePolicy(this, 'CachePolicy', {
    minTtl: cdk.Duration.seconds(minTtl),
    maxTtl: cdk.Duration.seconds(maxTtl),
    defaultTtl: cdk.Duration.seconds(defaultTtl),
})

Result:

"CachePolicy": {
 "Type": "AWS::CloudFront::CachePolicy",
 "Properties": {
  "CachePolicyConfig": {
   "DefaultTTL": {
    "Ref": "SsmParameterValueMin...Parameter" // Min
   },
   "MaxTTL": {
    "Ref": "SsmParameterValueMin...Parameter" // Min
   },
   "MinTTL": {
    "Ref": "SsmParameterValueMin...Parameter" // Min
   },
   // ...

Possible Solution

No response

Additional Information/Context

Suspected issue: https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-cloudfront/lib/cache-policy.ts#L143

Looks like Math.max() usage with tokenized number results in "random"(depending on the definition order) SSM parameter being picked.

// props.defaultTtl token will be a negative number, e.g. -1.8881545897088877e+289
const defaultTtl = Math.max((props.defaultTtl ?? Duration.days(1)).toSeconds(), minTtl);
// props.maxTtl token will be a negative number, e.g. -1.8881545897088894e+289
const maxTtl = Math.max((props.maxTtl ?? Duration.days(365)).toSeconds(), defaultTtl); 

CDK CLI Version

2.81.0 (build bd920f2)

Framework Version

No response

Node.js Version

v18.16.0

OS

MacOS

Language

Typescript

Language Version

Typescript 5.0.4

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-cloudfrontRelated to Amazon CloudFrontbugThis issue is a bug.effort/smallSmall work item – less than a day of effortp2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions