-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.yaml
186 lines (166 loc) · 4.81 KB
/
template.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Rails WebSockets on Lambda
Parameters:
RailsEnv:
Type: String
Default: staging
AllowedValues:
- staging
- production
Globals:
Function:
Architectures:
- arm64
AutoPublishAlias: live
DeploymentPreference:
Type: AllAtOnce
Environment:
Variables:
RAILS_ENV: !Ref RailsEnv
DATABASE_URL: x-crypteia-ssm:/websocket-demo/env/DATABASE_URL
LAMBDA_CABLE_CONNECTIONS_TABLE: !Ref WSTableConnections
LAMBDA_CABLE_SUBSCRIPTIONS_TABLE: !Ref WSTableSubscriptions
LAMBDA_CABLE_LOG_LEVEL: debug
Timeout: 30
Resources:
RailsLambda:
Type: AWS::Serverless::Function
Metadata:
DockerContext: .
Dockerfile: Dockerfile
DockerTag: web
Properties:
FunctionUrlConfig:
AuthType: NONE
MemorySize: 1792
PackageType: Image
Policies:
- Statement:
- Effect: Allow
Action: ["ssm:Get*", "ssm:Describe*"]
Resource:
- !Sub arn:aws:ssm:*:${AWS::AccountId}:parameter/websocket-demo/env/*
WSTableConnections:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: connection_id
AttributeType: S
- AttributeName: connection_identifier
AttributeType: S
BillingMode: PAY_PER_REQUEST
GlobalSecondaryIndexes:
- IndexName: connection_identifier_index
KeySchema:
- AttributeName: connection_identifier
KeyType: HASH
Projection:
ProjectionType: ALL
KeySchema:
- AttributeName: connection_id
KeyType: HASH
TimeToLiveSpecification:
AttributeName: ttl
Enabled: true
WSTableSubscriptions:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: identifier
AttributeType: S
- AttributeName: connection_id
AttributeType: S
BillingMode: PAY_PER_REQUEST
GlobalSecondaryIndexes:
- IndexName: connection_id_index
KeySchema:
- AttributeName: connection_id
KeyType: HASH
Projection:
ProjectionType: ALL
KeySchema:
- AttributeName: identifier
KeyType: HASH
WSConnector:
Type: AWS::Serverless::Connector
Properties:
Source: { Id: RailsLambda }
Destination:
- Id: WSTableConnections
- Id: WSTableSubscriptions
Permissions: [Read, Write]
WSApi:
Type: AWS::ApiGatewayV2::Api
Properties:
Name: !Ref RailsLambda
ProtocolType: WEBSOCKET
RouteSelectionExpression: "$request.body.message"
WSDeployment:
Type: AWS::ApiGatewayV2::Deployment
DependsOn:
- WSConnectRoute
- WSDefaultRoute
- WSDisconnectRoute
Properties:
ApiId: !Ref WSApi
WSStage:
Type: AWS::ApiGatewayV2::Stage
Properties:
StageName: cable
DeploymentId: !Ref WSDeployment
ApiId: !Ref WSApi
WSPolicy:
Type: AWS::IAM::Policy
DependsOn: RailsLambdaRole
Properties:
PolicyName: !Sub "${RailsLambda}-ws-policy"
PolicyDocument:
Statement:
- Effect: Allow
Action: ["execute-api:Invoke", "execute-api:ManageConnections"]
Resource:
- !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${WSApi}/*
Roles:
- !Ref RailsLambdaRole
WSConnectRoute:
Type: AWS::ApiGatewayV2::Route
Properties:
ApiId: !Ref WSApi
AuthorizationType: NONE
RouteKey: "$connect"
OperationName: WSConnectRoute
Target: !Sub integrations/${WSIntegration}
WSDefaultRoute:
Type: AWS::ApiGatewayV2::Route
Properties:
ApiId: !Ref WSApi
AuthorizationType: NONE
RouteKey: "$default"
OperationName: WSDefaultRoute
Target: !Sub integrations/${WSIntegration}
WSDisconnectRoute:
Type: AWS::ApiGatewayV2::Route
Properties:
ApiId: !Ref WSApi
AuthorizationType: NONE
RouteKey: "$disconnect"
OperationName: WSDisconnectRoute
Target: !Sub integrations/${WSIntegration}
WSIntegration:
Type: AWS::ApiGatewayV2::Integration
Properties:
ApiId: !Ref WSApi
IntegrationType: AWS_PROXY
IntegrationUri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${RailsLambda.Arn}:live/invocations
WSPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !Sub "${RailsLambda}:live"
Principal: apigateway.amazonaws.com
SourceArn: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${WSApi}/*
Outputs:
RailsLambdaUrl:
Description: Lambda Function URL
Value: !GetAtt RailsLambdaUrl.FunctionUrl