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.

    Saturday, September 3, 2016

    How to configure com_api with Joomla?

    Software required (Assume Joomla is installed)
    - Joomla version 3.6.2
    - com_api plugin (ac_api_installer_v1.8.5.zip)

    Installation steps

    • Login into admin console, click on "Extensions" → "Manage" → "Install" menu

    • Upload extension zip file to install the plugin.
    • Click on "Components" → "Api" → "API Keys", click on "New" button at "API Keys" page to launch "Add New Key" page

    • Click "User" button to add an user, click "Save & Close" button to dismiss the page. API key for the selected user is generated.

    • . Source code below is use to call "users" api from com_api, response in json format is returned
    • <?php
      
      $url="http://localhost/home/index.php?option=com_api&format=raw&app=users&resource=users&key=08a3ffdf9243bc883849e689c3749293"; 
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_NOBODY, false);
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      
      $resp = curl_exec($ch);
      print_r($resp);
      
      // Close request to clear up some resources
      curl_close($ch);
      
    • Source code below is use to call "login" api from com_api, response in json format is returned
    • <?php
      $url="http://localhost/home/index.php?option=com_api&format=raw&app=users&resource=login&key=08a3ffdf9243bc883849e689c3749293"; 
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_NOBODY, false);
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_exec($ch);
      
      $html = curl_exec($ch);
      print_r($html);
      
      curl_close($ch);
      
      

    • Source code below is use to call "config" api from com_api, response in json format is returned
    • <?php
      
      $url="http://localhost/home/index.php?option=com_api&format=raw&app=users&resource=config&key=08a3ffdf9243bc883849e689c3749293"; 
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_NOBODY, false);
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_exec($ch);
      $html = curl_exec($ch);
      
      print_r($html);
      
      curl_close($ch);