11// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22// SPDX-License-Identifier: MIT-0
33
4- import * as cdk from "@aws-cdk/core" ;
5- import { CfnParameter , Duration } from "@aws-cdk/core" ;
6- import * as s3 from "@aws-cdk/aws-s3" ;
7- import * as s3deploy from "@aws-cdk/aws-s3-deployment" ;
8- import * as cloudfront from "@aws-cdk/aws-cloudfront" ;
9- import * as lambda from "@aws-cdk/aws-lambda" ;
10- import * as apigw from "@aws-cdk/aws-apigateway" ;
4+ import {
5+ Duration ,
6+ CfnOutput ,
7+ Stack ,
8+ StackProps ,
9+ RemovalPolicy ,
10+ } from "aws-cdk-lib" ;
11+ import * as s3 from "aws-cdk-lib/aws-s3" ;
12+ import * as s3deploy from "aws-cdk-lib/aws-s3-deployment" ;
13+ import * as cloudfront from "aws-cdk-lib/aws-cloudfront" ;
14+ import * as lambda from "aws-cdk-lib/aws-lambda" ;
15+ import * as apigw from "aws-cdk-lib/aws-apigateway" ;
16+ import { Construct } from "constructs" ;
1117
12- export class SsrStack extends cdk . Stack {
13- constructor ( scope : cdk . Construct , id : string , props ?: cdk . StackProps ) {
18+ export class SsrStack extends Stack {
19+ constructor ( scope : Construct , id : string , props ?: StackProps ) {
1420 super ( scope , id , props ) ;
1521
16- const mySiteBucketName = new CfnParameter ( this , "mySiteBucketName" , {
17- type : "String" ,
18- description : "The name of S3 bucket to upload react application"
19- } ) ;
20-
2122 const mySiteBucket = new s3 . Bucket ( this , "ssr-site" , {
22- bucketName : mySiteBucketName . valueAsString ,
2323 websiteIndexDocument : "index.html" ,
2424 websiteErrorDocument : "error.html" ,
2525 publicReadAccess : false ,
2626 //only for demo not to use in production
27- removalPolicy : cdk . RemovalPolicy . DESTROY
27+ removalPolicy : RemovalPolicy . DESTROY ,
2828 } ) ;
29- new cdk . CfnOutput ( this , "Bucket" , { value : mySiteBucket . bucketName } ) ;
29+ new CfnOutput ( this , "Bucket" , { value : mySiteBucket . bucketName } ) ;
3030
3131 const originAccessIdentity = new cloudfront . OriginAccessIdentity (
3232 this ,
@@ -36,23 +36,23 @@ export class SsrStack extends cdk.Stack {
3636
3737 new s3deploy . BucketDeployment ( this , "Client-side React app" , {
3838 sources : [ s3deploy . Source . asset ( "../simple-ssr/build/" ) ] ,
39- destinationBucket : mySiteBucket
39+ destinationBucket : mySiteBucket ,
4040 } ) ;
4141
4242 const ssrFunction = new lambda . Function ( this , "ssrHandler" , {
4343 runtime : lambda . Runtime . NODEJS_12_X ,
4444 code : lambda . Code . fromAsset ( "../simple-ssr/server-build" ) ,
4545 memorySize : 128 ,
4646 timeout : Duration . seconds ( 5 ) ,
47- handler : "index.handler"
47+ handler : "index.handler" ,
4848 } ) ;
4949
5050 const ssrEdgeFunction = new lambda . Function ( this , "ssrEdgeHandler" , {
5151 runtime : lambda . Runtime . NODEJS_12_X ,
5252 code : lambda . Code . fromAsset ( "../simple-ssr/edge-build" ) ,
5353 memorySize : 128 ,
5454 timeout : Duration . seconds ( 5 ) ,
55- handler : "index.handler"
55+ handler : "index.handler" ,
5656 } ) ;
5757
5858 const ssrEdgeFunctionVersion = new lambda . Version (
@@ -62,10 +62,10 @@ export class SsrStack extends cdk.Stack {
6262 ) ;
6363
6464 const ssrApi = new apigw . LambdaRestApi ( this , "ssrEndpoint" , {
65- handler : ssrFunction
65+ handler : ssrFunction ,
6666 } ) ;
6767
68- new cdk . CfnOutput ( this , "SSR API URL" , { value : ssrApi . url } ) ;
68+ new CfnOutput ( this , "SSR API URL" , { value : ssrApi . url } ) ;
6969
7070 const apiDomainName = `${ ssrApi . restApiId } .execute-api.${ this . region } .amazonaws.com` ;
7171
@@ -77,44 +77,44 @@ export class SsrStack extends cdk.Stack {
7777 {
7878 s3OriginSource : {
7979 s3BucketSource : mySiteBucket ,
80- originAccessIdentity : originAccessIdentity
80+ originAccessIdentity : originAccessIdentity ,
8181 } ,
8282 behaviors : [
8383 {
8484 isDefaultBehavior : true ,
8585 lambdaFunctionAssociations : [
8686 {
8787 eventType : cloudfront . LambdaEdgeEventType . ORIGIN_REQUEST ,
88- lambdaFunction : ssrEdgeFunctionVersion
89- }
90- ]
91- }
92- ]
88+ lambdaFunction : ssrEdgeFunctionVersion ,
89+ } ,
90+ ] ,
91+ } ,
92+ ] ,
9393 } ,
9494 {
9595 customOriginSource : {
9696 domainName : apiDomainName ,
9797 originPath : "/prod" ,
98- originProtocolPolicy : cloudfront . OriginProtocolPolicy . HTTPS_ONLY
98+ originProtocolPolicy : cloudfront . OriginProtocolPolicy . HTTPS_ONLY ,
9999 } ,
100100 behaviors : [
101101 {
102- pathPattern : "/ssr"
103- }
104- ]
105- }
106- ]
102+ pathPattern : "/ssr" ,
103+ } ,
104+ ] ,
105+ } ,
106+ ] ,
107107 }
108108 ) ;
109109
110- new cdk . CfnOutput ( this , "CF URL" , {
111- value : `https://${ distribution . distributionDomainName } `
110+ new CfnOutput ( this , "CF URL" , {
111+ value : `https://${ distribution . distributionDomainName } ` ,
112112 } ) ;
113- new cdk . CfnOutput ( this , "Lambda SSR URL" , {
114- value : `https://${ distribution . distributionDomainName } /ssr`
113+ new CfnOutput ( this , "Lambda SSR URL" , {
114+ value : `https://${ distribution . distributionDomainName } /ssr` ,
115115 } ) ;
116- new cdk . CfnOutput ( this , "Lambda@Edge SSR URL" , {
117- value : `https://${ distribution . distributionDomainName } /edgessr`
116+ new CfnOutput ( this , "Lambda@Edge SSR URL" , {
117+ value : `https://${ distribution . distributionDomainName } /edgessr` ,
118118 } ) ;
119119 }
120120}
0 commit comments