Skip to content

Commit 984f820

Browse files
authored
Merge pull request #42 from modoodalvi/feature/cdkv2
Migrate to CDK v2
2 parents 33fca72 + 19ac1e9 commit 984f820

8 files changed

Lines changed: 30551 additions & 7973 deletions

File tree

cdk/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ cdk.out
99

1010
# Parcel default cache directory
1111
.parcel-cache
12+
13+
# IDEs
14+
.idea

cdk/bin/cdk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
// SPDX-License-Identifier: MIT-0
44

55
import "source-map-support/register";
6-
import * as cdk from "@aws-cdk/core";
6+
import { App } from "aws-cdk-lib";
77
import { ApiStack } from "../lib/api-stack";
88
import { SsrStack } from "../lib/srr-stack";
99

1010
const demoEnv = { region: "us-east-1" };
11-
const app = new cdk.App();
11+
const app = new App();
1212
new ApiStack(app, "SSRApiStack", { env: demoEnv });
1313
new SsrStack(app, "SSRAppStack", { env: demoEnv });

cdk/cdk.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
{
22
"app": "npx ts-node bin/cdk.ts",
33
"context": {
4-
"@aws-cdk/core:enableStackNameDuplicates": "true",
5-
"aws-cdk:enableDiffNoFail": "true",
6-
"@aws-cdk/core:stackRelativeExports": "true"
74
}
85
}

cdk/lib/api-stack.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
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 { Duration } from "@aws-cdk/core";
6-
import * as lambda from "@aws-cdk/aws-lambda";
7-
import * as apigw from "@aws-cdk/aws-apigateway";
4+
import { Duration, CfnOutput, Stack, StackProps } from "aws-cdk-lib";
5+
import * as lambda from "aws-cdk-lib/aws-lambda";
6+
import * as apigw from "aws-cdk-lib/aws-apigateway";
7+
import { Construct } from "constructs";
88

9-
export class ApiStack extends cdk.Stack {
10-
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
9+
export class ApiStack extends Stack {
10+
constructor(scope: Construct, id: string, props?: StackProps) {
1111
super(scope, id, props);
1212

1313
const apiFunction = new lambda.Function(this, "apiHandler", {
1414
runtime: lambda.Runtime.NODEJS_12_X,
1515
code: lambda.Code.fromAsset("../simple-ssr/api"),
1616
memorySize: 128,
1717
timeout: Duration.seconds(5),
18-
handler: "index.handler"
18+
handler: "index.handler",
1919
});
2020

2121
const api = new apigw.LambdaRestApi(this, "apiEndpoint", {
2222
handler: apiFunction,
2323
defaultCorsPreflightOptions: {
2424
allowOrigins: apigw.Cors.ALL_ORIGINS,
25-
allowMethods: apigw.Cors.ALL_METHODS
26-
}
25+
allowMethods: apigw.Cors.ALL_METHODS,
26+
},
2727
});
2828

29-
new cdk.CfnOutput(this, "apiurl", { value: api.url });
29+
new CfnOutput(this, "apiurl", { value: api.url });
3030
}
3131
}

cdk/lib/srr-stack.ts

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
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

Comments
 (0)