Created
November 1, 2018 19:37
-
-
Save zigzhang/e4215d5d42e1f9909230db5003b80c1c to your computer and use it in GitHub Desktop.
Serverless & RDBS (Part 1) - Set up AWS RDS Aurora and Lambda with serverless
This file contains hidden or 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
// Require and initialize outside of your main handler | |
const mysql = require('serverless-mysql')({ | |
config: { | |
database: process.env.AURORA_DB_NAME, | |
user: process.env.AURORA_USERNAME, | |
password: process.env.AURORA_PASSWORD, | |
host: process.env.AURORA_HOST, | |
port: process.env.AURORA_PORT | |
} | |
}); | |
// Async query handler | |
exports.sqs = async (event, context) => { | |
// Get your query | |
const record = event.Records[0]; | |
const { query } = JSON.parse(record.body); | |
// Run your query | |
let results = await mysql.query(query); | |
// Run clean up function | |
await mysql.end(); | |
// Return the results | |
return results; | |
}; | |
// Sync query handler | |
exports.query = async (event, context) => { | |
// Get your query | |
const data = event.body ? JSON.parse(event.body) : {}; | |
const { query } = data; | |
// Run your query | |
let results = await mysql.query(query); | |
// Run clean up function | |
await mysql.end(); | |
// Return the results | |
return results; | |
}; |
This file contains hidden or 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
service: aurora | |
provider: | |
name: aws | |
runtime: nodejs8.10 | |
region: us-west-2 | |
stage: development | |
environment: | |
AURORA_HOST: ${self:custom.AURORA.HOST} | |
AURORA_PORT: ${self:custom.AURORA.PORT} | |
AURORA_DB_NAME: ${self:custom.AURORA.DB_NAME} | |
AURORA_USERNAME: ${self:custom.AURORA.USERNAME} | |
AURORA_PASSWORD: ${self:custom.AURORA.PASSWORD} | |
# Define variables here for lisibility | |
custom: | |
AURORA: | |
DB_NAME: aurora${opt:stage, self:provider.stage} | |
USERNAME: master | |
PASSWORD: password | |
HOST: | |
Fn::GetAtt: [AuroraRDSCluster, Endpoint.Address] | |
PORT: | |
Fn::GetAtt: [AuroraRDSCluster, Endpoint.Port] | |
VPC_CIDR: 10 | |
SQS: | |
NAME: aurora-queue-${opt:stage, self:provider.stage} | |
functions: | |
async_query: | |
handler: src/handler.sqs | |
vpc: | |
securityGroupIds: | |
- Fn::GetAtt: ServerlessVPC.DefaultSecurityGroup | |
subnetIds: | |
- Ref: ServerlessSubnetA | |
- Ref: ServerlessSubnetB | |
- Ref: ServerlessSubnetC | |
events: | |
- sqs: | |
arn: | |
Fn::GetAtt: [AuroraSQSQueue, Arn] | |
batchSize: 1 | |
sync_query: | |
handler: src/handler.query | |
vpc: | |
securityGroupIds: | |
- Fn::GetAtt: ServerlessVPC.DefaultSecurityGroup | |
subnetIds: | |
- Ref: ServerlessSubnetA | |
- Ref: ServerlessSubnetB | |
- Ref: ServerlessSubnetC | |
events: | |
- http: | |
path: /query | |
method: post | |
resources: | |
Resources: | |
AsyncQueryQueue: | |
Type: AWS::SQS::Queue | |
Properties: | |
QueueName: ${self:custom.SQS.NAME} | |
# VPC config | |
ServerlessVPC: | |
Type: AWS::EC2::VPC | |
Properties: | |
CidrBlock: ${self:custom.AURORA.VPC_CIDR}.0.0.0/16 | |
EnableDnsSupport: true | |
EnableDnsHostnames: true | |
InstanceTenancy: default | |
ServerlessSubnetA: | |
DependsOn: ServerlessVPC | |
Type: AWS::EC2::Subnet | |
Properties: | |
VpcId: | |
Ref: ServerlessVPC | |
AvailabilityZone: ${self:provider.region}a | |
CidrBlock: ${self:custom.AURORA.VPC_CIDR}.0.0.0/24 | |
ServerlessSubnetB: | |
DependsOn: ServerlessVPC | |
Type: AWS::EC2::Subnet | |
Properties: | |
VpcId: | |
Ref: ServerlessVPC | |
AvailabilityZone: ${self:provider.region}b | |
CidrBlock: ${self:custom.AURORA.VPC_CIDR}.0.1.0/24 | |
ServerlessSubnetC: | |
DependsOn: ServerlessVPC | |
Type: AWS::EC2::Subnet | |
Properties: | |
VpcId: | |
Ref: ServerlessVPC | |
AvailabilityZone: ${self:provider.region}c | |
CidrBlock: ${self:custom.AURORA.VPC_CIDR}.0.2.0/24 | |
# Aurora DB config | |
AuroraSubnetGroup: | |
Type: AWS::RDS::DBSubnetGroup | |
Properties: | |
DBSubnetGroupDescription: "Aurora Subnet Group" | |
SubnetIds: | |
- Ref: ServerlessSubnetA | |
- Ref: ServerlessSubnetB | |
- Ref: ServerlessSubnetC | |
AuroraRDSClusterParameter: | |
Type: AWS::RDS::DBClusterParameterGroup | |
Properties: | |
Description: Parameter group for the Serverless Aurora RDS DB. | |
Family: aurora-mysql5.7 | |
Parameters: | |
character_set_database: "utf32" | |
AuroraRDSInstanceParameter: | |
Type: AWS::RDS::DBParameterGroup | |
Properties: | |
Description: Parameter group for the Serverless Aurora RDS DB. | |
Family: aurora-mysql5.7 | |
Parameters: | |
sql_mode: IGNORE_SPACE | |
max_connections: 100 | |
wait_timeout: 900 | |
interactive_timeout: 900 | |
AuroraRDSCluster: | |
Type: "AWS::RDS::DBCluster" | |
Properties: | |
MasterUsername: ${self:custom.AURORA.USERNAME} | |
MasterUserPassword: ${self:custom.AURORA.PASSWORD} | |
DBSubnetGroupName: | |
Ref: AuroraSubnetGroup | |
Engine: aurora-mysql | |
EngineVersion: "5.7" | |
DatabaseName: ${self:custom.AURORA.DB_NAME} | |
BackupRetentionPeriod: 3 | |
DBClusterParameterGroupName: | |
Ref: AuroraRDSClusterParameter | |
VpcSecurityGroupIds: | |
- Fn::GetAtt: ServerlessVPC.DefaultSecurityGroup | |
AuroraRDSInstance: | |
Type: "AWS::RDS::DBInstance" | |
Properties: | |
DBInstanceClass: db.t2.medium | |
DBSubnetGroupName: | |
Ref: AuroraSubnetGroup | |
Engine: aurora-mysql | |
EngineVersion: "5.7" | |
PubliclyAccessible: false | |
DBParameterGroupName: | |
Ref: AuroraRDSInstanceParameter | |
DBClusterIdentifier: | |
Ref: AuroraRDSCluster |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can I fix this error when trying to deploy:
Your application definition - serverless.yml contains value
AWS::EC2::VPC
for/resources/Resources/ServerlessVPC/Type
that violates one of our rules. Please refer to documentation for valid values for/resources/Resources/ServerlessVPC/Type
.