Hi. New-ish to AWS configuration.
Edited: The lambda’s inside the VPC also time out and do not return the database results. I don’t know how to fix my setup as I’m unfamiliar with the concepts here.
I can connect to my RDS Postgres instance when running it from Lambda. I would like this database to be accessible outside of the VPC as well, so I can connect to it with an external client (like PSQL). I’m not sure how to set this up using the Cloudformation config.
Can anyone help me out with this?
The serverless.yml
file:
provider:
name: aws
runtime: nodejs6.10
# you can overwrite defaults here
stage: ${self:custom.currentStage}
region: ap-southeast-2
environment:
DATABASE_URL:
Fn::Join: [":", [Fn::GetAtt: [ServerlessRDSCluster, Endpoint.Address], Fn::GetAtt: [ServerlessRDSCluster, Endpoint.Port]]]
# you can add statements to the Lambda function's IAM Role here
iamRoleStatements:
- Effect: "Allow"
Action:
- "ec2:CreateNetworkInterface"
- "ec2:DescribeNetworkInterfaces"
- "ec2:DeleteNetworkInterface"
Resource: "*"
functions:
authWithKey:
handler: handler.authWithKey
vpc:
securityGroupIds:
- "Fn::GetAtt": ServerlessSecurityGroup.GroupId
subnetIds:
- Ref: ServerlessSubnetA
- Ref: ServerlessSubnetB
- Ref: ServerlessSubnetC
apiKeyEndpoint:
handler: handler.apiKeyEndpoint
vpc:
securityGroupIds:
- "Fn::GetAtt": ServerlessSecurityGroup.GroupId
subnetIds:
- Ref: ServerlessSubnetA
- Ref: ServerlessSubnetB
- Ref: ServerlessSubnetC
events:
- http:
path: apiKey
method: get
integration: lambda
private: true
request:
parameters:
headers:
'X-Api-Key': true
authorizer:
name: authWithKey
identitySource: method.request.header.X-Api-Key
cors:
origins:
- '*'
headers:
- Content-Type
- X-Amz-Date
- Authorization
- X-Api-Key
- X-Amz-Security-Token
# cloud formation stuff
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
- IpProtocol: tcp
FromPort: '11211'
ToPort: '11211'
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:
DBName: "telematics${self:custom.currentStage}"
AllocatedStorage: 10
DBInstanceClass: "db.t2.micro"
Engine: "postgres"
EngineVersion: "9.6.2"
MasterUsername: "<user>"
MasterUserPassword: "<pass>"
VPCSecurityGroups:
- "Fn::GetAtt": ServerlessStorageSecurityGroup.GroupId
DBSubnetGroupName:
Ref: ServerlessRDSSubnetGroup
DeletionPolicy: "Snapshot"