11# Building the Wild Rydes Backend Components Layer
22
33In 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
55DynamoDB tables. You will also create the IAM polices and roles required by
66these components.
77
@@ -22,11 +22,6 @@ The following objects will be used as you create the resources in the console fo
2222 from DynamoDB using the ` tickets-get.js ` and ` health-check.js ` Lambda functions
2323* ` wild-rydes-dynamodb-post.json ` - This is the policy needed in order to write
2424 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
3025* ` health-check.js ` - Lambda function for checking the status of our application health
3126* ` tickets-get.js ` - Lambda function triggered by API Gateway to put application data
3227 into DynamoDB
@@ -36,9 +31,9 @@ The following objects will be used as you create the resources in the console fo
3631There are several steps needed to deploy the API and Lambda functions via the
3732console. The basic steps are:
3833
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
40352 . Create the required Amazon DynamoDB table
41- 3 . Create the four AWS Lambda functions
36+ 3 . Create the needed AWS Lambda functions
42374 . Create the Amazon API Gateway for the region you are currently deploying
43385 . Testing to ensure our backend components are all working as expected
4439
@@ -67,16 +62,15 @@ Name your policy `TicketGetPolicy` and click **Create policy**
6762
6863![ Create Policy Editor] ( images/create-policy-2.png )
6964
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.
7267
7368** Download policy** : [ TicketPostPolicy] ( wild-rydes-dynamodb-post.json )
7469
75- ** Download policy** : [ TicketReplicatePolicy] ( wild-rydes-dynamodb-replication.json )
7670
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
7872were 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
8074AWS Best Practice of granting [ least privilege] ( http://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege ) .
8175
8276In 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
9892
9993![ Choose Role Final] ( images/create-role-final.png )
10094
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.
10497
10598## 2. Create the DynamoDB Table
10699
@@ -122,23 +115,20 @@ That’s all that is required for now to set up the table.
122115
123116![ DymamoDB Create SXRTickets] ( images/dynamodb-create-sxrtickets.png )
124117
125- ## 3. Create Four Lambda functions
118+ ## 3. Create Three Lambda functions
126119
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
128121the 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 **
130123
131124![ Create Lambda function] ( images/create-lambda-function.png )
132125
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)
136128
137129Name 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.
142132
143133For the Handler, enter ` tickets-get.handler ` and then paste the following code into the
144134editor you see on your screen:
@@ -154,21 +144,21 @@ your function will not work - case matters*
154144
155145Once everything is set correctly, click ** Save** near the top center of the screen.
156146
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 `
158153as 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.
160155
161156| Function Name | Handler Name | Execution Role | Env Var Key | Env Var Value |
162157| --------------------- | --------------------- | ------------------------------- | ------------- | -------------- |
163158| [ TicketGetFunction] ( tickets-get.js ) | tickets-get.handler | TicketGetRole | TABLE_NAME | SXRTickets |
164159| [ 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 |
167160| [ SXRHealthCheckFunction] ( health-check.js ) | health-check.handler | TicketGetRole | TABLE_NAME | SXRTickets |
168161
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.
172162
173163## 4. Create API Gateway Endpoint
174164
@@ -287,8 +277,6 @@ files within. You will see several files - here are descriptions of each:
287277 to retrieve tickets from DynamoDB
288278* ` tickets-post.js ` – This is the Node.js code required by our second Lambda function
289279 to create new tickets in DynamoDB
290- * ` tickets-replicate.js ` – This is the Node.js code that replicates dynamodb data to
291- another region.
292280* ` health-check.js ` - Lambda function for checking the status of our application health
293281
294282
@@ -341,9 +329,9 @@ You can do this using the following CLI command. Note that you must replace
341329
342330 aws cloudformation package \
343331 --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 ]
347335
348336** IMPORTANT** DO NOT deploy any resources to Singapore during your initial pass
349337on Module 1. You will come back in Module 3 and then deploy the same components
@@ -354,11 +342,13 @@ convenience.
354342
355343 aws cloudformation package \
356344 --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 ]
360348
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*
362352
363353## 3. Deploy a stack of resources
364354
@@ -373,8 +363,8 @@ Go ahead and run the following CLI command:
373363
374364 aws cloudformation deploy \
375365 --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 \
378368 --capabilities CAPABILITY_IAM
379369
380370** IMPORTANT** DO NOT deploy any resources to Singapore during your initial pass
@@ -386,8 +376,8 @@ convenience.
386376
387377 aws cloudformation deploy \
388378 --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 \
391381 --capabilities CAPABILITY_IAM
392382
393383
@@ -408,6 +398,9 @@ our `TicketGetFunction` Lambda function and the `POST` method calling our `Ticke
408398Lambda function. You can also see that an empty DynamoDB table was set up as well as IAM
409399roles to allow our functions to speak to DynamoDB.
410400
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+
411404You can confirm that your API is working by copying your API URL and appending ` /ticket `
412405to it before navigating to it into your browser. It should return the following:
413406
0 commit comments