Skip to content

Commit 177fc6e

Browse files
author
Peter Markushin
committed
Add CDK deployment stack for the frontend
1 parent d6e17ba commit 177fc6e

File tree

10 files changed

+2661
-7314
lines changed

10 files changed

+2661
-7314
lines changed

.eslintignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
node_modules
22
public
33
vite.config.ts
4-
serverless.yml
4+
serverless.yml
5+
.cdk.staging
6+
cdk.out

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ node_modules
1212
coverage
1313
dist
1414
dist-ssr
15+
dist-infra
16+
cdk.out
17+
.cdk.staging
18+
1519
*.local
1620

1721
# Editor directories and files

.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.ts
2+
!*.d.ts
3+
4+
# CDK asset staging directory
5+
.cdk.staging
6+
cdk.out

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,16 @@ Runs tests in console, in browser or with coverage.
3636
### `lint`, `prettier`
3737

3838
Runs linting and formatting for all files in `src` folder.
39+
40+
### `build-infra`
41+
42+
Builds CDK Stack
43+
44+
### `deploy`
45+
46+
Builds and deploys website, effectively `npm run build`, `npm run build-infra` and `cdk deploy`
47+
48+
## App URLs
49+
50+
* http://cdkstack-myfirstbucketb8884501-18j76n8h0qbzd.s3-website-us-east-1.amazonaws.com - responds with 403 due to bucket policies, use cloudfront distribution url instead
51+
* https://d3k4gkvfxelkyh.cloudfront.net - cloudfront distribution

cdk.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"app": "dist-infra/main.js",
3+
"context": {
4+
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
5+
"@aws-cdk/core:checkSecretUsage": true,
6+
"@aws-cdk/core:target-partitions": [
7+
"aws",
8+
"aws-cn"
9+
],
10+
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
11+
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
12+
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
13+
"@aws-cdk/aws-iam:minimizePolicies": true,
14+
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
15+
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
16+
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
17+
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
18+
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
19+
"@aws-cdk/core:enablePartitionLiterals": true,
20+
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
21+
"@aws-cdk/aws-iam:standardizedServicePrincipals": true,
22+
"@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true,
23+
"@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true,
24+
"@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true,
25+
"@aws-cdk/aws-route53-patters:useCertificate": true,
26+
"@aws-cdk/customresources:installLatestAwsSdkDefault": false,
27+
"@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true,
28+
"@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true,
29+
"@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true,
30+
"@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true,
31+
"@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true,
32+
"@aws-cdk/aws-redshift:columnId": true,
33+
"@aws-cdk/aws-stepfunctions-tasks:enableEmrServicePolicyV2": true,
34+
"@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true,
35+
"@aws-cdk/aws-apigateway:requestValidatorUniqueId": true
36+
}
37+
}

infra/src/main.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env node
2+
import "source-map-support/register";
3+
import * as cdk from "aws-cdk-lib";
4+
import { CdkStack } from "./main/app/cdk-stack";
5+
6+
const app = new cdk.App();
7+
new CdkStack(app, "CdkStack", {});

infra/src/main/app/cdk-stack.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { RemovalPolicy, CfnOutput } from "aws-cdk-lib";
2+
import * as cdk from "aws-cdk-lib";
3+
import { aws_cloudfront as cloudfront } from "aws-cdk-lib";
4+
import { Construct } from "constructs";
5+
import * as S3 from "aws-cdk-lib/aws-s3";
6+
import * as S3Deploy from "aws-cdk-lib/aws-s3-deployment";
7+
import {
8+
CloudFrontWebDistribution,
9+
OriginAccessIdentity,
10+
} from "aws-cdk-lib/aws-cloudfront";
11+
import { join } from "path";
12+
13+
export class CdkStack extends cdk.Stack {
14+
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
15+
super(scope, id, props);
16+
17+
const bucket = new S3.Bucket(this, "MyFirstBucket", {
18+
removalPolicy: RemovalPolicy.DESTROY,
19+
autoDeleteObjects: true,
20+
// blockPublicAccess: S3.BlockPublicAccess.BLOCK_ACLS,
21+
accessControl: S3.BucketAccessControl.BUCKET_OWNER_FULL_CONTROL,
22+
websiteIndexDocument: "index.html",
23+
websiteErrorDocument: "index.html",
24+
});
25+
26+
const accessDeniedErrorResponse: cloudfront.CfnDistribution.CustomErrorResponseProperty =
27+
{
28+
errorCode: 403,
29+
errorCachingMinTtl: 30,
30+
responseCode: 200,
31+
responsePagePath: "/index.html",
32+
};
33+
const notFoundErrorResponse: cloudfront.CfnDistribution.CustomErrorResponseProperty =
34+
{
35+
errorCode: 404,
36+
errorCachingMinTtl: 30,
37+
responseCode: 200,
38+
responsePagePath: "/index.html",
39+
};
40+
41+
const oai = new OriginAccessIdentity(this, `origin-access-id`, {});
42+
bucket.grantRead(oai);
43+
44+
const distribution = new CloudFrontWebDistribution(
45+
this,
46+
`MyFirstDistribution`,
47+
{
48+
originConfigs: [
49+
{
50+
s3OriginSource: {
51+
s3BucketSource: bucket,
52+
originAccessIdentity: oai,
53+
},
54+
behaviors: [{ isDefaultBehavior: true }],
55+
},
56+
],
57+
errorConfigurations: [accessDeniedErrorResponse, notFoundErrorResponse],
58+
}
59+
);
60+
61+
new S3Deploy.BucketDeployment(this, "MyFirstDeploy", {
62+
sources: [
63+
S3Deploy.Source.asset(join(__dirname, "..", "..", "..", "dist")),
64+
],
65+
destinationBucket: bucket,
66+
});
67+
68+
new CfnOutput(this, "BucketUrl", {
69+
value: bucket.bucketWebsiteUrl,
70+
exportName: "FrontendBucketUrl",
71+
});
72+
73+
new CfnOutput(this, "DistributionUrl", {
74+
value: distribution.distributionDomainName,
75+
exportName: "FrontendUrl",
76+
});
77+
}
78+
}

infra/tsconfig.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"module": "commonjs",
5+
"lib": [
6+
"es2020",
7+
"dom"
8+
],
9+
"declaration": true,
10+
"strict": true,
11+
"noImplicitAny": true,
12+
"strictNullChecks": true,
13+
"noImplicitThis": true,
14+
"alwaysStrict": true,
15+
"noUnusedLocals": false,
16+
"noUnusedParameters": false,
17+
"noImplicitReturns": true,
18+
"noFallthroughCasesInSwitch": false,
19+
"inlineSourceMap": true,
20+
"inlineSources": true,
21+
"experimentalDecorators": true,
22+
"strictPropertyInitialization": false,
23+
"typeRoots": [
24+
"../node_modules/@types"
25+
],
26+
"outDir": "../dist-infra",
27+
},
28+
"exclude": [
29+
"node_modules",
30+
"cdk.out"
31+
]
32+
}

0 commit comments

Comments
 (0)