Skip to content

Commit

Permalink
updates for re:Invent 2018 SRV-326 Workshop (aws-samples#168)
Browse files Browse the repository at this point in the history
* angular upgrade; new toast library had to be used as part of upgrade.

* updates to toast and misc

* amplify additions.

* angular cli upgrade

* angular cli upgrade

* changes from cloud9

* amplify updates

* changes based on global table

* updates to failover stack; cognito unauth reqrmnt

* updated package and deploy cli commands

referencing new yaml files for primary and failover regions

* updated failover region cli command

* added notes where updates

* added notes where changes are needed

* can ignore section 2:Replicating the data

* addded policy allowing table scan in failover region

* update C9 environment.

* cleanup instructions

* updates to refresh table rows on form post. button to refresh region location.

* doc updates df

* doc updates df

* Further doc updates

* Update README_InstallDevDependencies.md

* Cloud 9 Image

* Update README_InstallDevDependencies.md

* cloud9 image

* image updates

* image updates

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README_InstallDevDependencies.md

* Update README_InstallDevDependencies.md

* Doc Updates - remove replication items

* Update README.md

* Update README.md

* Image Additions

* Image Additions

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Image Additions

* Update README.md

* Image Additions

* Image Additions

* Update README.md

* Update README.md

* Image Additions

* Update README.md

* Image Additions

* Image Additions

* Update README.md

* Update README.md

* Image Additions

* Image Additions

* Image Additions

* Update README.md

* Image Additions

* Update README.md

* Update README.md

* Update README.md

* Image Additions

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Image Additions

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README_InstallDevDependencies.md

* Update README_InstallDevDependencies.md

* remove resource id's
  • Loading branch information
juanlamadrid20 authored and mikedeck committed Nov 26, 2018
1 parent 1e24799 commit 559f1a0
Show file tree
Hide file tree
Showing 61 changed files with 1,029 additions and 714 deletions.
85 changes: 39 additions & 46 deletions MultiRegion/1_API/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Building the Wild Rydes Backend Components Layer

In this module, you will deploy backend application components to AWS. These
backend components include several AWS Lambda functions, two API Gateway Endpoints and two
backend components include several AWS Lambda functions, two API Gateway Endpoints and
DynamoDB tables. You will also create the IAM polices and roles required by
these components.

Expand All @@ -22,11 +22,6 @@ The following objects will be used as you create the resources in the console fo
from DynamoDB using the `tickets-get.js` and `health-check.js` Lambda functions
* `wild-rydes-dynamodb-post.json` - This is the policy needed in order to write
to DynamoDB using the `tickets-post.js` Lambda function
* `wild-rydes-dynamodb-replication.json` - This is the policy needed in order
to use DynambDB Streams to replicate to a second region using the `tickets-replicate.js`
Lambda function
* `tickets-replicate.js` Lambda function to replicate new DynamoDB records to our
failover region
* `health-check.js` - Lambda function for checking the status of our application health
* `tickets-get.js` - Lambda function triggered by API Gateway to put application data
into DynamoDB
Expand All @@ -36,9 +31,9 @@ The following objects will be used as you create the resources in the console fo
There are several steps needed to deploy the API and Lambda functions via the
console. The basic steps are:

1. Create the appropriate IAM policies and roles our four AWS Lambda functions
1. Create the appropriate IAM policies and roles our AWS Lambda functions
2. Create the required Amazon DynamoDB table
3. Create the four AWS Lambda functions
3. Create the needed AWS Lambda functions
4. Create the Amazon API Gateway for the region you are currently deploying
5. Testing to ensure our backend components are all working as expected

Expand Down Expand Up @@ -67,16 +62,15 @@ Name your policy `TicketGetPolicy` and click **Create policy**

![Create Policy Editor](images/create-policy-2.png)

Now repeat these exact same steps two more times in order to create the
following two additional polices that will be needed during the workshop.
Now repeat these exact same steps one more time in order to create the
following additional policy that will be needed during the workshop.

**Download policy**: [TicketPostPolicy](wild-rydes-dynamodb-post.json)

**Download policy**: [TicketReplicatePolicy](wild-rydes-dynamodb-replication.json)

Next you will create the three roles that correspond to the three polices that
Next you will create the three roles that correspond to the polices that
were just created. Each of these roles will be used by a different Lambda
function thereby limiting the permissions of each function. This follows an
function thereby limiting the permissions of each function. This follows the
AWS Best Practice of granting [least privilege](http://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).

In the Console, select the **IAM** service and choose **Roles** from the left,
Expand All @@ -98,9 +92,8 @@ On the next screen, enter `TicketGetRole` for the Role Name and select **Create

![Choose Role Final](images/create-role-final.png)

Repeat the same steps two more times, this time creating the role for
`TicketPostRole` and `TicketReplicateRole` and attaching
the corresponding policy you created earlier.
Repeat the same steps one more time, this time creating the role for
`TicketPostRole` and attaching the corresponding policy you created earlier.

## 2. Create the DynamoDB Table

Expand All @@ -122,23 +115,20 @@ That’s all that is required for now to set up the table.

![DymamoDB Create SXRTickets](images/dynamodb-create-sxrtickets.png)

## 3. Create Four Lambda functions
## 3. Create Three Lambda functions

Next, you will create four Lambda functions. First, navigate to **Lambda** in
Next, you will create three Lambda functions. First, navigate to **Lambda** in
the console (again ensuring you are still in the correct region) and click
**Create a function**
**Create a function** Ensure you choose **Author from scratch**

![Create Lambda function](images/create-lambda-function.png)

Next select “Author from scratch”

![Lambda author from scratch](images/lambda-author-scratch.png)
Change the runtime to `Node.js 6.10`. ('Node.js 8.10' should work but it
has not been tested)

Name your first function `TicketGetFunction` and assign the role with the **matching**
name you created previously to it and click **Create function**

On the next screen, ensure the runtime is `Node.js 6.10`. If it isn’t, simply
select it.
name you created previously to it and click **Create function** and move on to the main
Lambda interface.

For the Handler, enter `tickets-get.handler` and then paste the following code into the
editor you see on your screen:
Expand All @@ -154,21 +144,21 @@ your function will not work - case matters*

Once everything is set correctly, click **Save** near the top center of the screen.

We still need to create three more lambda functions. All of them use `Node.js 6.10`
**IMPORTANT NOTE** When editing the Lambda code using the console, it is VERY important that
your file name match the 'Handler Name' in the table below. You must rename the file from
the defaut of index.js or your function will not work! For example, if your handler name is
*tickets-get.handler* then your Lambda file name should be *tickets-get.js*

We still need to create two more lambda functions. All of them use `Node.js 6.10`
as the runtime. Repeat the same steps you used above. The table below provides the
information needed for all four functions. Note that you have already done the first one.
information needed for all three functions. Note that you have already done the first one.

| Function Name | Handler Name | Execution Role | Env Var Key | Env Var Value |
| --------------------- | --------------------- | ------------------------------- | ------------- | -------------- |
| [TicketGetFunction](tickets-get.js) | tickets-get.handler | TicketGetRole | TABLE_NAME | SXRTickets |
| [TicketPostFunction](tickets-post.js) | tickets-post.handler | TicketPostRole | TABLE_NAME | SXRTickets |
| [TicketReplicateFunction](tickets-replicate.js) | tickets-replicate.handler | TicketReplicateRole | TABLE_NAME | SXRTickets |
| TicketReplicateFunction | | | TARGET_REGION | ap-southeast-1 |
| [SXRHealthCheckFunction](health-check.js) | health-check.handler | TicketGetRole | TABLE_NAME | SXRTickets |

Note that `TicketReplicateFunction` has two variables - make sure you enter both.
Also note that proper capitalization matters with `Environment Variables`. Improper
case will cause issues later in the workshop.

## 4. Create API Gateway Endpoint

Expand Down Expand Up @@ -287,8 +277,6 @@ files within. You will see several files - here are descriptions of each:
to retrieve tickets from DynamoDB
* `tickets-post.js` – This is the Node.js code required by our second Lambda function
to create new tickets in DynamoDB
* `tickets-replicate.js` – This is the Node.js code that replicates dynamodb data to
another region.
* `health-check.js` - Lambda function for checking the status of our application health


Expand Down Expand Up @@ -341,9 +329,9 @@ You can do this using the following CLI command. Note that you must replace

aws cloudformation package \
--region eu-west-1 \
--template-file wild-rydes-api.yaml \
--output-template-file wild-rydes-api-output.yaml \
--s3-bucket [bucket_name_you_created_above]
--template-file wild-rydes-api-primary-region.yaml \
--output-template-file wild-rydes-api-primary-region-output.yaml \
--s3-bucket [eu_west_bucket_name_you_created_above]

**IMPORTANT** DO NOT deploy any resources to Singapore during your initial pass
on Module 1. You will come back in Module 3 and then deploy the same components
Expand All @@ -354,11 +342,13 @@ convenience.

aws cloudformation package \
--region ap-southeast-1 \
--template-file wild-rydes-api.yaml \
--output-template-file wild-rydes-api-output.yaml \
--s3-bucket [bucket_name_you_created_above]
--template-file wild-rydes-api-failover-region.yaml \
--output-template-file wild-rydes-api-failover-region-output.yaml \
--s3-bucket [ap_southeast_bucket_name_you_created_above]

If all went well, you should get a success message and instructions to deploy your new template.
If all went well, you should get a success message and instructions to deploy your new template.
Follow those instructions. *NOTE: You will need to add '--capabilities CAPABILITY_IAM' to the*
*command in order to successfully deploy*

## 3. Deploy a stack of resources

Expand All @@ -373,8 +363,8 @@ Go ahead and run the following CLI command:

aws cloudformation deploy \
--region eu-west-1 \
--template-file wild-rydes-api-output.yaml \
--stack-name wild-rydes-api \
--template-file wild-rydes-api-primary-region-output.yaml \
--stack-name wild-rydes-api-primary \
--capabilities CAPABILITY_IAM

**IMPORTANT** DO NOT deploy any resources to Singapore during your initial pass
Expand All @@ -386,8 +376,8 @@ convenience.

aws cloudformation deploy \
--region ap-southeast-1 \
--template-file wild-rydes-api-output.yaml \
--stack-name wild-rydes-api \
--template-file wild-rydes-api-failover-region-output.yaml \
--stack-name wild-rydes-api-failover \
--capabilities CAPABILITY_IAM


Expand All @@ -408,6 +398,9 @@ our `TicketGetFunction` Lambda function and the `POST` method calling our `Ticke
Lambda function. You can also see that an empty DynamoDB table was set up as well as IAM
roles to allow our functions to speak to DynamoDB.

TODO: Instructions for setting up DynamoDB global table. MUST DO THIS BEFORE NEXT STEP. CANT HAVE DATA IN TABLE BEFORE SETTING UP GLOBAL REPLOICATION


You can confirm that your API is working by copying your API URL and appending `/ticket`
to it before navigating to it into your browser. It should return the following:

Expand Down
Binary file modified MultiRegion/1_API/images/create-lambda-function.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified MultiRegion/1_API/images/create-lambda-ticket-get.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
185 changes: 185 additions & 0 deletions MultiRegion/1_API/wild-rydes-api-failover-region.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: SXR API Stack for failover region.

Resources:

TicketServiceAPI:
Type: AWS::Serverless::Api
Properties:
StageName: prod
DefinitionBody:
swagger: 2.0
info:
title:
Ref: AWS::StackName
paths:
"/ticket":
get:
responses: {}
x-amazon-apigateway-integration:
uri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${TicketGetFunction.Arn}/invocations
passthroughBehavior: "when_no_match"
httpMethod: "POST"
type: "aws_proxy"
post:
responses: {}
x-amazon-apigateway-integration:
uri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${TicketPostFunction.Arn}/invocations
passthroughBehavior: "when_no_match"
httpMethod: "POST"
type: "aws_proxy"
options:
consumes:
- "application/json"
produces:
- "application/json"
responses:
'200':
description: "200 response"
headers:
Access-Control-Allow-Origin:
type: "string"
Access-Control-Allow-Methods:
type: "string"
Access-Control-Allow-Headers:
type: "string"
x-amazon-apigateway-integration:
responses:
default:
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Methods: "'POST,GET,OPTIONS'"
method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Origin: "'*'"
requestTemplates:
application/json: "{\"statusCode\": 200}"
passthroughBehavior: "when_no_match"
type: "mock"
"/health":
get:
responses: {}
x-amazon-apigateway-integration:
uri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HealthFunction.Arn}/invocations
passthroughBehavior: "when_no_match"
httpMethod: "POST"
type: "aws_proxy"
options:
consumes:
- "application/json"
produces:
- "application/json"
responses:
'200':
description: "200 response"
headers:
Access-Control-Allow-Origin:
type: "string"
Access-Control-Allow-Methods:
type: "string"
Access-Control-Allow-Headers:
type: "string"
x-amazon-apigateway-integration:
responses:
default:
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Methods: "'POST,GET,OPTIONS'"
method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Origin: "'*'"
requestTemplates:
application/json: "{\"statusCode\": 200}"
passthroughBehavior: "when_no_match"
type: "mock"
swagger: '2.0'

TicketGetFunction:
Type: AWS::Serverless::Function
Properties:
Handler: tickets-get.handler
Runtime: nodejs6.10
FunctionName: TicketGetFunction
Policies:
- AWSLambdaDynamoDBExecutionRole #managed policy
- Version: '2012-10-17' # Policy Document
Statement:
- Effect: Allow
Action:
- dynamodb:Scan
- dynamodb:GetItem
Resource: "*"
Environment:
Variables:
TABLE_NAME: SXRTickets
Events:
GetResource:
Type: Api
Properties:
Path: /ticket
Method: get
RestApiId: !Ref TicketServiceAPI

TicketPostFunction:
Type: AWS::Serverless::Function
Properties:
Handler: tickets-post.handler
Runtime: nodejs6.10
FunctionName: TicketPostFunction
Policies:
- AWSLambdaDynamoDBExecutionRole #managed policy
- Version: '2012-10-17' # Policy Document
Statement:
- Effect: Allow
Action:
- dynamodb:PutItem
Resource: "*"
Environment:
Variables:
TABLE_NAME: SXRTickets
Events:
GetResource:
Type: Api
Properties:
Path: /ticket
Method: post
RestApiId: !Ref TicketServiceAPI

HealthFunction:
Type: AWS::Serverless::Function
Properties:
Handler: health-check.handler
Runtime: nodejs6.10
FunctionName: SXRHealthCheckFunction
Policies:
- AWSLambdaDynamoDBExecutionRole #managed policy
- Version: '2012-10-17' # Policy Document
Statement:
- Effect: Allow
Action:
- dynamodb:Scan
- dynamodb:GetItem
Resource: "*"
Environment:
Variables:
TABLE_NAME: SXRTickets
Events:
GetResource:
Type: Api
Properties:
Path: /health
Method: get
RestApiId: !Ref TicketServiceAPI

Outputs:
ApiUrl:
Description: URL of your API endpoint
Value: !Join
- ''
- - https://
- !Ref TicketServiceAPI
- '.execute-api.'
- !Ref 'AWS::Region'
- '.amazonaws.com/prod/'
Loading

0 comments on commit 559f1a0

Please sign in to comment.