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": "*"
  }
}

1 comment:

rmouniak said...


This blog gives very important info about AWS,Thanks for sharing
AWS Online Course