Aws-cdk: Support include body for LambdaFunctionAssociation

Created on 31 Mar 2020  路  4Comments  路  Source: aws/aws-cdk

When creating lambda-edge behaviours from AWS console , we can select Include Body for lambda edge association.
But the same configuration can't be done by AWS CDK because Include Body property is not available in LambdaFunctionAssociation class.

https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cloudfront.LambdaFunctionAssociation.html

@aws-cdaws-cloudfront efforsmall feature-request in-progress p1

Most helpful comment

@nitishdhar it's convoluted but the CDK provides a raw override.

Here's how I got it working:

const distribution = new cloudfront.CloudFrontWebDistribution(this, 'WebDistribution', {
  originConfigs: [{
    s3OriginSource: {
      s3BucketSource: bucket
    },
    behaviors: [{
      isDefaultBehavior: true,
      compress: true
    }, {
      pathPattern: 'api/*',
      compress: true,
      allowedMethods: cloudfront.CloudFrontAllowedMethods.ALL,
      minTtl: cdk.Duration.days(0),
      maxTtl: cdk.Duration.days(0),
      defaultTtl: cdk.Duration.days(0),
      forwardedValues: {
        queryString: true,
        cookies: {
          forward: 'all'
        }
      },
      lambdaFunctionAssociations: [{
        eventType: cloudfront.LambdaEdgeEventType.ORIGIN_REQUEST,
        lambdaFunction: apiLambdaVersion
      }]
    }]
  }]
})
// get the CloudFormation template
const distributionCfn = distribution.node.defaultChild as cloudfront.CfnDistribution
// Add IncludeBody on the first cache behavior - indexed at 0 because the default behaviour is not on the CacheBehaviors array 
distributionCfn.addOverride('Properties.DistributionConfig.CacheBehaviors.0.LambdaFunctionAssociations.0.IncludeBody', true)

distributionCfn above is the CloudFormation template which would look something like this:

Type: AWS::CloudFront::Distribution
Properties: 
  DistributionConfig:
    DefaultCacheBehavior:
      ...
    CacheBehaviors:
      - PathPatern: api/*
        LambdaFunctionAssociations:
          - EventType: origin-request
            LambdaFunctionARN: arn:...

The first argument in addOverride is a path to the AWS::CloudFront::Distribution LambdaFunctionAssociation.

So Properties.DistributionConfig.CacheBehaviors.0.LambdaFunctionAssociations.0.IncludeBody adds IncludeBody to LambdaFunctionAssociations[0] on CacheBehaviors[0].

Hopefully this will get sorted out soon, and we won't need to do this. Looks like the property in the CDK was just overlooked.

All 4 comments

Is there an intermediate way to achieve this for now?

@nitishdhar it's convoluted but the CDK provides a raw override.

Here's how I got it working:

const distribution = new cloudfront.CloudFrontWebDistribution(this, 'WebDistribution', {
  originConfigs: [{
    s3OriginSource: {
      s3BucketSource: bucket
    },
    behaviors: [{
      isDefaultBehavior: true,
      compress: true
    }, {
      pathPattern: 'api/*',
      compress: true,
      allowedMethods: cloudfront.CloudFrontAllowedMethods.ALL,
      minTtl: cdk.Duration.days(0),
      maxTtl: cdk.Duration.days(0),
      defaultTtl: cdk.Duration.days(0),
      forwardedValues: {
        queryString: true,
        cookies: {
          forward: 'all'
        }
      },
      lambdaFunctionAssociations: [{
        eventType: cloudfront.LambdaEdgeEventType.ORIGIN_REQUEST,
        lambdaFunction: apiLambdaVersion
      }]
    }]
  }]
})
// get the CloudFormation template
const distributionCfn = distribution.node.defaultChild as cloudfront.CfnDistribution
// Add IncludeBody on the first cache behavior - indexed at 0 because the default behaviour is not on the CacheBehaviors array 
distributionCfn.addOverride('Properties.DistributionConfig.CacheBehaviors.0.LambdaFunctionAssociations.0.IncludeBody', true)

distributionCfn above is the CloudFormation template which would look something like this:

Type: AWS::CloudFront::Distribution
Properties: 
  DistributionConfig:
    DefaultCacheBehavior:
      ...
    CacheBehaviors:
      - PathPatern: api/*
        LambdaFunctionAssociations:
          - EventType: origin-request
            LambdaFunctionARN: arn:...

The first argument in addOverride is a path to the AWS::CloudFront::Distribution LambdaFunctionAssociation.

So Properties.DistributionConfig.CacheBehaviors.0.LambdaFunctionAssociations.0.IncludeBody adds IncludeBody to LambdaFunctionAssociations[0] on CacheBehaviors[0].

Hopefully this will get sorted out soon, and we won't need to do this. Looks like the property in the CDK was just overlooked.

Hi @marcgreenstock and @nitishdhar

Yeap, this is just an oversight. Adding to our docket.

Thanks for reporting this and thanks @marcgreenstock for the correct workaround 馃憤

@marcgreenstock Thanks!! your solution worked for me as well.

Was this page helpful?
0 / 5 - 0 ratings