Skip to content

Instantly share code, notes, and snippets.

@tlaitinen
Created May 2, 2018 14:19
Show Gist options
  • Select an option

  • Save tlaitinen/99db4e336d2ef97d27f2b7762071d987 to your computer and use it in GitHub Desktop.

Select an option

Save tlaitinen/99db4e336d2ef97d27f2b7762071d987 to your computer and use it in GitHub Desktop.
service: backend
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: eu-west-1
# you can add statements to the Lambda function's IAM Role here
iamRoleStatements:
- Effect: "Allow"
Action:
- ec2:CreateNetworkInterface
- ec2:DescribeNetworkInterfaces
- ec2:DeleteNetworkInterface
Resource: "*"
# you can define service wide environment variables here
environment:
postgresEndpoint:
"Fn::Join": [":", ["Fn::GetAtt": [ServerlessRDSCluster, Endpoint.Address], "Fn::GetAtt": [ServerlessRDSCluster, Endpoint.Port]]]
postgresDatabase: "backend"
postgresUser: "master"
postgresPassword: ${env:POSTGRES_PASSWORD}
functions:
index:
handler: index.handler
vpc:
securityGroupIds:
- "Fn::GetAtt": ServerlessSecurityGroup.GroupId
subnetIds:
- Ref: ServerlessSubnetA
- Ref: ServerlessSubnetB
- Ref: ServerlessSubnetC
events:
- http:
path: /
method: ANY
cors: true
- http:
path: /{any+}
method: ANY
cors: true
resources:
Resources:
ServerlessVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: "10.0.0.0/16"
ServerlessSubnetA:
DependsOn: ServerlessVPC
Type: AWS::EC2::Subnet
Properties:
VpcId:
Ref: ServerlessVPC
AvailabilityZone: ${self:provider.region}a
CidrBlock: "10.0.0.0/24"
ServerlessSubnetB:
DependsOn: ServerlessVPC
Type: AWS::EC2::Subnet
Properties:
VpcId:
Ref: ServerlessVPC
AvailabilityZone: ${self:provider.region}b
CidrBlock: "10.0.1.0/24"
ServerlessSubnetC:
DependsOn: ServerlessVPC
Type: AWS::EC2::Subnet
Properties:
VpcId:
Ref: ServerlessVPC
AvailabilityZone: ${self:provider.region}c
CidrBlock: "10.0.2.0/24"
ServerlessSecurityGroup:
DependsOn: ServerlessVPC
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: SecurityGroup for Serverless Functions
VpcId:
Ref: ServerlessVPC
ServerlessStorageSecurityGroup:
DependsOn: ServerlessVPC
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Ingress for RDS Instance
VpcId:
Ref: ServerlessVPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '5432'
ToPort: '5432'
SourceSecurityGroupId:
Ref: ServerlessSecurityGroup
ServerlessRDSSubnetGroup:
Type: AWS::RDS::DBSubnetGroup
Properties:
DBSubnetGroupDescription: "RDS Subnet Group"
SubnetIds:
- Ref: ServerlessSubnetA
- Ref: ServerlessSubnetB
- Ref: ServerlessSubnetC
ServerlessRDSCluster:
DependsOn: ServerlessStorageSecurityGroup
Type: AWS::RDS::DBInstance
Properties:
Engine: Postgres
EngineVersion: 10.3
DBName: backend
MasterUsername: master
MasterUserPassword: ${env:POSTGRES_PASSWORD}
DBInstanceClass: db.t2.micro
PubliclyAccessible: true
AllocatedStorage: 20
VPCSecurityGroups:
- "Fn::GetAtt": ServerlessStorageSecurityGroup.GroupId
DBSubnetGroupName:
Ref: ServerlessRDSSubnetGroup
plugins:
- serverless-offline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment