1
1
# Building the Wild Rydes Backend Components Layer
2
2
3
3
In this module, you will deploy backend application components to AWS. These
4
- backend components include several AWS Lambda functions, two API Gateway Endpoints and two
4
+ backend components include several AWS Lambda functions, two API Gateway Endpoints and
5
5
DynamoDB tables. You will also create the IAM polices and roles required by
6
6
these components.
7
7
@@ -22,11 +22,6 @@ The following objects will be used as you create the resources in the console fo
22
22
from DynamoDB using the ` tickets-get.js ` and ` health-check.js ` Lambda functions
23
23
* ` wild-rydes-dynamodb-post.json ` - This is the policy needed in order to write
24
24
to DynamoDB using the ` tickets-post.js ` Lambda function
25
- * ` wild-rydes-dynamodb-replication.json ` - This is the policy needed in order
26
- to use DynambDB Streams to replicate to a second region using the ` tickets-replicate.js `
27
- Lambda function
28
- * ` tickets-replicate.js ` Lambda function to replicate new DynamoDB records to our
29
- failover region
30
25
* ` health-check.js ` - Lambda function for checking the status of our application health
31
26
* ` tickets-get.js ` - Lambda function triggered by API Gateway to put application data
32
27
into DynamoDB
@@ -36,9 +31,9 @@ The following objects will be used as you create the resources in the console fo
36
31
There are several steps needed to deploy the API and Lambda functions via the
37
32
console. The basic steps are:
38
33
39
- 1 . Create the appropriate IAM policies and roles our four AWS Lambda functions
34
+ 1 . Create the appropriate IAM policies and roles our AWS Lambda functions
40
35
2 . Create the required Amazon DynamoDB table
41
- 3 . Create the four AWS Lambda functions
36
+ 3 . Create the needed AWS Lambda functions
42
37
4 . Create the Amazon API Gateway for the region you are currently deploying
43
38
5 . Testing to ensure our backend components are all working as expected
44
39
@@ -67,16 +62,15 @@ Name your policy `TicketGetPolicy` and click **Create policy**
67
62
68
63
![ Create Policy Editor] ( images/create-policy-2.png )
69
64
70
- Now repeat these exact same steps two more times in order to create the
71
- following two additional polices that will be needed during the workshop.
65
+ Now repeat these exact same steps one more time in order to create the
66
+ following additional policy that will be needed during the workshop.
72
67
73
68
** Download policy** : [ TicketPostPolicy] ( wild-rydes-dynamodb-post.json )
74
69
75
- ** Download policy** : [ TicketReplicatePolicy] ( wild-rydes-dynamodb-replication.json )
76
70
77
- Next you will create the three roles that correspond to the three polices that
71
+ Next you will create the three roles that correspond to the polices that
78
72
were just created. Each of these roles will be used by a different Lambda
79
- function thereby limiting the permissions of each function. This follows an
73
+ function thereby limiting the permissions of each function. This follows the
80
74
AWS Best Practice of granting [ least privilege] ( http://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege ) .
81
75
82
76
In the Console, select the ** IAM** service and choose ** Roles** from the left,
@@ -98,9 +92,8 @@ On the next screen, enter `TicketGetRole` for the Role Name and select **Create
98
92
99
93
![ Choose Role Final] ( images/create-role-final.png )
100
94
101
- Repeat the same steps two more times, this time creating the role for
102
- ` TicketPostRole ` and ` TicketReplicateRole ` and attaching
103
- the corresponding policy you created earlier.
95
+ Repeat the same steps one more time, this time creating the role for
96
+ ` TicketPostRole ` and attaching the corresponding policy you created earlier.
104
97
105
98
## 2. Create the DynamoDB Table
106
99
@@ -122,23 +115,20 @@ That’s all that is required for now to set up the table.
122
115
123
116
![ DymamoDB Create SXRTickets] ( images/dynamodb-create-sxrtickets.png )
124
117
125
- ## 3. Create Four Lambda functions
118
+ ## 3. Create Three Lambda functions
126
119
127
- Next, you will create four Lambda functions. First, navigate to ** Lambda** in
120
+ Next, you will create three Lambda functions. First, navigate to ** Lambda** in
128
121
the console (again ensuring you are still in the correct region) and click
129
- ** Create a function**
122
+ ** Create a function** Ensure you choose ** Author from scratch **
130
123
131
124
![ Create Lambda function] ( images/create-lambda-function.png )
132
125
133
- Next select “Author from scratch”
134
-
135
- ![ Lambda author from scratch] ( images/lambda-author-scratch.png )
126
+ Change the runtime to ` Node.js 6.10 ` . ('Node.js 8.10' should work but it
127
+ has not been tested)
136
128
137
129
Name your first function ` TicketGetFunction ` and assign the role with the ** matching**
138
- name you created previously to it and click ** Create function**
139
-
140
- On the next screen, ensure the runtime is ` Node.js 6.10 ` . If it isn’t, simply
141
- select it.
130
+ name you created previously to it and click ** Create function** and move on to the main
131
+ Lambda interface.
142
132
143
133
For the Handler, enter ` tickets-get.handler ` and then paste the following code into the
144
134
editor you see on your screen:
@@ -154,21 +144,21 @@ your function will not work - case matters*
154
144
155
145
Once everything is set correctly, click ** Save** near the top center of the screen.
156
146
157
- We still need to create three more lambda functions. All of them use ` Node.js 6.10 `
147
+ ** IMPORTANT NOTE** When editing the Lambda code using the console, it is VERY important that
148
+ your file name match the 'Handler Name' in the table below. You must rename the file from
149
+ the defaut of index.js or your function will not work! For example, if your handler name is
150
+ * tickets-get.handler* then your Lambda file name should be * tickets-get.js*
151
+
152
+ We still need to create two more lambda functions. All of them use ` Node.js 6.10 `
158
153
as the runtime. Repeat the same steps you used above. The table below provides the
159
- information needed for all four functions. Note that you have already done the first one.
154
+ information needed for all three functions. Note that you have already done the first one.
160
155
161
156
| Function Name | Handler Name | Execution Role | Env Var Key | Env Var Value |
162
157
| --------------------- | --------------------- | ------------------------------- | ------------- | -------------- |
163
158
| [ TicketGetFunction] ( tickets-get.js ) | tickets-get.handler | TicketGetRole | TABLE_NAME | SXRTickets |
164
159
| [ TicketPostFunction] ( tickets-post.js ) | tickets-post.handler | TicketPostRole | TABLE_NAME | SXRTickets |
165
- | [ TicketReplicateFunction] ( tickets-replicate.js ) | tickets-replicate.handler | TicketReplicateRole | TABLE_NAME | SXRTickets |
166
- | TicketReplicateFunction | | | TARGET_REGION | ap-southeast-1 |
167
160
| [ SXRHealthCheckFunction] ( health-check.js ) | health-check.handler | TicketGetRole | TABLE_NAME | SXRTickets |
168
161
169
- Note that ` TicketReplicateFunction ` has two variables - make sure you enter both.
170
- Also note that proper capitalization matters with ` Environment Variables ` . Improper
171
- case will cause issues later in the workshop.
172
162
173
163
## 4. Create API Gateway Endpoint
174
164
@@ -287,8 +277,6 @@ files within. You will see several files - here are descriptions of each:
287
277
to retrieve tickets from DynamoDB
288
278
* ` tickets-post.js ` – This is the Node.js code required by our second Lambda function
289
279
to create new tickets in DynamoDB
290
- * ` tickets-replicate.js ` – This is the Node.js code that replicates dynamodb data to
291
- another region.
292
280
* ` health-check.js ` - Lambda function for checking the status of our application health
293
281
294
282
@@ -341,9 +329,9 @@ You can do this using the following CLI command. Note that you must replace
341
329
342
330
aws cloudformation package \
343
331
--region eu-west-1 \
344
- --template-file wild-rydes-api.yaml \
345
- --output-template-file wild-rydes-api-output.yaml \
346
- --s3-bucket [bucket_name_you_created_above ]
332
+ --template-file wild-rydes-api-primary-region .yaml \
333
+ --output-template-file wild-rydes-api-primary-region- output.yaml \
334
+ --s3-bucket [eu_west_bucket_name_you_created_above ]
347
335
348
336
** IMPORTANT** DO NOT deploy any resources to Singapore during your initial pass
349
337
on Module 1. You will come back in Module 3 and then deploy the same components
@@ -354,11 +342,13 @@ convenience.
354
342
355
343
aws cloudformation package \
356
344
--region ap-southeast-1 \
357
- --template-file wild-rydes-api.yaml \
358
- --output-template-file wild-rydes-api-output.yaml \
359
- --s3-bucket [bucket_name_you_created_above ]
345
+ --template-file wild-rydes-api-failover-region .yaml \
346
+ --output-template-file wild-rydes-api-failover-region- output.yaml \
347
+ --s3-bucket [ap_southeast_bucket_name_you_created_above ]
360
348
361
- If all went well, you should get a success message and instructions to deploy your new template.
349
+ If all went well, you should get a success message and instructions to deploy your new template.
350
+ Follow those instructions. * NOTE: You will need to add '--capabilities CAPABILITY_IAM' to the*
351
+ * command in order to successfully deploy*
362
352
363
353
## 3. Deploy a stack of resources
364
354
@@ -373,8 +363,8 @@ Go ahead and run the following CLI command:
373
363
374
364
aws cloudformation deploy \
375
365
--region eu-west-1 \
376
- --template-file wild-rydes-api-output.yaml \
377
- --stack-name wild-rydes-api \
366
+ --template-file wild-rydes-api-primary-region- output.yaml \
367
+ --stack-name wild-rydes-api-primary \
378
368
--capabilities CAPABILITY_IAM
379
369
380
370
** IMPORTANT** DO NOT deploy any resources to Singapore during your initial pass
@@ -386,8 +376,8 @@ convenience.
386
376
387
377
aws cloudformation deploy \
388
378
--region ap-southeast-1 \
389
- --template-file wild-rydes-api-output.yaml \
390
- --stack-name wild-rydes-api \
379
+ --template-file wild-rydes-api-failover-region- output.yaml \
380
+ --stack-name wild-rydes-api-failover \
391
381
--capabilities CAPABILITY_IAM
392
382
393
383
@@ -408,6 +398,9 @@ our `TicketGetFunction` Lambda function and the `POST` method calling our `Ticke
408
398
Lambda function. You can also see that an empty DynamoDB table was set up as well as IAM
409
399
roles to allow our functions to speak to DynamoDB.
410
400
401
+ TODO: Instructions for setting up DynamoDB global table. MUST DO THIS BEFORE NEXT STEP. CANT HAVE DATA IN TABLE BEFORE SETTING UP GLOBAL REPLOICATION
402
+
403
+
411
404
You can confirm that your API is working by copying your API URL and appending ` /ticket `
412
405
to it before navigating to it into your browser. It should return the following:
413
406
0 commit comments