Created
May 27, 2022 07:14
-
-
Save jatinmehrotra/e79c185d6b88ab3ed28ffb38670684cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Stack, Construct, Aws, StackProps } from '@aws-cdk/core' | |
import * as s3 from '@aws-cdk/aws-s3' | |
import * as cloudfront from '@aws-cdk/aws-cloudfront' | |
import * as lambda from '@aws-cdk/aws-lambda' | |
import * as origins from '@aws-cdk/aws-cloudfront-origins' | |
import * as s3deploy from '@aws-cdk/aws-s3-deployment' | |
export class CdkCdnLambdaedgeStack extends Stack { | |
readonly bucket: s3.Bucket | |
readonly distribution: cloudfront.Distribution | |
constructor(scope: Construct, id: string, props?: StackProps) { | |
super(scope, id, props); | |
const bucket = new s3.Bucket(this, 'Bucket', { | |
cors: [ | |
{ | |
allowedMethods: [s3.HttpMethods.GET, s3.HttpMethods.HEAD], | |
allowedOrigins: ['*'], | |
allowedHeaders: ['*'], | |
}, | |
], | |
}) | |
// new s3deploy.BucketDeployment(this, 'DeployWebsite', { | |
// sources: [s3deploy.Source.asset('../')], | |
// destinationBucket: bucket | |
// }); | |
const uriRedirector = new cloudfront.experimental.EdgeFunction( | |
this, | |
'UriRedirector', | |
{ | |
code: lambda.Code.fromAsset('dist/events/object-cache'), | |
runtime: lambda.Runtime.NODEJS_14_X, | |
handler: 'index.handle', | |
} | |
) | |
const defaultBehaviourOriginRequestPolicy = new cloudfront.OriginRequestPolicy( | |
this, | |
'OriginRequestPolicy', | |
{ | |
cookieBehavior: cloudfront.OriginRequestCookieBehavior.all(), | |
headerBehavior: cloudfront.OriginRequestHeaderBehavior.allowList( | |
'Origin', | |
'Access-Control-Request-Headers', | |
'Access-Control-Request-Method' | |
), | |
queryStringBehavior: cloudfront.OriginRequestQueryStringBehavior.all(), | |
} | |
) | |
const s3Origin = new origins.S3Origin(bucket) | |
this.distribution = new cloudfront.Distribution(this, 'Distribution', { | |
defaultBehavior: { | |
origin: s3Origin, | |
edgeLambdas: [ | |
{ | |
functionVersion: uriRedirector.currentVersion, | |
eventType: cloudfront.LambdaEdgeEventType.ORIGIN_REQUEST, | |
}, | |
], | |
originRequestPolicy: defaultBehaviourOriginRequestPolicy, | |
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.HTTPS_ONLY, | |
allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL, | |
}, | |
sslSupportMethod: cloudfront.SSLMethod.SNI, | |
minimumProtocolVersion: cloudfront.SecurityPolicyProtocol.TLS_V1_2016, | |
enableLogging: true, | |
logIncludesCookies: true, | |
defaultRootObject: 'index.html', | |
priceClass: cloudfront.PriceClass.PRICE_CLASS_ALL, | |
}) | |
const cfnDistribution = this.distribution.node | |
.defaultChild as cloudfront.CfnDistribution | |
cfnDistribution.overrideLogicalId('Distribution830FAC52') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment