Search code examples
amazon-cloudfrontaws-cdk

AWS CDK: There can only be one default behavior across all sources


Background

I have a piece of cdk code that runs in a function. Each time through it creates a cloud front distribution. I want one instance to have a different value of behavior. This seems like a very simple thing, but I always get the error below that I do not understand.

Code

      // 1. Default:
        let behavior: cloudfront.Behavior = {isDefaultBehavior: true};

        // Lambda Edge / Cloudfront Function Authentication... 
        if (subDomain == "monkey-ops") {

            // 2. Cloudfront Function.
            const cfFunct = new cloudfront.Function(this, 'id', {
                functionName: 'http-auth-ops',
                comment: 'http-auth for monkey-ops.monkeytronics.co.nz static site',
                code: cloudfront.FunctionCode.fromFile({filePath: __dirname + '\\http-auth-ops-cf.js'})
            });

            behavior = {
                isDefaultBehavior: false,
                functionAssociations: [{
                    eventType: cloudfront.FunctionEventType.VIEWER_REQUEST,
                    function: cfFunct
                }] 
            };
            
            // behavior = {isDefaultBehavior: true};
        } else {
            behavior = {isDefaultBehavior: true};
        }
        

        let cloudFrontDistribution = new cloudfront.CloudFrontWebDistribution(this, subDomain + 'Distribution', {
            originConfigs: [
                {
                    customOriginSource: {
                        domainName: s3Bucket.bucketWebsiteDomainName,
                        originProtocolPolicy: cloudfront.OriginProtocolPolicy.HTTP_ONLY,
                    },          
                    // behaviors : [ {isDefaultBehavior: true} ],
                    behaviors : [ behavior ],
                }
            ],
            viewerCertificate: cloudfront.ViewerCertificate.fromAcmCertificate(
                tslCert,
                {
                    aliases: [ subDomain + '.monkeytronics.co.nz' ],
                    // securityPolicy: cloudfront.SecurityPolicyProtocol.SSL_V3, // default
                    securityPolicy: cloudfront.SecurityPolicyProtocol.TLS_V1_2_2021,
                    sslMethod: cloudfront.SSLMethod.SNI, // default
                },
            ),
        });

Error

Gives the following error, which I can't unpick...

    Error: There can only be one default behavior across all sources. [ One default behavior per distribution ].
        at new CloudFrontWebDistribution (D:\MonkeySource\2-Cloud\cdk_stacks\node_modules\aws-cdk-lib\aws-cloudfront\lib\web-distribution.js:1:6631)
        at new StaticSite (D:\MonkeySource\2-Cloud\cdk_stacks\lib\factory\static-site\static-site-factory.ts:120:42)
        at new SnWebStack (D:\MonkeySource\2-Cloud\cdk_stacks\lib\sn-web-stack.ts:57:7)
        at Object.<anonymous> (D:\MonkeySource\2-Cloud\cdk_stacks\bin\cdk.ts:19:1)
        at Module._compile (node:internal/modules/cjs/loader:1101:14)
        at Module.m._compile (D:\MonkeySource\2-Cloud\cdk_stacks\node_modules\ts-node\src\index.ts:1056:23)
        at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
        at Object.require.extensions.<computed> [as .ts] (D:\MonkeySource\2-Cloud\cdk_stacks\node_modules\ts-node\src\index.ts:1059:12)
        at Module.load (node:internal/modules/cjs/loader:981:32)
        at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    
    Subprocess exited with error 1

Solution

  • The error message is misleading. You are getting it because you have no default behavior configured.

    {
        isDefaultBehavior: true, // <-- This needs to be true for one behavior
        functionAssociations: [{
            eventType: cloudfront.FunctionEventType.VIEWER_REQUEST,
            function: cfFunct
        }] 
    }