Search code examples
amazon-web-servicesaws-cloudformationamazon-cloudfront

CloudFormation deploy doesn't associate second CloudFront function


I have a CloudFront distribution with a single function:

  CloudFrontDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
…
        DefaultCacheBehavior:
…
          FunctionAssociations:
            - EventType: viewer-request
              FunctionARN: !GetAtt FooFunction.FunctionMetadata.FunctionARN

I deploy the stack using aws cloudformation deploy … and see that in the console the FooFunction has a status of Deployed.

I define another function in the CloudFormation template and call aws cloudformation deploy … again.

  CloudFrontDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
…
        DefaultCacheBehavior:
…
          FunctionAssociations:
            - EventType: viewer-request
              FunctionARN: !GetAtt FooFunction.FunctionMetadata.FunctionARN
            - EventType: viewer-request
              FunctionARN: !GetAtt BarFunction.FunctionMetadata.FunctionARN

Note that both functions have AutoPublish: true set.

In the console both functions now appear under "Functions". The status of FooFunction still shows Deployed. However the status of BarFunction is Published. And if I click on BarFunction, under "Associated distributions" it says:

This function is not associated with any distributions.

Yet it is clearly listed under FunctionAssociations in the CloudFormation template.

I've tried almost everything to try to get around this. Most recently I even deleted both functions.

  1. I deleted both functions themselves from the template, as well as the entire FunctionAssociations section of the distribution.
  2. I did an aws cloudformation deploy … and then verified in the console that both functions no longer exist.
  3. I manually invalidated the entire (/*) distribution from the console.
  4. I added back both functions to the template, along with the FunctionAssociations.
  5. I did another aws cloudformation deploy.

I get back two functions:

  • FooFunction: Deployed
  • BarFunction: Published

Somehow AWS remembered these two functions, even though they were supposedly completely deleted, and only associated one of them with the distribution, when the configurations for both are identical in the template.

Why is CloudFormation associating only one function with the distribution and not both, and remembering which was associated with the distribution and which was not even after deleting them?

I opened CloudFormation CLI Issue #991 to report this, but I'm asking here in hopes it's just something simple I'm doing wrong. That ticket explains all the other workarounds I've tried. There seems to be no way to have two functions deployed at the same time, even though they are both listed in the FunctionAssociations section.


Solution

  • I think your problem here is that both functions are associated to the same distribution, event type and cache behavior.

    Per the docs restrictions on all edge functions

    For a given cache behavior, the following restrictions apply:

    Each event type (viewer request, origin request, origin response, and viewer response) can have only one edge function association.

    Based on your CFN it appears that you are associating both viewer-request functions to the same Default Cache Behavior.

    If you want both of them deployed then you need to associate each function to different event types and/or different cache behaviors.