-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yaml
240 lines (222 loc) · 6.57 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
AWSTemplateFormatVersion: "2010-09-09"
Description: Practice 1
Transform:
- AWS::Serverless-2016-10-31
Globals:
Function:
Runtime: nodejs14.x
Timeout: 30
MemorySize: 1024
Environment:
Variables:
USERNAME: Jordi
NODE_OPTIONS: --enable-source-maps
AWS_NODEJS_CONNECTION_REUSE_ENABLED: 1
Resources:
###########################################
# Practice 2 #
###########################################
Dynamodb:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub '${AWS::StackName}-dynamodb-table'
AttributeDefinitions:
- AttributeName: id # Partition key
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
LambdaWriteDynamodbRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub '${AWS::StackName}-LambdaWriteDynamodbRole'
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
lambda.amazonaws.com
Action:
sts:AssumeRole
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: !Sub '${AWS::StackName}-LambdaWriteDynamodbPolicy'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- dynamodb:PutItem
Resource:
- !GetAtt Dynamodb.Arn
LambdaDeleteDynamodbRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub '${AWS::StackName}-LambdaDeleteDynamodbRole'
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
lambda.amazonaws.com
Action:
sts:AssumeRole
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: !Sub '${AWS::StackName}-LambdaDeleteDynamodbPolicy'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- dynamodb:DeleteItem
Resource:
- !GetAtt Dynamodb.Arn
LambdaGetDynamodbRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub '${AWS::StackName}-LambdaGetDynamodbRole'
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
lambda.amazonaws.com
Action:
sts:AssumeRole
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: !Sub '${AWS::StackName}-LambdaGetDynamodbPolicy'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- dynamodb:GetItem
Resource:
- !GetAtt Dynamodb.Arn
PutLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub '${AWS::StackName}-put-dynamodb-lambda'
Handler: put.handler
CodeUri: dist/practice2
Role: !GetAtt LambdaWriteDynamodbRole.Arn
DeleteLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub '${AWS::StackName}-delete-dynamodb-lambda'
Handler: delete.handler
CodeUri: dist/practice2
Role: !GetAtt LambdaDeleteDynamodbRole.Arn
GetLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub '${AWS::StackName}-get-dynamodb-lambda'
Handler: get.handler
CodeUri: dist/practice2
Role: !GetAtt LambdaGetDynamodbRole.Arn
###########################################
# Practice 3 #
###########################################
Player1Lambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub '${AWS::StackName}-player-1-lambda'
CodeUri: dist/practice3
Handler: player1.handler
Runtime: nodejs12.x
Policies:
- Statement:
- Effect: Allow
Resource: '*'
Action:
- events:PutEvents
Player2Lambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub '${AWS::StackName}-player-2-lambda'
CodeUri: dist/practice3
Handler: player2.handler
Runtime: nodejs12.x
Policies:
- Statement:
- Effect: Allow
Resource: '*'
Action:
- events:PutEvents
# create the eventbus for ping pong
pabloFraileEventBus:
Type: AWS::Events::EventBus
Properties:
Name: !Sub "${AWS::StackName}-event-bus"
# Define the event pattern, specify the patterns, and the bus used
Player1EventRule:
Type: AWS::Events::Rule
Properties:
Description: "Player 1 Event Rule"
EventBusName: !Sub "${AWS::StackName}-event-bus"
EventPattern:
source:
- "player2"
State: "ENABLED"
Targets:
-
Arn:
Fn::GetAtt:
- "Player1Lambda"
- "Arn"
Id: "player1ServiceTarget"
# Resource that grants permission to EventBridge to invoke the target.
Player1LambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: "Player1Lambda"
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn:
Fn::GetAtt:
- "Player1EventRule"
- "Arn"
# Define the event pattern, specify the patterns, and the bus used
Player2EventRule:
Type: AWS::Events::Rule
Properties:
Description: "Player 2 Event Rule"
EventBusName: !Sub "${AWS::StackName}-event-bus"
EventPattern:
source:
- "player1"
State: "ENABLED"
Targets:
-
Arn:
Fn::GetAtt:
- "Player2Lambda"
- "Arn"
Id: "player1ServiceTarget"
# Resource that grants permission to EventBridge to invoke the target.
Player2LambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: "Player2Lambda"
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn:
Fn::GetAtt:
- "Player2EventRule"
- "Arn"