-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b597928
commit 6537fb3
Showing
10 changed files
with
262 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Simple Web Service</title> | ||
<style> | ||
|
||
pre { | ||
background: #eee; | ||
border-radius: 4px; | ||
padding: 1em; | ||
} | ||
|
||
form { | ||
margin: 1em; | ||
} | ||
|
||
</style> | ||
</head> | ||
<body> | ||
<h1>Simple Web Service</h1> | ||
Enter something in the box below and hit the button: | ||
|
||
<form id="form"> | ||
<input type="text" id="input"> | ||
<input type="submit" id="btn" value="Submit"> | ||
</form> | ||
<pre id="output"> | ||
</pre> | ||
<script src="//code.jquery.com/jquery-3.5.1.min.js"></script> | ||
<script> | ||
$('#form').submit(e => { | ||
if (e.preventDefault) { e.preventDefault(); } | ||
|
||
// Send input to server | ||
$.post('/api/', { input: $('#input').val() }).then(output => { | ||
$('#output').text(JSON.stringify(output, undefined, 2)); | ||
}); | ||
|
||
// Clear text box | ||
$('#input').val(''); | ||
|
||
return false; | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** Configuration file */ | ||
export const applicationMetaData = { | ||
account: "${AWS_ACCOUNT}", | ||
region: "${AWS_REGION}", | ||
|
||
/** Primary Route53 Domain */ | ||
domainName: "web.job4u.vn", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as cdk from '@aws-cdk/core'; | ||
import * as cloudwatch from '@aws-cdk/aws-cloudwatch'; | ||
|
||
/** | ||
* Interface that downstream stacks expect to monitoring subsystem. | ||
*/ | ||
export interface IMonitoring { | ||
addGraphs(title: string, ...widgets: cloudwatch.IWidget[]): void; | ||
} | ||
|
||
/** | ||
* Class with monitoring facilities. | ||
*/ | ||
export class MonitoringStack extends cdk.Stack implements IMonitoring { | ||
private dashboard: cloudwatch.Dashboard; | ||
|
||
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { | ||
super(scope, id, props); | ||
|
||
this.dashboard = new cloudwatch.Dashboard(this, 'Serverless-Webapp Dashboard'); | ||
} | ||
|
||
public addGraphs(title: string, ...widgets: cloudwatch.IWidget[]): void { | ||
this.dashboard.addWidgets(new cloudwatch.TextWidget({ | ||
markdown: `# ${title}`, | ||
})); | ||
this.dashboard.addWidgets(...widgets); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,137 @@ | ||
import * as cdk from '@aws-cdk/core'; | ||
|
||
import * as apigateway from '@aws-cdk/aws-apigateway'; | ||
import * as certmgr from '@aws-cdk/aws-certificatemanager'; | ||
import * as dynamodb from '@aws-cdk/aws-dynamodb'; | ||
import * as cloudwatch from '@aws-cdk/aws-cloudwatch'; | ||
import * as cloudfront from '@aws-cdk/aws-cloudfront'; | ||
import * as s3 from '@aws-cdk/aws-s3'; | ||
import * as s3deploy from '@aws-cdk/aws-s3-deployment'; | ||
import * as origins from '@aws-cdk/aws-cloudfront-origins'; | ||
import * as lambda from '@aws-cdk/aws-lambda'; | ||
import { IMonitoring } from './monitoring-stack'; | ||
|
||
/** ServerlessWebapp StackProps */ | ||
export interface WebAppStackProps extends cdk.StackProps { | ||
|
||
/** | ||
* Domain name for the CloudFront distribution | ||
* (Requires 'certificate' to be set) | ||
* @default - Automatically generated domain name under CloudFront domain | ||
*/ | ||
readonly domainName?: string; | ||
|
||
/** | ||
* Certificate for the CloudFront distribution | ||
* (Requires 'domainName' to be set) | ||
* @default - Automatically generated domain name under CloudFront domain | ||
*/ | ||
readonly certificate?: certmgr.ICertificate; | ||
|
||
/** | ||
* Table to use as backing store for the Lambda Function | ||
*/ | ||
// readonly table: dynamodb.ITable; | ||
|
||
/** | ||
* Where to add metrics | ||
*/ | ||
// readonly monitoring: IMonitoring; | ||
} | ||
|
||
/** | ||
* Serverless Web-App Stack. | ||
*/ | ||
export class ServerlessWebappStack extends cdk.Stack { | ||
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { | ||
constructor(scope: cdk.Construct, id: string, props: WebAppStackProps) { | ||
super(scope, id, props); | ||
|
||
// The code that defines your stack goes here | ||
|
||
/** Step 1. Route53-Domain & ACM-Certificate */ | ||
if (!!props.domainName !== !!props.certificate) { | ||
throw new Error('Supply either both or neither of \'domainName\' and \'certificate\''); | ||
} | ||
|
||
/** | ||
* FIXME #4 | ||
* | ||
* Step 2. Lambda & API-Gateway | ||
*/ | ||
// const func = new lambda.Function(this, 'Lambda', { | ||
// runtime: lambda.Runtime.NODEJS_14_X, | ||
// handler: 'index.handler', | ||
// code: lambda.Code.fromAsset(`${__dirname}/../build/src/lambda-handlers/lambda-handlers`), | ||
// environment: { | ||
// TABLE_ARN: props.table.tableArn | ||
// }, | ||
// timeout: cdk.Duration.seconds(10), | ||
// }); | ||
|
||
// props.table.grantReadWriteData(func); | ||
|
||
// const apiGateway = new apigateway.LambdaRestApi(this, 'API-Gateway', { | ||
// handler: func, | ||
// endpointTypes: [apigateway.EndpointType.REGIONAL], | ||
// }); | ||
|
||
/** | ||
* Step 3. S3 bucket & Cloudfront distribution | ||
*/ | ||
|
||
/** 3.1. S3 bucket to hold the Website with a CloudFront distribution */ | ||
// const bucket = new s3.Bucket(this, 'Bucket', { | ||
// removalPolicy: cdk.RemovalPolicy.DESTROY, | ||
// }); | ||
// const distribution = new cloudfront.Distribution(this, 'Dist', { | ||
// defaultBehavior: { origin: new origins.S3Origin(bucket) }, | ||
// additionalBehaviors: { | ||
// '/api/*': { | ||
// origin: new origins.HttpOrigin(`${apiGateway.restApiId}.execute-api.${this.region}.amazonaws.com`, { | ||
// originPath: `/${apiGateway.deploymentStage.stageName}`, | ||
// }), | ||
// allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL, | ||
// cachePolicy: cloudfront.CachePolicy.CACHING_DISABLED, | ||
// }, | ||
// }, | ||
// defaultRootObject: 'index.html', | ||
// domainNames: props.domainName ? [props.domainName] : undefined, | ||
// certificate: props.certificate, | ||
// }); | ||
|
||
/** 3.2. Upload assets to the S3 bucket */ | ||
// new s3deploy.BucketDeployment(this, 'Deploy', { | ||
// destinationBucket: bucket, | ||
// sources: [s3deploy.Source.asset(`${__dirname}/../../projects/web`)], | ||
// distribution, | ||
// prune: false, | ||
// }); | ||
|
||
/** | ||
* FIXME #5 | ||
* Monitoring: 99% of the requests should be faster than given latency. | ||
* E301: latency 3 seconds | ||
*/ | ||
// props.monitoring.addGraphs('Application', | ||
// new cloudwatch.GraphWidget({ | ||
// title: 'P99 Latencies', | ||
// left: [ | ||
// apiGateway.metricLatency({ statistic: 'P99' }), | ||
// apiGateway.metricIntegrationLatency({ statistic: 'P99' }), | ||
// ], | ||
// }), | ||
|
||
// new cloudwatch.GraphWidget({ | ||
// title: 'Counts vs errors', | ||
// left: [ | ||
// apiGateway.metricCount(), | ||
// ], | ||
// right: [ | ||
// apiGateway.metricClientError(), | ||
// apiGateway.metricServerError(), | ||
// ], | ||
// }), | ||
// ); | ||
|
||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
serverless-webapp/src/lambda-handlers/healthcheck/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* FIXME #4 | ||
* 1. Health Check | ||
* 2. Version | ||
*/ | ||
export async function handler(event: any, _context: any) { | ||
return { | ||
statusCode: 200, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(event, undefined, 2), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters