Monday 13 March 2017

Build an API Gateway To Access a Lambda Function



API Gateway: Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. Amazon API Gateway handles all the tasks involved in accepting and processing up to hundreds of thousands of concurrent API calls. Amazon API Gateway has no minimum fees or starts up costs. You pay only for the API calls you receive and the amount of data transferred out.



AWS Lambda is an event-driven, serverless computing platform provided by Amazon as a part of the Amazon Web Services. AWS Lambda executes your code only when needed and scales automatically, All you need to do is supply your code in one of the languages that AWS Lambda supports (currently Node.js, Java, C#, and Python).


Here in this documentation, I am going to give a detailed explanation and step by step process of how to use Amazon API Gateway and also how to create Lambda functions in AWS.



  • To create a custom API Gateway, connect your custom API Gateway to a set of AWS Lambda functions, and then call the AWS Lambda functions from your API Gateway and also created Lambda function in AWS.



Create Lambda Function:



  • To create Lambda function, go to the AWS services and select “Lambda”.










  • Click on “Create a Lambda Function”.  








  • Click on “Blank box” then the drop down list appears and selects “Python 2.7”.








  • Select “Blank Function”. 





   
  • Click on “Next” button.



  • Give the “Name”.
  • Click on “Runtime” then the drop down list appears and select “Python 2.7”









  • Give the “Handler” name as “function_name.lambda_handler”.
  • Click on “Role” then the drop down list appears and selects “Choose an existing role”.
  • Click on “Existing role” then the drop down list appears and select “Example_function”.
  • Click on “Next” button.










  • Click on “Create Function”.







  • Successfully created Lambda function.










Create an API Gateway


  • To create an API Gateway, go to the AWS console and select “API Gateway”.







  • Click on “Create API”.






  • Enter the “API name”.
  • The description field is optional.


NOTE: API name must exist in the Lambda Function. 









  • Click on “ Create API “  button.






  • Created our API Gateway name.
  • Click on “Actions” then the drop down list appears and select “Create Resources”.









  • Give the “Resource Name” automatically it will take the name in “Resource Path”.
  • You must have enabled the “CORS”.
  • Click on “Create Resource”.







  • Click on “Actions” then the drop down list appears and select “Create Method”. 







  • Select an empty box then the drop down list appears and selects “GET” method. 





  • Click on “Lambda Region” then the drop down list appears and select “us-west-2”. 








  • Give the “Lambda Function Name”.
  • Click on “Save” button.








  • You will get the dialogue box for add permission to Lambda function.
  • Click on “OK” button.









  • Click on “Integration Request”. 









  • Click on “Body Mapping Templates”.
  • Click on “Add Mapping templates”.







  • You have to write in textbox as “application/json” and
  • Click on “Tick Mark”.









  • Click on “Yes, secure this integration”. 







  • Click on “application/json”
  • In generate template you have to write input params in JSON format.
            Ex : “username” : “$input.params('username')”

  • Click on “Save” button.








  • Click on “Actions” then the drop down list appears and select “Deploy API”. 








  • Select Deployment stageas “New Stage”.










  • Give “Stage Name” as “prod” and
  • Click on “Deploy” button.









  • Click on “prod” then the drop down list appears and selects “GET” method.
  • It will generate “Invoke URL” of our “API Gateway Name”.









  • Copy “Invoke URL” and paste in a browser and enter
  • You will see the output in that page.










Conclusion: As shown, this is the way how to create Lambda function and API Gateway in Amazon Web Services. Also, this shows how to access a Lambda function and execute with the help of URL in API Gateway.






                     Further any queries contact me,



Ramakrishna Koppala,

krishna.rama107@gmail.com,

(M) +91-9885487177, 

Mouri Tech Pvt Ltd,


www.mouritech.com.