Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is best way to create invalidation after cloud formation created cloud front?

I am creating a completely serverless solution which will create an s3 bucket and CloudFront too. Using cloud formation template from bitbucket pipeline

I also want to create invalidate for CloudFront.

1) is it possible to create invalidation in cloud formation?

2) If no, then how can I get distribution id from my cloud formation and then create the invalidation using aws cli

CFDistribution:
Type: 'AWS::CloudFront::Distribution'
DependsOn: UIBucket
Properties:
  DistributionConfig:
    Aliases:
      - !Sub "${AppSubDomain}.${SSMDomain}"
    Origins:
      - DomainName: !GetAtt UIBucket.DomainName
        Id: S3BucketOrigin
        S3OriginConfig:
          OriginAccessIdentity: !Join
            - ''
            - - 'origin-access-identity/cloudfront/'
              - !Ref CFOriginAccessIdentity
    Comment: !Sub 'CloudFront origin for ${AppSubDomain}.${SSMDomain}'
    DefaultCacheBehavior:
      AllowedMethods:
        - GET
        - HEAD
        - OPTIONS
      TargetOriginId: S3BucketOrigin
      ForwardedValues:
        QueryString: 'false'
        Cookies:
          Forward: none
      ViewerProtocolPolicy: redirect-to-https
    DefaultRootObject: index.html
    Enabled: 'true'
    HttpVersion: http2
    PriceClass: PriceClass_All
    ViewerCertificate:
      AcmCertificateArn: !Ref SSMWildcardCertificateARN
      SslSupportMethod: sni-only
  Tags:
    - Key: "Type"
      Value: "Host"
    - Key: "Product"
      Value: !Ref Product
    - Key: "Environment"
      Value: !Ref SSMEnvironment
like image 749
Nikhil Kapoor Avatar asked Jul 15 '19 06:07

Nikhil Kapoor


People also ask

What is the use of invalidation in CloudFront?

Amazon CloudFront's invalidation feature, which allows you to remove an object from the CloudFront cache before it expires, now supports the * wildcard character. You can add a * wildcard character at the end of an invalidation path to remove all objects that match this path.

How long does it take for CloudFront to invalidate?

Object invalidations typically take from 10 to 100 seconds to complete. You can check the status of an invalidation by viewing your distribution from the CloudFront console.

How much does it cost to invalidate cache on CloudFront?

Invalidation requests for the first 1,000 files each month are provided at no additional charge; above this level, there is a $0.005 charge for invalidating each additional file. You can read more about the invalidation feature in the Amazon CloudFront Developer Guide.

How do I invalidate multiple files using the CloudFront API?

When you use the CloudFront API directly, invalidation paths must begin with a leading slash. You can also invalidate multiple files simultaneously by using the * wildcard. The *, which replaces 0 or more characters, must be the last character in the invalidation path.

Does the automatic invalidation of CloudFront URLs work with focus point?

The automatic invalidation does not work with focus point, which means that if you set a new focus point the CloudFront URLs will not be invalidated automatically. Once the cached URLs have expired the new focus point will be visible. The automatic invalidation feature does not work with any other CDN than CloudFront offered by AWS.

Why does CloudFront return an invalidationbatchalreadyexists error?

Instead, CloudFront returns information about the invalidation request that you previously created with the same CallerReference . If CallerReference is a value you already sent in a previous invalidation batch request but the content of any Path is different from the original request, CloudFront returns an InvalidationBatchAlreadyExists error.

How do I cancel an invalidation request in CloudFront?

Choose Invalidate. When you submit an invalidation request to CloudFront, CloudFront forwards the request to all edge locations within a few seconds, and each edge location starts processing the invalidation immediately. As a result, you can’t cancel an invalidation after you submit it.


1 Answers

I use CloudFront with CloudFormation too and I didn' find a way to create invalidation using CloudFormation. If you check AWS Docs, CloudFormation allows 3 types related to CloudFront

CloudFront
  AWS::CloudFront::CloudFrontOriginAccessIdentity
  AWS::CloudFront::Distribution
  AWS::CloudFront::StreamingDistribution

and none of these create an invalidation. Answering your first question:

1) is it possible to create invalidation in cloud formation?

No.

2) If no, then how can I get distribution id from my cloud formation and then create the invalidation using aws cli

You can add distribution to CloudFormation template output:

Outputs:
  CloudFrontDistributionID:
    Description: 'CloudFront distribution ID'
    Value: !Ref CloudFrontDistribution
  CloudFrontURL:
    Description: 'CloudFront URL'
    Value:!GetAtt CloudFrontDistribution.DomainName

save distribution ID using bash (check this question):

$ distributionId=${aws cloudformation describe-stacks --stack-name MY_STACK --query "Stacks[0].Outputs[?OutputKey=='CloudFrontDistributionID'].OutputValue" --output text}

and, finally, create CloudFront invalidation:

$ aws cloudfront create-invalidation --distribution-id $distributionId --paths /index.html /error.html
like image 185
Pedro Arantes Avatar answered Sep 22 '22 06:09

Pedro Arantes