Skip to content

Instantly share code, notes, and snippets.

@nathanmalishev
Last active August 13, 2022 14:21
Show Gist options
  • Save nathanmalishev/65b3f16c5acb0dc668453675b74298f3 to your computer and use it in GitHub Desktop.
Save nathanmalishev/65b3f16c5acb0dc668453675b74298f3 to your computer and use it in GitHub Desktop.
# 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
@nathanmalishev
Copy link
Author

nathanmalishev commented Jun 3, 2018

@afeather20s
Copy link

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