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.


      Friday, July 28, 2017

      How to configure MySQL ODBC Drivers for SSIS?

      Software required 

      - MySQL ODBC connector 5.3.9 (32 bits)
      - Microsoft Visual Studio 2012
      - Window (64 bits)

      Note: Microsoft Visual Studio 2012 support MySQL ODBC driver (32 bits) only. Hence, MySQL ODBC connector 5.3.9 (32 bits) is required on Window (64 bits).


      Installation steps
      • On Window 64 bits, the installed MySQL ODBC driver (32 bits) will not show at ODBC Data Source Administrator console.
      • To configure the MySQL ODBC driver (64 bits), browse to "C:\Windows\SysWOW64" and type in the command "odbcad32" to launch ODBC Data Source Administrator console
      • Configure "User Data Sources" as usual, Microsoft Visual Studio will be able to detect the newly created "User Data Sources" that configured on 32 bits driver.

      Monday, October 3, 2016

      How to setup OAuth 2.0 server using Apigility?

      Software required 

      - apigility 1.5
      - composer version 1.2.0
      - git version 2.10.0

      Installation steps
      • Assume apigility has been upgraded to version 1.5 by executing the command below. Version before 1.5 is buggy
       composer update  
      [APIGILITY_HOME] is your apigility installation home folder
      •  Launch your APIGILITY admin module, click on "Authentication" menu to launch the configuration page. Click "New adapter" button to launch the adapter configuration page.
      • Fill in the info as shown in the screenshot below, click on "Save" button to create adapter.

      •  Ensure the Status API authentication type is set to the newly created adapter, "oauth2".



       http://apigility/oauth/authorize?response_type=code&client_id=testclient&redirect_uri=/oauth/receivecode&state=xyz   
      • Approval page is displayed as shown in the screen below

      • Once the "testclient" is approved, an authentication code is generated.

      • Use postman with the configuration as shown in the screenshot below, generated access_token to access the API


      • Call the "status" api and "ping" services with the access_token as shown in the screenshot below.

      Public Clients
      • client_secret value is omitted for this case. User ID, password and client_id is required. grant_type = "password" is used. Please refer to the screenshot below

      Application Access

      • This is used for machine to machine integration. client_id and client_secret are required. grant_type = "client_credentials" is used. Please refer to the screenshot below

        Sunday, September 11, 2016

        How to fix "Error on rename of mysql frm due to permission denied" error message on mysql?

        Try to integrate "lucadegasperi", an OAuth  component on my application recently, I was required to patch the database by running the given patch script. I encountered error message as shown below when run the patch script.

        C:\wamp\www\lumen>php artisan migrate --seed
        Migration table created successfully.
        
        
          [Illuminate\Database\QueryException]
          SQLSTATE[HY000]: General error: 7 Error on rename of '.\[DATABASE_NAME]\#sql-2f6
          c_6.frm' to '.\[DATABASE_NAME]\oauth_session_scopes.frm' (Errcode: 13 - Permissi
          on denied) (SQL: alter table `oauth_session_scopes` add index `oauth_sessio
          n_scopes_session_id_index`(`session_id`))
        
        
        
          [PDOException]
          SQLSTATE[HY000]: General error: 7 Error on rename of '.\[DATABASE_NAME]\#sql-2f6
          c_6.frm' to '.\[DATABASE_NAME]\oauth_session_scopes.frm' (Errcode: 13 - Permissi
          on denied)
        

        I was using a superuser account to connect to mysql database. Should not prompt this error message

        Solution

        • The root cause is the Mcafee antivirus on my Windows 10 stop the MySql to edit the FRM file when "alter" command is executed.
        •  In order to run the patch script, I temporary turn off the real time scanning on my Mcafee. Launch Mcafee and click on "Virus and Spyware protection" option.
        • Select "Real-Time Scanning"option and turn off the "Real-Time scanning" option.

        • Rerun the command "php artisan migrate --seed", all tables was created successfully.