Created
July 18, 2020 23:02
-
-
Save isbkch/9e95f50201b0f6561e4ca90eff7a026b to your computer and use it in GitHub Desktop.
A serverless.yml file configuring a AWS ElastiCache redis instance that is accessible by all AWS Lambda functions deployed by this serverless function.
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
service: my-service | |
provider: | |
name: aws | |
runtime: nodejs12.x | |
stage: ${opt:stage, 'dev'} | |
environment: | |
REDIS_HOST: | |
"Fn::GetAtt": [ElasticCacheCluster, RedisEndpoint.Address] | |
functions: | |
trigger: | |
handler: src/serverless.trigger | |
vpc: | |
securityGroupIds: | |
- "Fn::GetAtt": ServerlessSecurityGroup.GroupId | |
subnetIds: | |
- Ref: PrivateSubnetA | |
- Ref: PrivateSubnetB | |
events: | |
- http: POST /trigger | |
status: | |
handler: src/serverless.getStatus | |
vpc: | |
securityGroupIds: | |
- "Fn::GetAtt": ServerlessSecurityGroup.GroupId | |
subnetIds: | |
- Ref: PrivateSubnetA | |
- Ref: PrivateSubnetB | |
events: | |
- http: GET /status | |
transform: | |
handler: src/serverless.transform | |
vpc: | |
securityGroupIds: | |
- "Fn::GetAtt": ServerlessSecurityGroup.GroupId | |
subnetIds: | |
- Ref: PrivateSubnetA | |
- Ref: PrivateSubnetB | |
resources: | |
Resources: | |
VPC: | |
Type: AWS::EC2::VPC | |
Properties: | |
CidrBlock: "10.0.0.0/16" | |
IP: | |
Type: AWS::EC2::EIP | |
Properties: | |
Domain: vpc | |
InternetGateway: | |
Type: AWS::EC2::InternetGateway | |
VPCGatewayAttachment: | |
Type: AWS::EC2::VPCGatewayAttachment | |
Properties: | |
VpcId: | |
Ref: VPC | |
InternetGatewayId: | |
Ref: InternetGateway | |
NatGateway: | |
Type: AWS::EC2::NatGateway | |
Properties: | |
AllocationId: | |
Fn::GetAtt: | |
- IP | |
- AllocationId | |
SubnetId: | |
Ref: PublicSubnetA | |
PrivateSubnetA: | |
DependsOn: VPC | |
Type: AWS::EC2::Subnet | |
Properties: | |
VpcId: | |
Ref: VPC | |
AvailabilityZone: ${self:provider.region}a | |
CidrBlock: "10.0.1.0/24" | |
PrivateSubnetA: | |
Type: AWS::EC2::Subnet | |
Properties: | |
VpcId: | |
Ref: VPC | |
AvailabilityZone: !Sub "${AWS::Region}a" | |
CidrBlock: "10.0.1.0/24" | |
PublicSubnetA: | |
Type: AWS::EC2::Subnet | |
Properties: | |
VpcId: | |
Ref: VPC | |
AvailabilityZone: !Sub "${AWS::Region}a" | |
CidrBlock: "10.0.2.0/24" | |
PrivateSubnetB: | |
Type: AWS::EC2::Subnet | |
Properties: | |
VpcId: | |
Ref: VPC | |
AvailabilityZone: !Sub "${AWS::Region}b" | |
CidrBlock: "10.0.3.0/24" | |
PublicSubnetB: | |
Type: AWS::EC2::Subnet | |
Properties: | |
VpcId: | |
Ref: VPC | |
AvailabilityZone: !Sub "${AWS::Region}b" | |
CidrBlock: "10.0.4.0/24" | |
PrivateRouteTable: | |
Type: AWS::EC2::RouteTable | |
Properties: | |
VpcId: | |
Ref: VPC | |
PrivateRoute: | |
Type: AWS::EC2::Route | |
Properties: | |
RouteTableId: | |
Ref: PrivateRouteTable | |
DestinationCidrBlock: 0.0.0.0/0 | |
NatGatewayId: | |
Ref: NatGateway | |
PublicRouteTable: | |
Type: AWS::EC2::RouteTable | |
Properties: | |
VpcId: | |
Ref: VPC | |
PublicRoute: | |
Type: AWS::EC2::Route | |
Properties: | |
RouteTableId: | |
Ref: PublicRouteTable | |
DestinationCidrBlock: 0.0.0.0/0 | |
GatewayId: | |
Ref: InternetGateway | |
SubnetRouteTableAssociationLambdaPrivateA: | |
Type: AWS::EC2::SubnetRouteTableAssociation | |
Properties: | |
SubnetId: | |
Ref: PrivateSubnetA | |
RouteTableId: | |
Ref: PrivateRouteTable | |
SubnetRouteTableAssociationLambdaPublicA: | |
Type: AWS::EC2::SubnetRouteTableAssociation | |
Properties: | |
SubnetId: | |
Ref: PublicSubnetA | |
RouteTableId: | |
Ref: PublicRouteTable | |
SubnetRouteTableAssociationLambdaPrivateB: | |
Type: AWS::EC2::SubnetRouteTableAssociation | |
Properties: | |
SubnetId: | |
Ref: PrivateSubnetB | |
RouteTableId: | |
Ref: PrivateRouteTable | |
SubnetRouteTableAssociationLambdaPublicB: | |
Type: AWS::EC2::SubnetRouteTableAssociation | |
Properties: | |
SubnetId: | |
Ref: PublicSubnetB | |
RouteTableId: | |
Ref: PublicRouteTable | |
ServerlessSecurityGroup: | |
DependsOn: VPC | |
Type: AWS::EC2::SecurityGroup | |
Properties: | |
GroupDescription: SecurityGroup for Serverless Functions | |
VpcId: | |
Ref: VPC | |
ServerlessStorageSecurityGroup: | |
DependsOn: VPC | |
Type: AWS::EC2::SecurityGroup | |
Properties: | |
GroupDescription: Ingress for Redis Cluster | |
VpcId: | |
Ref: VPC | |
SecurityGroupIngress: | |
- IpProtocol: tcp | |
FromPort: '6379' | |
ToPort: '6379' | |
SourceSecurityGroupId: | |
Ref: ServerlessSecurityGroup | |
ServerlessCacheSubnetGroup: | |
Type: AWS::ElastiCache::SubnetGroup | |
Properties: | |
Description: "Cache Subnet Group" | |
SubnetIds: | |
- Ref: PrivateSubnetA | |
ElasticCacheCluster: | |
DependsOn: ServerlessStorageSecurityGroup | |
Type: AWS::ElastiCache::CacheCluster | |
Properties: | |
AutoMinorVersionUpgrade: true | |
Engine: redis | |
CacheNodeType: ${self:custom.config.CACHE_INSTANCE_SIZE} | |
NumCacheNodes: 1 | |
VpcSecurityGroupIds: | |
- "Fn::GetAtt": ServerlessStorageSecurityGroup.GroupId | |
CacheSubnetGroupName: | |
Ref: ServerlessCacheSubnetGroup | |
custom: | |
config: | |
CACHE_INSTANCE_SIZE: cache.t3.micro |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment