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
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.
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.
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With