/
template.yaml
200 lines (189 loc) · 5.28 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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Code to deploy aws infrastructure for 2 microservices
focus in order and payment services
# Global Parameters to easy changes in future
Parameters:
EventBusName:
Type: String
Description: Event bus name
Default: 'default'
Globals:
# base configuration for all lambdas
Function:
Runtime: go1.x
Timeout: 5
Architectures:
- x86_64
Resources:
# Configuration for API
APIGatewayApi:
Type: AWS::Serverless::Api
Properties:
Name: 'microservices_API'
StageName: Dev
Models:
Order:
type: object
required:
- user_id
- item
- quantity
- total_price
properties:
user_id:
type: string
item:
type: string
quantity:
type: integer
total_price:
type: number
Payment:
type: object
required:
- order_id
- status
properties:
order_id:
Type: string
status:
type: string
# Deploy all the functions
OrderApiFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: Order_API_Lambda
CodeUri: services/functions/order_api
Handler: order_api_lambda
# Configuration of api method and access to lambda
Events:
ApiEvent:
Type: Api
Properties:
RestApiId:
Ref: APIGatewayApi
Path: /order
Method: POST
RequestModel:
Model: Order
Required: true
ValidateBody: true
# Policy statements for evenBridge use
Policies:
- EventBridgePutEventsPolicy:
EventBusName: !Ref EventBusName
- DynamoDBCrudPolicy:
TableName: !Ref OrderTable
# Environment variables
Environment:
Variables:
EVENT_BUS_NAME: !Ref EventBusName
TABLE_NAME: !Ref OrderTable
PaymentApiFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: Payment_API_Lambda
CodeUri: services/functions/payment_api
Handler: payment_api_lambda
# Configuration of api method and access to lambda
Events:
ApiEvent:
Type: Api
Properties:
RestApiId:
Ref: APIGatewayApi
Path: /payment
Method: POST
RequestModel:
Model: Payment
Required: true
ValidateBody: true
# Policy statements for evenBridge use
Policies:
- EventBridgePutEventsPolicy:
EventBusName: !Ref EventBusName
- DynamoDBCrudPolicy:
TableName: !Ref PaymentTable
# Environment variables
Environment:
Variables:
EVENT_BUS_NAME: !Ref EventBusName
TABLE_NAME: !Ref PaymentTable
OrderEventFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: Order_Event_Lambda
CodeUri: services/functions/order_event
Handler: order_event_lambda
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref OrderTable
Events:
Trigger:
Type: EventBridgeRule
Properties:
RuleName: payment_to_order_rule
Pattern:
source:
- custom.PaymentApiFunction
RetryPolicy:
MaximumRetryAttempts: 5
MaximumEventAgeInSeconds: 120
# Environment variables
Environment:
Variables:
TABLE_NAME: !Ref OrderTable
PaymentEventFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: Payment_Event_Lambda
CodeUri: services/functions/payment_event
Handler: payment_event_lambda
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref PaymentTable
Events:
Trigger:
Type: EventBridgeRule
Properties:
RuleName: order_to_payment_rule
Pattern:
source:
- custom.OrderApiFunction
RetryPolicy:
MaximumRetryAttempts: 5
MaximumEventAgeInSeconds: 120
# Environment variables
Environment:
Variables:
TABLE_NAME: !Ref PaymentTable
# Simple tables in DynamoDB
OrderTable:
Type: AWS::Serverless::SimpleTable
Properties:
TableName: OrderTable
PrimaryKey:
Name: OrderID
Type: String
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
PaymentTable:
Type: AWS::Serverless::SimpleTable
Properties:
TableName: PaymentTable
PrimaryKey:
Name: OrderID
Type: String
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
Outputs:
OrderApi:
Description: 'API Gateway endpoint URL for dev stage for order function'
Value: !Sub 'https://${APIGatewayApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/Dev/order/'
paymentApi:
Description: 'API Gateway endpoint URL for dev stage for payment function'
Value: !Sub 'https://${APIGatewayApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/Dev/payment/'