Showing posts with label capstone. Show all posts
Showing posts with label capstone. Show all posts

Friday, April 29, 2022

AWS IBM Capstone Part 2 - Lambda, API Gateway portion


Step 1: Create a NodeJs Lambda function

  • Select Lambda service
  • Click on Create function button
  • Select Author From Scratch.
  • Function name : ApiFunc
  • Runtime: Node.js 14.x (arm68)
  • Permission ---> change default execution role
  • Select create new role from AWS policy Template
  • Provide the role name apifuncrole
  • Click on Create function button (this will take some time)

Step 2: Use the below code for the Lambda function 

'use strict';

const https = require('https');
exports.handler = async (event) => {
    let dataString = '';

    const response = await new Promise((resolve, reject) => {
        const req = https.get("https://jsonplaceholder.typicode.com/todos/1", function(res) {
          res.on('data', chunk => {
            dataString += chunk;
          });
          res.on('end', () => {
            resolve({
                statusCode: 200,
                body: JSON.stringify(
                        {
                            "data": JSON.parse(dataString).title,
                            "timestamp": new Date()
                        }, null, 4
                    )
            });
          });
        });
       
        req.on('error', (e) => {
          reject({
              statusCode: 500,
              body: 'Something went wrong!'
          });
        });
    });
   
    return response;
};

Then Click on  File--->save and Click on Deploy


Step 3: Create Rest API using API Gateway.

  • Select API Gateway Service
  • Click on Create API button
  • Select RestAPI
  • Click on Build button
  • Select New API
  • Put in any Api Name: myapi
  • Click on Create Api Button.

Step 4: Create a Resource

  • Select Action--> Create Resource
  • Resource Name: what-is-my-title
  • Click on Create Resource Button

Step 5: Initialize GET Method for Resource

  • Select resource what-is-my-title
  • Go to Action
  • Create Method from Drop down 
  • select Get Method
  • click on tick mark

Step 6:Choose the integration point for your new method (link to your lambda function).

  • Click on Get Method 
  • Integration Type: Lambda Function
  • Select check box Use Lambda proxy Integration
  • Lambda function: <<Lamda function you created in step 1>> 
  • Click on Save button

Step 7: Staging the API (masking it with the first layer or exposing the service) www.google.com/irfan-api

  • Select GET and Goto Action--->Deploy API
  • Deployment Stage : New Stage
  • Stage Name: api
  • Click on the Deploy button.

Step 8: You will find a prompt indicating: Invoke URL on the top and open that URL in a new tab and at the end of the URL.

  • add what-is-my-title
  • You will be able to see the title and current date and time

Step 9: Download the Json file for API Gateway.

  • In the Stages page, click on Export tab 
  • select the Swagger
  • select Export as Swagger and click on json. 

Step 10: Go to IAM services, click on Roles Tab, take screen shot and copy IAM Role policy 




Fluentd

Open-source log data collector > why logs? - for compliance (auditing, company, business) - for security (transparency, monitoring, admin...