Last active
August 13, 2022 14:21
-
-
Save nathanmalishev/65b3f16c5acb0dc668453675b74298f3 to your computer and use it in GitHub Desktop.
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 template is a Cloud Formation template, that has AWS Sam seamlessly mixed in | |
# It is extremely powerful as you will see! | |
# This is a template of Lambda & RDS, publically available (so no private VPC) | |
AWSTemplateFormatVersion: 2010-09-09 | |
Transform: AWS::Serverless-2016-10-31 # This is to tell cloudformation we need to transform this template, as it has SAM! | |
Description: Severless set up with an RDS | |
######## ### ######## ### ## ## | |
## ## ## ## ## ## ## ## ### ### | |
## ## ## ## ## ## ## ## #### #### | |
######## ## ## ######## ## ## ## ### ## | |
## ######### ## ## ######### ## ## | |
## ## ## ## ## ## ## ## ## | |
## ## ## ## ## ## ## ## ## | |
Parameters: | |
dbUserPassword: | |
NoEcho: true | |
Type: String | |
dbUserName: | |
NoEcho: true | |
Type: String | |
DbSize: | |
Type: String | |
Default: db.t2.small | |
## Globals is unique to AWS Sam and is not featured in Cloudformation | |
## These Globals under Function are passed to each AWS::Serverless::Function as properties | |
Globals: | |
Function: | |
Runtime: go1.x | |
Timeout: 10 | |
Tracing: Active | |
Tags: | |
demo: true | |
Environment: | |
Variables: | |
MY_SQL_URI: !Join ['', [!Ref dbUserName, ':', !Ref dbUserPassword, '@(', !GetAtt DatabaseCluster.Endpoint.Address, ':', !GetAtt DatabaseCluster.Endpoint.Port, ')/mydb']] | |
# The powerful intrinsic function Join, here we join a db connection string and make it a global | |
# So every lambda function can access it right away! | |
## ### ## ## ######## ######## ### | |
## ## ## ### ### ## ## ## ## ## ## | |
## ## ## #### #### ## ## ## ## ## ## | |
## ## ## ## ### ## ######## ## ## ## ## | |
## ######### ## ## ## ## ## ## ######### | |
## ## ## ## ## ## ## ## ## ## ## | |
######## ## ## ## ## ######## ######## ## ## | |
# The following sections is AWS Sam | |
# It ties together Lambda functions so they are easy to deploy as you will see | |
Resources: | |
Write: | |
Type: AWS::Serverless::Function # This type Serverless::Function is unique to Sam | |
Properties: | |
Policies: | |
- AWSXrayWriteOnlyAccess | |
Handler: dist/handler/write # Ofcourse we tell need to mention where the code is | |
Events: # It lets you define events that trigger your lambda function | |
GetEvent: | |
Type: Api # This is an API Gateway event, it automically handles all in & outs of setting up an API gateway with Lambda! | |
Properties: | |
Path: /write | |
Method: get | |
Read: | |
Type: AWS::Serverless::Function | |
Properties: | |
Policies: | |
- AWSXrayWriteOnlyAccess | |
Handler: dist/handler/read | |
Events: | |
GetEvent: | |
Type: Api | |
Properties: | |
Path: /read | |
Method: get | |
######## ######## ###### | |
## ## ## ## ## ## | |
## ## ## ## ## | |
######## ## ## ###### | |
## ## ## ## ## | |
## ## ## ## ## ## | |
## ## ######## ###### | |
PublicDatabaseSubnetGroup: | |
Type: AWS::RDS::DBSubnetGroup | |
Properties: | |
DBSubnetGroupDescription: CloudFormation managed DB subnet group. | |
SubnetIds: !Split [ ',', !ImportValue LambdaVPCExperiementPublicSubnets] | |
# More powerful intrinsic functions, we import the Public Subnets from another stack & split it into a list like SubnetIds require | |
DatabaseCluster: | |
Type: AWS::RDS::DBCluster | |
Properties: | |
MasterUsername: !Ref dbUserName | |
MasterUserPassword: !Ref dbUserPassword | |
Engine: aurora | |
DBSubnetGroupName: !Ref PublicDatabaseSubnetGroup | |
DatabasePrimaryInstance: | |
Type: AWS::RDS::DBInstance | |
Properties: | |
Engine: aurora | |
DBClusterIdentifier: !Ref "DatabaseCluster" | |
DBInstanceClass: !Ref DbSize | |
DBSubnetGroupName: !Ref PublicDatabaseSubnetGroup | |
PubliclyAccessible: true |
Does this handle updates as well or does it blow the rds instance away?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check out https://github.com/nathanmalishev/go-lambda-vpc-experiment for the up to date code
Or https://medium.freecodecamp.org/lambda-vpc-cold-starts-a-latency-killer-5408323278dd for the accompanying article