Saturday, March 31, 2018

How to setup Serverless server on AWS (RESTful APIs)?

Prerequisite

- Register an AWS account

Note: AWS has given a very details step by step guide. This is for my own note taking and reference

Create a New REST API
  • Login to AWS management console, select "API Gateway" in the "Networking & Content Delivery" section.
  • Click on "Get Started" button
  • Select "New API" and enter "BeezRydes" in the "API name" field
  • Select "Edge Optimized" in the "Endpoint Type" field.
  • Click "Create API" button
Create a Cognito User Pools Authorizer
  • Select "Authorizers in the left menu

  • Select "Create New Authorizer" button
  • Enter "BeezRydes" in the "Name" field
  • Select "Cognito" for the "Type" field
  • Enter "BeezPool" in the "Cognito User Pool" field
  • Enter "Authorization" in the "Token Source" field
  • Click on "Create" link
Verify your authorizer configuration
  • Browse to the /ride.html, please refer to screenshot below. Copy the token.

     
  • Back to the "Authorizers" page, click on the "Test" link

  • Paste the authorization code into "Test Authorizer" page

  • Click on the "Test" button, Response code: 200 us returned
Create a new resource and method
  • Click on the "Resources" in the left menu
  • Click on the "Actions" button, follow by "Create Resource" option
  • Enter "ride" in the "Resource Name" field, the "Resource Path" is auto populated as "ride"
  • Ensure the "Ensure API Gateway CORS" checkbox is checked
  • Click on the "Create Resource" button
  • Click on the "Actions" button, follow by "Create Method" option
  • Select "POST" in the OPTIONS dropdown, click the checkmark afterwards.
  • Select "Lambda Function" in the "Integration type" field
  • Check the "Use Lambda Proxy integration" check box
  • Enter the name of the function you created in the previous module, RequestUnicorn, for Lambda Function.
  • Click on "Save" button
  • Click on the "Method Request" 
  • Click on "Edit" icon next to the "Authorization" field
  • Select "BeezRydes Cognito user pool authorize", follow by the checkmark
Deploy Your API

  • Click on the "Actions" button, follow by "Deploy API" option
  • Select "[New Stage]" in the "Deployment Stage" field.
  • Enter "prod" in the "Stage name" field
  • Click on the "Deploy" button
  • Please take note of the "Invoke URL", "https://1g4tkvr1pj.execute-api.ap-southeast-1.amazonaws.com/prod"
Update the Website Config
  • Open the config.js file in a text editor
  • Update the invokeUrl setting under the api key in the config.js file.
  • window._config = {
        cognito: {
            userPoolId: 'XX-XXXXXXXXX-X_XXXXXXXXX', // e.g. us-east-2_uXboG5pAb
            userPoolClientId: 'XXXXXXXXXXXXXXXXXXXXXXXXXX', // e.g. 25ddkmj4v6hfsfvruhpfi7n4hv
            region: 'XX-XXXXXXXXX-X' // e.g. us-east-2
        },
        api: {
            invokeUrl: 'https://1g4tkvr1pj.execute-api.ap-southeast-1.amazonaws.com/prod' 
        }
    };
  • In the AWS Management Console, choose Services then select S3 under Storage.
  • Navigate to the website bucket and then browse to the js key prefix.
  • Upload the config.js file


    Validate your implementation
    • Visit /rides.html page, for example, "http://beez-firstname-lastname.s3-website-ap-southeast-1.amazonaws.com/signin.html"
    • Sign in page is displayed. Login with the registered user in thee previous tutorial.
    • Click on the map follow by "Request Unicorn" web service, the "Unicorn" will appear on the map as shown in the screenshot below



      Friday, March 30, 2018

      How to setup Serverless server on AWS (Serverless Service Backend)?

      Prerequisite

      - Register an AWS account

      Note: AWS has given a very details step by step guide. This is for my own note taking and reference

      Create an Amazon DynamoDB Table
      • Login to AWS management console, select "DynamoDB" in the "Database" section.
      • Choose "Create tables" button
      • Enter "Rides" for the table name
      • Enter "RideId" in the Primary Key column and select String for the key type
      • Make sure "Use default settings" checkbox is checked 
      • Click on the "Create" button, the new table "Rides" is created.


      Create an IAM Role for Your Lambda function

      • Select "IAM" in the "Security, Identity & Compliance" section.
      • Click on the "Roles" at the left menu, click on "Create Role" button
      • Select "Lambda" for the "Select your use case", click on the "Next Permissions" button
      • Enter "AWSLambdaBasicExecutionRole" to filter box, check on the checkbox and click on the "Next Review" button.
      • Enter "BeezLambda" in the "Role Name" field, click on the "Create Role" button
      • Select "BeezLamda" by clicking on the hyperlink, click on the "Add inline policy" link
      • Click on the "Choose a service", select "DynamoDB"
      • Click on the "Select actions", enter "PutItem" into the "Actions" textbox, check the "PutItem" checkbox.
      • Select the Resources section
      • Click on the "Add ARN" link to launch the "Add ARN" popup window, ensure the "Specific" option is selected
      • Fill in the "Region", "Account" and "Table Name", click on "Add" button to add the ARN
      • Click on the "Review Policy" button to launch "Review Policy" page.
      • Fill in the "DynamoDBWriteAccess" in the name field, click on the "Create Policy" button
      Create a Lambda Function for Handling Requests
      • Click on "Lambda" in the "Compute" section.
      • Click "Create Function" 
      • Enter RequestUnicorn in the Name field
      • Select Node.js 6.10 for the Runtime. 
      • Select "Choose an existing role" in the Role dropdown.
      • Choose "BeezLambda" in the "Existing Role" field
      • Click on the "Create function"
      • Scroll down to the Function code section and replace the exiting code in the index.js code editor with the contents of requestUnicorn.js. Please refer to "requestUnicorn.js" in https://github.com/awslabs/aws-serverless-workshops/archive/master.zip
      • Click on the "Save" button
      Test your implementation
      • Select "Configure test event" from the the "Select a test event..." dropdown.

      • Enter "TestRequestEvent" in the "Event name" in the "Configure test event" form.
      {
          "path": "/ride",
          "httpMethod": "POST",
          "headers": {
              "Accept": "*/*",
              "Authorization": "eyJraWQiOiJLTzRVMWZs",
              "content-type": "application/json; charset=UTF-8"
          },
          "queryStringParameters": null,
          "pathParameters": null,
          "requestContext": {
              "authorizer": {
                  "claims": {
                      "cognito:username": "the_username"
                  }
              }
          },
          "body": "{\"PickupLocation\":{\"Latitude\":47.6174755835663,\"Longitude\":-122.28837066650185}}"
      }
      • Click "Create"
      • Click on the "Test" button besides the "TestRequestEvent"
      • Verify the Test result is shown as listed below 
      {
        "statusCode": 201,
        "body": "{\"RideId\":\"eXif6NFtMpatJupR5kc3QA\",\"Unicorn\":{\"Name\":\"Shadowfax\",\"Color\":\"White\",\"Gender\":\"Male\"},\"UnicornName\":\"Shadowfax\",\"Eta\":\"30 seconds\",\"Rider\":\"the_username\"}",
        "headers": {
          "Access-Control-Allow-Origin": "*"
        }
      }

      Tuesday, March 27, 2018

      How to setup Serverless server on AWS (User Management)?

      Prerequisite

      - Register an AWS account

      Note: AWS has given a very details step by step guide. This is for my own note taking and reference

      Create an Amazon Cognito User Pool
      • Login to AWS website, select "Services" menu and "Cognito" sub menu under the "Security, Identity & Compliance" section.

      • Click on "Manage your User Pools" button
      • Click on "Click here to create a user pool" link
      • Provide a name for user pool such as "BeezPool", select "Review Defaults".
      • Click on "Create Pool", user pool with new pool id is created.
      Create an Amazon Cognito User Pool
      • Select "App Clients" in the "General settings" section of the left menu
      • Click "Add an app client" hyperlink
      • Enter the App client name such as "BeezWebApp"
      • Uncheck "Generate client secret" checkbox
      • Click on "Create app client" button
      • Newly generated "App client id" is generated

      Update the config.js File in Your Website Bucket
      • Based on the source code use in previous tutorial, https://github.com/awslabs/aws-serverless-workshops/archive/master.zip, alter /js/config.js file.
      • Click on the "General Settings" of the left menu, you can find the "Pool id" in the pool details page
      • Click on the "App Client Settings" of the left menu,  you can find the "App Client id" in the app client details page
      • Alter /js/config.js with the user pool id and app client id as shown below. 
      
      window._config = {
          cognito: {
              userPoolId: 'XX-XXXXXXXXX-X_XXXXXXXXX', // e.g. us-east-2_uXboG5pAb
              userPoolClientId: 'XXXXXXXXXXXXXXXXXXXXXXXXXX', // e.g. 25ddkmj4v6hfsfvruhpfi7n4hv
              region: 'XX-XXXXXXXXX-X' // e.g. us-east-2
          },
          api: {
              invokeUrl: '' // e.g. https://rc7nyt4tql.execute-api.us-west-2.amazonaws.com/prod',
          }
      };
       
      • Open the Amazon S3 console by visiting https://console.aws.amazon.com/s3/.
      • Select "beez-firstname-lastname" website bucket that has been created in the previous tutorial
      • Browse to the js prefix, click on the "Upload" button, upload /js/config.js that we have edited in the previous step to overwrite config.js file
      Validate your implementation
      • Visit /register.html under the website domain


      • Fill in the form, submit the user details. Verify email page will be displayed.


      • A confirmation code will be sent to the email account. Enter the email and confirmation code, follow by "Verify" button, user will be redirected to login page. Login again and user will be redirected to /ride.html page and a "Successful Authenticated" message will be displayed.





      Monday, March 26, 2018

      How to setup Serverless server on AWS (Static Web Hosting)?

      Prerequisite

      - Register an AWS account

      Note: AWS has given a very details step by step guide. This is for my own note taking and reference

      Installation steps (Host a static website)
      • Login to AWS website, select your region.

      • Click on "S3" on the "Storage" section, "Create bucket" page is launched.

      • Click on "Create Bucket" button, fill in the form with the details and click on "Create" button
      • A S3 bucket will be created. Please refer to the screenshot below
      • A S3 bucket will be created. Please refer to the screenshot below:-
        https://github.com/awslabs/aws-serverless-workshops/archive/master.zip 

      • Select the newly created S3 bucket,  launch the AWS management console.


      • Click on "Upload" button and copy all the files from "WebApplication\1_StaticWebHosting\website" folder into the "Upload" window..


      • Click on "Permission" tab, follow by "Bucket Policy" button, enter the "Bucket policy" and click "Save" button 
      • Click on "Properties" tab, follow by select "Static website hosting" and "Use this bucket to host a website" option. Enter index.html in the "Index Document" field. Click on "Save" button

      • Browse to the url "http://beez-firstname-lastname.s3-website-ap-southeast-1.amazonaws.com/", the template page is displayed.