|
| 1 | +import * as cdk from 'aws-cdk-lib'; |
| 2 | +import {RemovalPolicy} from 'aws-cdk-lib'; |
| 3 | +import {Construct} from 'constructs'; |
| 4 | +import {BlockPublicAccess, Bucket, BucketEncryption} from "aws-cdk-lib/aws-s3"; |
| 5 | +import {BucketDeployment, Source} from "aws-cdk-lib/aws-s3-deployment"; |
| 6 | +import { |
| 7 | + AllowedMethods, |
| 8 | + Distribution, |
| 9 | + Function, |
| 10 | + FunctionCode, |
| 11 | + FunctionEventType, |
| 12 | + FunctionRuntime, |
| 13 | + OriginAccessIdentity |
| 14 | +} from "aws-cdk-lib/aws-cloudfront"; |
| 15 | +import {BehaviorOptions} from "aws-cdk-lib/aws-cloudfront/lib/distribution"; |
| 16 | +import {S3Origin} from "aws-cdk-lib/aws-cloudfront-origins"; |
| 17 | + |
| 18 | +export class DemoCloudfrontFunctionsStack extends cdk.Stack { |
| 19 | + constructor(scope: Construct, id: string, props?: cdk.StackProps) { |
| 20 | + super(scope, id, props); |
| 21 | + |
| 22 | + // create a bucket to deploy the website files |
| 23 | + const bucket = new Bucket(this, 'WebsiteBucket', { |
| 24 | + encryption: BucketEncryption.S3_MANAGED, |
| 25 | + versioned: true, |
| 26 | + enforceSSL: true, |
| 27 | + blockPublicAccess: BlockPublicAccess.BLOCK_ALL, |
| 28 | + removalPolicy: RemovalPolicy.DESTROY |
| 29 | + }); |
| 30 | + |
| 31 | + // create a s3 bucket deployment to deploy the website directory's files to the website bucket |
| 32 | + new BucketDeployment(this, 'WebsiteFiles', { |
| 33 | + destinationBucket: bucket, |
| 34 | + sources: [Source.asset('./website')], |
| 35 | + contentType: 'text/html', |
| 36 | + retainOnDelete: false, |
| 37 | + }); |
| 38 | + |
| 39 | + // create an Origin Access Identity for CloudFront |
| 40 | + const cloudfrontOAI = new OriginAccessIdentity(this, 'cloudfront-OAI', { |
| 41 | + comment: `OAI for ${id}` |
| 42 | + }); |
| 43 | + |
| 44 | + // grant read permissions on the bucket to the CloudFront's Origin Access Identity |
| 45 | + bucket.grantRead(cloudfrontOAI); |
| 46 | + |
| 47 | + // create a cloudFront function from the request-function.js file |
| 48 | + const requestFunction = new Function(this, 'RequestFunction', { |
| 49 | + functionName: 'RequestFunction', |
| 50 | + runtime: FunctionRuntime.JS_2_0, |
| 51 | + code: FunctionCode.fromFile({ |
| 52 | + filePath: './functions/request-function.js' |
| 53 | + }) |
| 54 | + }); |
| 55 | + |
| 56 | + // create a cloudFront function from the response-function.js file |
| 57 | + const responseFunction = new Function(this, 'ResponseFunction', { |
| 58 | + functionName: 'ResponseFunction', |
| 59 | + runtime: FunctionRuntime.JS_2_0, |
| 60 | + code: FunctionCode.fromFile({ |
| 61 | + filePath: './functions/response-function.js' |
| 62 | + }) |
| 63 | + }); |
| 64 | + |
| 65 | + // create a CloudFront behavior with origin of my website bucket and both request and response functions |
| 66 | + const defaultBehavior: BehaviorOptions = { |
| 67 | + origin: new S3Origin(bucket), |
| 68 | + compress: true, |
| 69 | + allowedMethods: AllowedMethods.ALLOW_ALL, |
| 70 | + functionAssociations: [ |
| 71 | + { |
| 72 | + function: requestFunction, |
| 73 | + eventType: FunctionEventType.VIEWER_REQUEST, |
| 74 | + }, |
| 75 | + { |
| 76 | + function: responseFunction, |
| 77 | + eventType: FunctionEventType.VIEWER_RESPONSE, |
| 78 | + } |
| 79 | + ] |
| 80 | + }; |
| 81 | + |
| 82 | + // create a CloudFront distribution with the behavior created |
| 83 | + const distribution = new Distribution(this, 'SiteDistribution', { |
| 84 | + comment: 'CloudFront Functions example', |
| 85 | + defaultRootObject: 'index.html', |
| 86 | + defaultBehavior: defaultBehavior, |
| 87 | + }); |
| 88 | + |
| 89 | + // create an output with the CloudFront distribution URL |
| 90 | + new cdk.CfnOutput(this, 'DistributionDomainName', { |
| 91 | + value: distribution.domainName, |
| 92 | + }); |
| 93 | + |
| 94 | + } |
| 95 | +} |
0 commit comments