Skip to content

Commit

Permalink
added a database generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Janowski committed Oct 14, 2018
1 parent fb3d8d7 commit 9e4dae3
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 9 deletions.
8 changes: 8 additions & 0 deletions db.init.sql
@@ -0,0 +1,8 @@
CREATE TABLE paths (
id BIGINT PRIMARY KEY NOT NULL,
ip BIGINT NOT NULL,
protocol VARCHAR(5) NOT NULL,
path VARCHAR(100) NOT NULL,
content VARCHAR(10000),
updated timestamp
);
2 changes: 1 addition & 1 deletion gateway.js
Expand Up @@ -9,7 +9,7 @@ const BODY_SCHEMA = Joi.object().keys({
from: Joi.string().regex(IP_REGEX).required(),
to: Joi.string().regex(IP_REGEX).required(),
regex: Joi.string().max(100),
path: Joi.string().regex(/^[\w\-!@#\$\%\^\&\*\.?=\|\,<>\/]+$/).required()
path: Joi.string().regex(/^[\w\-!@#\$\%\^\&\*\.?=\|\,<>\/]+$/).max(100).required()
});

const processErrorResponse = ex => {
Expand Down
32 changes: 24 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -14,6 +14,7 @@
"aws-sdk": "^2.329.0",
"joi": "^13.7.0",
"lodash": "^4.17.11",
"mysql": "^2.16.0",
"request": "^2.88.0",
"request-promise-native": "^1.0.5"
},
Expand Down
97 changes: 97 additions & 0 deletions serverless.yml
Expand Up @@ -24,6 +24,13 @@ provider:
environment:
MAX_IPS_TO_SCAN_PER_INSTANCE: 25000
MAX_JOBS_TO_CREATE: 10000
DB_NAME: ${self:custom.dbName}
DB_USER: ${self:custom.dbUser}
DB_PASSWORD: ${self:custom.dbUser}
DB_ADDRESS:
Fn::GetAtt: [ WebCache, Endpoint.Address ]
DB_PORT:
Fn::GetAtt: [ WebCache, Endpoint.Port ]
JOB_QUEUE:
Ref: JobQueue

Expand All @@ -33,13 +40,26 @@ package:
- package-lock.json
- jobExecutor.js
- gateway.js
- dbInit.js
- lib/**

custom:
dbName: ${opt:dbname, 'pathfinder'}
dbUser: ${opt:dbuser, 'root'}
dbPassword: ${opt:dbpassword, 'SecretRootPassword'}
dbPublic: ${opt:dbpublic, 'true'}

functions:
jobExecutor:
handler: jobExecutor.execute
memorySize: 192
timeout: 300
vpc:
securityGroupIds:
- Ref: VpcSecurityGroup
subnetIds:
- Ref: SubnetA
- Ref: SubnetB
environment:
NOTIFICATION_SNS_TOPIC:
Ref: NotificationSNSTopic
Expand All @@ -52,6 +72,12 @@ functions:
handler: gateway.find
timeout: 10
memorySize: 128
vpc:
securityGroupIds:
- Ref: VpcSecurityGroup
subnetIds:
- Ref: SubnetA
- Ref: SubnetB
events:
- http:
path: /find
Expand Down Expand Up @@ -93,6 +119,65 @@ resources:
Resource: "*"
Queues:
- Ref: JobQueue

WebCache:
Type: AWS::RDS::DBInstance
Properties:
DBName: ${self:custom.dbName}
DBInstanceIdentifier: pathfinder2-${self:provider.stage}
AllocatedStorage: '5'
DBInstanceClass: db.t2.medium
Engine: MySQL
MasterUsername: ${self:custom.dbUser}
MasterUserPassword: ${self:custom.dbPassword}
PubliclyAccessible: ${self:custom.dbPublic}
MultiAZ: true
Port: 3306
DBSubnetGroupName:
Ref:
DbSubnetGroup
VPCSecurityGroups:
- Ref:
VpcSecurityGroup
DeletionPolicy: Delete

DbSubnetGroup:
Type: AWS::RDS::DBSubnetGroup
Properties:
DBSubnetGroupDescription: "Subnet group for database access"
SubnetIds:
- Ref:
SubnetA
- Ref:
SubnetB

SubnetA:
Type: AWS::EC2::Subnet
Properties:
CidrBlock: 10.0.4.0/24
AvailabilityZone: ${self:provider.region}a
VpcId:
Ref: BackendVpc

SubnetB:
Type: AWS::EC2::Subnet
Properties:
CidrBlock: 10.0.3.0/24
AvailabilityZone: ${self:provider.region}b
VpcId:
Ref: BackendVpc

BackendVpc:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16

VpcSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: "Security group for RDS DB Instance."
VpcId:
Ref: BackendVpc
Outputs:
NotificationSNSTopic:
Description: "SNS topic that the events will be published to, once a matching response is found"
Expand All @@ -102,3 +187,15 @@ resources:
Description: "SQS queue that contains all the search jobs to be done"
Value:
Ref: JobQueue
DatabaseId:
Description: "Database ID"
Value:
Ref: WebCache
DatabaseHost:
Description: "Database Host"
Value:
Fn::GetAtt: [ WebCache, Endpoint.Address ]
DatabasePort:
Description: "Database port"
Value:
Fn::GetAtt: [ WebCache, Endpoint.Port ]

0 comments on commit 9e4dae3

Please sign in to comment.