-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteg.s3.ts
94 lines (82 loc) · 3.75 KB
/
integ.s3.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import cdk = require("aws-cdk-lib");
import { Construct } from 'constructs';
import cf = require('aws-cdk-lib/aws-cloudfront');
import iam = require('aws-cdk-lib/aws-iam');
import s3 = require('aws-cdk-lib/aws-s3');
import s3d = require('aws-cdk-lib/aws-s3-deployment');
import lambda = require('aws-cdk-lib/aws-lambda');
import * as path from 'path';
import * as hasha from 'hasha';
export interface CloudFrontOrginS3WithLambdaEdgeStackProps extends cdk.StackProps {
}
export class CloudFrontOrginS3WithLambdaEdgeStack extends cdk.Stack {
constructor(scope: Construct, id: string, props: CloudFrontOrginS3WithLambdaEdgeStackProps) {
super(scope, id, props);
const bucket = new s3.Bucket(this, "WebsiteBucket", {
bucketName: "cdk-sample-cloudfront-lambda-edge-s3-" + this.region + '-' + this.account,
// publicReadAccess: true,
websiteIndexDocument: "index.html",
});
new s3d.BucketDeployment(this, 'SimpleIndex', {
destinationBucket: bucket,
// ../../samples/website/
sources:[s3d.Source.asset(path.join(__dirname, '..', '..', 'samples', 'website'))],
});
const edgeLambdaExecutionRole = new iam.Role(this, 'EdgeLambdaExecutionRole', {
assumedBy: new iam.CompositePrincipal(
new iam.ServicePrincipal('lambda.amazonaws.com'),
new iam.ServicePrincipal('edgelambda.amazonaws.com')
),
managedPolicies: [
iam.ManagedPolicy.fromAwsManagedPolicyName(
'service-role/AWSLambdaBasicExecutionRole'
),
]
});
/// URL Rewrite of CloudFront Origin request
// const rewriteLambda = new lambda.Function(this, "UrlRewriteHandler", {
// code: new lambda.AssetCode("./samples/lambda/cloudfrontUrlRewrite"),
// handler: "index.handler",
// runtime: lambda.Runtime.NODEJS_12_X,
// role: edgeLambdaExecutionRole
// });
const rewriteLambda = new cf.experimental.EdgeFunction(this, "rewriteHandler", {
code: new lambda.AssetCode("./samples/lambda/cloudfrontUrlRewrite"),
handler: "index.handler",
runtime: lambda.Runtime.NODEJS_12_X,
role: edgeLambdaExecutionRole
});
/// Modify header of CloudFront Origin response
const responseHeaderLambda = new cf.experimental.EdgeFunction(this, "ReponseHeaderHandler", {
code: new lambda.AssetCode("./samples/lambda/cloudfrontUrlRewrite"),
handler: "index.handler",
runtime: lambda.Runtime.NODEJS_12_X,
role: edgeLambdaExecutionRole
});
const cloudfront = new cf.CloudFrontWebDistribution(this, 'MyAppCloudFront', {
originConfigs: [
{
s3OriginSource:{
s3BucketSource: bucket
},
behaviors: [{
isDefaultBehavior: true,
lambdaFunctionAssociations: [
{
eventType: cf.LambdaEdgeEventType.ORIGIN_REQUEST,
lambdaFunction: rewriteLambda
},
{
eventType: cf.LambdaEdgeEventType.ORIGIN_RESPONSE,
lambdaFunction: responseHeaderLambda
}
]
}]
}
],
});
// Outputs
new cdk.CfnOutput(this, 'BucketName', { value:bucket.bucketName });
new cdk.CfnOutput(this, 'CloudFrontWebsiteUrlExport', { value:cloudfront.distributionDomainName });
}
}