Skip to content

Commit ace3cff

Browse files
committed
add work to complete task #2
1 parent 4979e7f commit ace3cff

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ dist-ssr
2424
*.njsproj
2525
*.sln
2626
*.sw?
27+
cdk.out

cdk.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"app": "npx ts-node src/index.ts"
3+
}

src/index.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import * as s3 from "@aws-cdk/aws-s3";
2+
import * as iam from "@aws-cdk/aws-iam";
3+
import * as cdk from "@aws-cdk/core";
4+
import * as cloudFront from "@aws-cdk/aws-cloudfront";
5+
import * as deploy from "@aws-cdk/aws-s3-deployment";
6+
7+
const APP_PREFIX = "bw-nodejs-aws-shop-react";
8+
9+
const app = new cdk.App();
10+
11+
const stack = new cdk.Stack(app, `${APP_PREFIX}-stack`, {
12+
description: `This stack includes resources needed to deploy ${APP_PREFIX} application`,
13+
});
14+
15+
const bucket = new s3.Bucket(stack, `${APP_PREFIX}-bucket`, {
16+
bucketName: "bw-nodejs-aws-shop-react-cdk",
17+
websiteIndexDocument: "index.html",
18+
publicReadAccess: false,
19+
blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
20+
});
21+
22+
const oai = new cloudFront.OriginAccessIdentity(stack, `${APP_PREFIX}-oai`);
23+
24+
bucket.addToResourcePolicy(
25+
new iam.PolicyStatement({
26+
actions: ["s3:GetObject"],
27+
resources: [bucket.arnForObjects("*")],
28+
principals: [
29+
new iam.CanonicalUserPrincipal(
30+
oai.cloudFrontOriginAccessIdentityS3CanonicalUserId
31+
),
32+
],
33+
})
34+
);
35+
36+
const distribution = new cloudFront.CloudFrontWebDistribution(
37+
stack,
38+
`${APP_PREFIX}-distribution`,
39+
{
40+
comment: `${APP_PREFIX} distribution`,
41+
originConfigs: [
42+
{
43+
s3OriginSource: {
44+
s3BucketSource: bucket,
45+
originAccessIdentity: oai,
46+
},
47+
behaviors: [
48+
{
49+
isDefaultBehavior: true,
50+
},
51+
],
52+
},
53+
],
54+
}
55+
);
56+
57+
new deploy.BucketDeployment(stack, `${APP_PREFIX}-deployment`, {
58+
sources: [deploy.Source.asset("./dist")],
59+
destinationBucket: bucket,
60+
distribution,
61+
distributionPaths: ["/index.html", "/assets/*"],
62+
});
63+
64+
app.synth();

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"allowSyntheticDefaultImports": true,
1414
"strict": true,
1515
"forceConsistentCasingInFileNames": true,
16-
"module": "ESNext",
16+
"module": "commonjs",
1717
"moduleResolution": "Node",
1818
"resolveJsonModule": true,
1919
"isolatedModules": true,

0 commit comments

Comments
 (0)