Skip to content

Commit

Permalink
fix(cdk): unstable CloudFront function name
Browse files Browse the repository at this point in the history
- The bug that a CloudFront function name fluctuated over deployment to
  deployment is worked around. There is a bug in CDK that it may
  generate different function names at different deployments. Please see
  the following issue for more details,
    - aws/aws-cdk#15523

  As suggested in the above issue, I decided to give a fixed name to my
  CloudFront function. Function names never conflict between development
  and production stacks because `Node.addr` that is different between
  them is appended.

issue codemonger-io#2
  • Loading branch information
kikuomax committed Jun 11, 2022
1 parent e25596b commit 18328fc
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions cdk/lib/contents-distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
aws_cloudfront as cloudfront,
aws_cloudfront_origins as origins,
} from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { Construct, Node } from 'constructs';

import { ContentsBucket } from './contents-bucket';
import { DeploymentStage } from './deployment-stage';
Expand Down Expand Up @@ -48,6 +48,14 @@ export class ContentsDistribution extends Construct {
});

const expandIndexFn = new cloudfront.Function(this, 'ExpandIndexFunction', {
// provides a fixed function name because there is a bug in CDK that may
// generate different function IDs at different deployments and ends up
// with an error updating a function.
// see https://github.com/aws/aws-cdk/issues/15523
//
// note that a function name must be at most 64 characters long,
// and Node.addr fills 42 characters.
functionName: `ExpandIndexFunction${Node.of(this).addr}`,
comment: 'Expands a given URI so that it ends with index.html',
code: cloudfront.FunctionCode.fromFile({
filePath: path.resolve('cloudfront-fn', 'expand-index.js'),
Expand All @@ -68,7 +76,8 @@ export class ContentsDistribution extends Construct {
}],
// only static contents are served so far
cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD,
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
viewerProtocolPolicy:
cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
},
// do not add a slash (/) before the root object name.
defaultRootObject: 'index.html',
Expand Down

0 comments on commit 18328fc

Please sign in to comment.