How to create an API in AWS API Gateway using AWS Lambda – 2022

Machine Learning Projects

Hey guys, in today’s blog we will see how to create an API in AWS API Gateway using AWS Lambda. In the last blog, we saw a detailed step-by-step guide of how we can deploy an AWS Lambda using AWS ECR Docker Image. So without any further due, let’s do it.

Step 1 – You should have a Lambda deployed on AWS Lambda.

  • For today’s use case, we will create a simple Lambda function that will just take 2 numbers and return their sum as the result.
create an API in AWS API Gateway using AWS Lambda

Step 2 – Let’s create an API in AWS API Gateway using AWS Lambda.

  • Now let’s head over to API Gateway and hit the Create API button.
  • Select REST API and click on Build.
create an API in AWS API Gateway using AWS Lambda
  • Keep everything as it is and give a name to your API.
  • I named it test-API and simply hit the Create API button.
create an API in AWS API Gateway using AWS Lambda
  • Once Done you will see a screen like this.
create an API in AWS API Gateway using AWS Lambda
  • Now click on Actions and select the Create Method button.
  • Open the dropdown and select GET or POST depending upon your use case and click the check button next to it.
create an API in AWS API Gateway using AWS Lambda
  • Once you have completed the above steps, you will see a screen like this.
create an API in AWS API Gateway using AWS Lambda
  • On this screen, you just have to select the Lambda function for which you want to make the API.
  • I have selected the test Lambda that I created above.
  • Hit the save button.
  • Once done you will see a screen like this.
Machine Learning Projects

Step 3 – Deploy the API.

  • Now again click the Action button and click on the Deploy API button.
  • You will see a screen like this.
create an API in AWS API Gateway using AWS Lambda
  • Give any name to your stage and click on the Deploy button.
  • Once you complete this process, you will be directed to a screen like this where you can see your API as Invoke URL.
Machine Learning Projects
  • Now let’s check this API in Postman with num1 and num2 as parameters.
Machine Learning Projects
  • And BOOM here is the result 5 (sum of num1 and num2).

NOTE – Add the following headers in your return request to avoid CORRS error on the frontend. Also, you need to click on Enable Corrs in Action dropdown and then click Deploy API again to enable its effect.

return {
        "statusCode": 200, 
        "result": res, 
        'headers': {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Headers': 'Content-Type',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
            }
    }

So this is how you create an API in AWS API Gateway using AWS Lambda, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…

Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projectsFlask projects at machinelearningprojects.net

Leave a Comment

Your email address will not be published.