Skip to content

Commit

Permalink
fix: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
apalchys committed Nov 27, 2023
1 parent b77db6d commit 786fad5
Show file tree
Hide file tree
Showing 25 changed files with 5,363 additions and 459 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Build & Deploy

on:
push:
branches: [main]

concurrency:
group: pr_${{ github.head_ref }}
cancel-in-progress: true

jobs:
build_deploy:
name: Build & Deploy
runs-on: ubuntu-latest
steps:
- name: Setup Node.js environment
uses: actions/setup-node@v3
with:
node-version: "20"

- name: Checkout
uses: actions/checkout@v3

- name: Install dependencies
uses: bahmutov/npm-install@v1

- name: Build
run: |
npm run build
npm run deploy
- name: Deploy
env:
AWS_ACCESS_KEY_ID: ${{ secrets.DEPLOY_AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.DEPLOY_AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: eu-central-1
run: |
npx cdk deploy --require-approval never
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
.DS_Store
node_modules/
cdk.out/
6 changes: 6 additions & 0 deletions cdk.context.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"hosted-zone:account=511361162520:domainName=rs.school:region=eu-central-1": {
"Id": "/hostedzone/Z060689922CU3BKTLJ3DO",
"Name": "rs.school."
}
}
10 changes: 10 additions & 0 deletions cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"app": "npx ts-node src/cdk/App.ts",
"context": {
"@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
"@aws-cdk/core:stackRelativeExports": true,
"@aws-cdk/aws-lambda:recognizeVersionProps": true,
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true,
"@aws-cdk/core:target-partitions": ["aws", "aws-cn"]
}
}
3,686 changes: 3,227 additions & 459 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
{
"scripts": {
"build": "tsc",
"deploy": "npx cdk deploy --require-approval never",
"format": "prettier --write src"
},
"dependencies": {
"@aws-cdk/aws-apigatewayv2-alpha": "2.110.0-alpha.0",
"@aws-cdk/aws-apigatewayv2-integrations-alpha": "2.110.0-alpha.0",
"@aws-sdk/client-dynamodb": "^3.458.0",
"aws-cdk-lib": "2.110.1",
"constructs": "^10",
"querystring": "^0.2.1",
"ts-node": "^10.9.1"
},
"devDependencies": {
"@types/aws-lambda": "^8.10.129",
"esbuild": "^0.19.8",
"prettier": "^3.1.0"
}
}
195 changes: 195 additions & 0 deletions src/cdk/AngularCourseStack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import * as cdk from "aws-cdk-lib";
import * as route53 from "aws-cdk-lib/aws-route53";
import * as alias from "aws-cdk-lib/aws-route53-targets";
import * as apiv2 from "@aws-cdk/aws-apigatewayv2-alpha";
import * as integration from "@aws-cdk/aws-apigatewayv2-integrations-alpha";

import * as cloudfront from "aws-cdk-lib/aws-cloudfront";
import { Construct } from "constructs";
import { CfnOutput } from "aws-cdk-lib";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { Runtime } from "aws-cdk-lib/aws-lambda";

type Props = cdk.StackProps & {
certificateArn: string;
};

export class AngularCourseStack extends cdk.Stack {
fqdn: string;
url: string;

constructor(scope: Construct, id: string, props: Props) {
super(scope, id, props);

const { certificateArn } = props;

this.fqdn = `tasks.app.rs.school`;
this.url = `https://${this.fqdn}`;

const api: {
path: string;
method: string;
lambdaName: string;
}[] = [
{
path: "/login",
method: "POST",
lambdaName: "login",
},
{
path: "/logout",
method: "DELETE",
lambdaName: "logout",
},
{
path: "/registration",
method: "POST",
lambdaName: "registration",
},
{
path: "/users",
method: "GET",
lambdaName: "users",
},
{
path: "/profile",
method: "GET",
lambdaName: "profile-read",
},
{
path: "/profile",
method: "PUT",
lambdaName: "profile-update",
},
{
path: "/conversations/list",
method: "GET",
lambdaName: "list-my-conversations",
},
{
path: "/conversations/create",
method: "POST",
lambdaName: "create-personal-conversation",
},
{
path: "/conversations/delete",
method: "DELETE",
lambdaName: "delete-personal-conversation",
},
{
path: "/conversations/read",
method: "GET",
lambdaName: "conversation-read-messages",
},
{
path: "/conversations/append",
method: "POST",
lambdaName: "conversation-add-message",
},
{
path: "/groups/list",
method: "GET",
lambdaName: "groups-list",
},
{
path: "/groups/create",
method: "POST",
lambdaName: "groups-create",
},
{
path: "/groups/delete",
method: "DELETE",
lambdaName: "groups-delete",
},
{
path: "/groups/read",
method: "GET",
lambdaName: "groups-read-messages",
},
{
path: "/groups/append",
method: "POST",
lambdaName: "groups-add-message",
},
];

const httpApi = new apiv2.HttpApi(this, "HttpApi");

for (const route of api) {
new apiv2.HttpRoute(this, `Route-${route.path}-${route.method}`, {
httpApi,
integration: new integration.HttpLambdaIntegration(
`Integration-${route.path}-${route.method}`,
new NodejsFunction(this, `Lambda-${route.lambdaName}`, {
entry: `./src/functions/${route.lambdaName}.ts`,
memorySize: 256,
runtime: Runtime.NODEJS_20_X,
bundling: {
externalModules: ["@aws-sdk/*"],
},
}),
),
routeKey: apiv2.HttpRouteKey.with("/"),
});
}

const noCacheBehavior: cloudfront.Behavior = {
allowedMethods: cloudfront.CloudFrontAllowedMethods.ALL,
defaultTtl: cdk.Duration.seconds(0),
minTtl: cdk.Duration.seconds(0),
maxTtl: cdk.Duration.seconds(0),
forwardedValues: {
queryString: true,
headers: ["Origin", "Authorization"],
cookies: {
forward: "all",
},
},
};

const distribution = new cloudfront.CloudFrontWebDistribution(
this,
"Tasks",
{
originConfigs: [
{
customOriginSource: {
domainName: httpApi.url!,
originProtocolPolicy: cloudfront.OriginProtocolPolicy.HTTPS_ONLY,
},
behaviors: [
{
isDefaultBehavior: true,
pathPattern: "/angular/*",
...noCacheBehavior,
},
],
},
],
defaultRootObject: "/",
viewerCertificate: {
aliases: [this.fqdn],
props: {
// cloudfront needs certificate in us-east-1 so we pass it as string
acmCertificateArn: certificateArn,
sslSupportMethod: "sni-only",
minimumProtocolVersion: "TLSv1.2_2019",
},
},
},
);

// Create a DNS record. in Production it will be an apex record, otherwise we set recordName
new route53.ARecord(this, "AliasRecord", {
target: route53.RecordTarget.fromAlias(
new alias.CloudFrontTarget(distribution),
),
zone: route53.HostedZone.fromLookup(this, "HostedZone", {
domainName: "rs.school",
}),
recordName: this.fqdn,
});

new CfnOutput(this, "Url", { value: this.url });
}
}
10 changes: 10 additions & 0 deletions src/cdk/App.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as cdk from "aws-cdk-lib";
import { AngularCourseStack } from "./AngularCourseStack";

const app = new cdk.App();

new AngularCourseStack(app, `rsschool-tasks`, {
env: { account: "511361162520", region: "eu-central-1" },
certificateArn:
"arn:aws:acm:us-east-1:511361162520:certificate/07e01035-1bb4-430c-8b82-625565f66bdb",
});
Loading

0 comments on commit 786fad5

Please sign in to comment.