Cloudfront Function and association using AWS-CDK
Amazon CloudFront is a service that speeds up the distribution and delivery of static and dynamic web content through its global network of machines spread across hundreds of locations, also known as edge locations.
How Cloudfront Works?
CloudFront Functions are natively built into the CloudFront infrastructure with over 218+ points of presence spread across 90 cities and 47 countries. Each of these locations hosts an instance of the Functions runtime, which is an ECMAScript 5.1-compliant JavaScript engine, and each of these runtimes is capable of handling tens of millions of requests per second while delivering sub-millisecond latency.
We have already covered on how to do it through AWS CLI, check here(https://kuchbhilearning.blogspot.com/2022/09/aws-cloudfront-function-aws-has.html)
In this article let's focus on doing this through AWS-CDK.
We are assuming that you already have a sample code of function, with that being said let's dive in.
import * as cloudFront from 'aws-cdk-lib/aws-cloudfront';// After importing the cloudfront.// Cloudfront has a exposed a class called Functionconst cloudFrontFunction = new cloudFront.Function(stack, `cf-function`, {code: cloudFront.FunctionCode.fromFile({filePath: path.join(__dirname, 'cf-func/dist/index.js') // Give the path of the function})});const cloudFrontDistribution = new cloudFront.Distribution(stack, `cloudfront-distribution`, {// Default path pointing to S3 Web Bucket where static content residesdefaultBehavior: {// Point to S3 Web Bucket as originorigin: new cloudFrontOrigins.S3Origin(s3BucketWeb), // S3 bucket path// HTTP requests will be redirected to HTTPSviewerProtocolPolicy: cloudFront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,// Only allow GET, HEAD, OPTIONS methodsallowedMethods: cloudFront.AllowedMethods.ALLOW_GET_HEAD_OPTIONS,// Cache GET, HEAD, OPTIONScachedMethods: cloudFront.CachedMethods.CACHE_GET_HEAD_OPTIONS,// Apply the following to API endpoints onlyfunctionAssociations: [{function: cloudFrontFunction,eventType: cloudFront.FunctionEventType.VIEWER_REQUEST}]},});
The more important part to note here is the FunctionAssociation property to which we can assign the function and add an event type.
In this example we have kept the event type to Viewer_Request. You can also choose Viewer_Response based on the requirement.
Further Reading
Custom Header in Cloudfront(https://kuchbhilearning.blogspot.com/2022/11/add-custom-header-in-cloudfrontpass.html)
API Gateway and CloudFront in the same domain with different path(https://kuchbhilearning.blogspot.com/2022/10/api-gateway-and-cloud-front-in-same.html)
Pass query params from CloudFront to API Gateway AWS-CDK(https://kuchbhilearning.blogspot.com/2022/10/pass-query-params-from-cloudfront-to.html)
Add CloudFront Behavior and Origin using AWS-CDK(https://kuchbhilearning.blogspot.com/2022/10/add-cloudfront-behavior-and-origin.html)
AWS CloudFront Function(https://kuchbhilearning.blogspot.com/2022/09/aws-cloudfront-function-aws-has.html)
No comments:
Post a Comment
If you have any doubts, Please let me know