Linux | Cloud | DevOps | Scripting

Breaking

Monday, 26 August 2019

Integrate Lambda Function with API-Gateway


Lambda Script - API Gateway

If we use one software to call another software for using their functionality; such types of requests are known as API requests and these calls are known as API calls

Advantages of API:

1. No need to share the code of the software.
2. Do not need to build that code in another system.
3. Easily and secure way to allow other companies to use the software functionality.

In API call, generally we use the following methods: 

1. get,
2. put,
3. post and
4. delete

For example, many times we have seen Google map on other company's website. This thing is done by API request. In this case, the person's website call to Google map (known as API call) and Google map will process that call on their own end and will only provide the output as a result. That person who is doing API Call will be responsible to pay to Google map.

In this practice, we are going to generate an URL and when someone will click on this, a snapshot will be created. For this, we need to have an active Lambda Function.

Steps we need to follow to integrate Lambda Function with API-Gateway:

1. Create an AWS EC2 instance and provide tag 'auto_snapshot=true'
2. Create an IAM role
3. Prepare the code for Lambda Function
4. Create a Lambda Function
5. Integrate Lambda code with API Gateway
6. Deploy API
7. Verify snapshot using URL in any browser

Use the below-mentioned URL to cover steps from 1 to 4:

https://redhatpanacia.blogspot.com/2019/08/auto-snapshot-using-lambda.html

If you performed that practical right now, then just delete the trigger first and then continue to API request. To delete that trigger just click on 'X' symbol beside the Trigger and then save.

Delete the trigger from previous practical
Fig: Delete the trigger from previous practical

Step 5: Integrate Lambda code with API Gateway:

AWS ➔ Services ➔ API gateway ➔ Get started ➔ Choose the protocol: Rest ➔ Create new API ➔ New API ➔ API name: auto_snapshot_api ➔ provide a description ➔ Create API.

API Gateway
Fig: API Gateway
Whenever we access a webpage, first Get method call functions. So, here also we will use the Get function call.

After clicking on Create API ➔ Actions ➔ Create Method ➔ from the drop-down list choose GET ➔ now click on tick mark beside GET…

API Gateway Function
Fig: API Gateway Function
…Integration type: Lambda function ➔ Lambda Function: auto_snapshot (select Lambda function which you created) ➔ Save.

API Get-function setup
Fig: API Get-function setup

Step 6: Deploy API

After saving the Get method, navigate to Actions ➔ Deploy API…

Deploy API
Fig: Deploy API
…Deployment stage: [New stage] ➔ provide a stage name ➔ Deploy.

Get method URL
Fig: Get method URL

Step 7: Verify the snapshot using URL in any browser:

As we click on Deploy, this will result an Invoke URL on the screen. We can use this URL to take snapshots. Also, we can forward this URL to anyone and as he will click on this URL, which will output a null value on the browser and a snapshot will be created which we can verify in AWS EC2 service.

Enjoy!



No comments:

Post a Comment

Pages