How to Integrate AWS Lambda function with API Gateway

Riyaz Sayyad | AWS Certified Database Specialist
A free video tutorial from Riyaz Sayyad | AWS Certified Database Specialist
Best Selling Instructor, Tech Evangelist at Rizmax Software
4.5 instructor rating • 3 courses • 28,186 students

Learn more from the full course

AWS Lambda & Serverless Architecture Bootcamp (Build 5 Apps)

AWS Serverless with AWS Lambda, API Gateway, Amazon DynamoDB, Step Functions, SAM, the Serverless Framework, CICD & more

25:23:00 of on-demand video • Updated December 2019

  • Master AWS Lambda, API Gateway, DynamoDB, and Step Functions from the ground up (Full of Demos and Hands On)
  • Streamline your development and deployment with AWS SAM as well as the Serverless Framework
  • Automate serverless deployment with AWS CI/CD tools like CodeCommit, CodeBuild and CodePipeline
  • Build Serverless REST API, Web App, Android and iOS Mobile Apps, Alexa Skill, IoT App and more
  • Integrate different services like S3, Kinesis, SNS, SQS and more in your serverless projects
  • Implement OAuth 2.0 Authentication and Authorization with AWS Cognito
  • Document your serverless APIs using API Gateway and Swagger
  • Learn Serverless Best Practices
English Now that we have our Lambda function ready, let’s go to the API Gateway console and hook this function to our API. Let’s open the GET method. We’ll change the integration type from Mock to Lambda function. So click on Integration Request. Change the Integration type from Mock to Lambda function. My Lambda function is in region us-west-2. So, If you look at the Lambda console, you will find the region code in the URL here. Alright, then we simply start typing the name of our Lambda function, i.e. Get Random Message, and that shows up here. And click on save. It will ask us to confirm that we want to switch to Lambda integration. Because we are changing the integration type from Mock to Lambda, its going to ask us. Click OK to confirm. And now it’s going to ask to give permissions to API Gateway to invoke our Lambda function. Click OK and AWS will automatically assign the necessary permissions to the API Gateway service to invoke our lamda function. Let’s go back to the Method Execution screen.So we can test this new API. Click Test. There are no parameters for this method. So simply click the Test. And we see the Response Body – Over the moon! But now, the response we see is slightly different from what we had earlier with Mock integration. It’s a plain text string. And if you see the response from our earlier test, we see a JSON string or a JSON object with key message and a corresponding value. So, we have to actually convert the Lambda response to JSON format. So, let’s go back in here and click on Integration response. Expand the HTTP 200 status response and under Mapping templates, select the application/json content type. Here we have to specify the transformation rules to map the incoming data to the expected JSON format. We create a simple JSON string here, just like we did in the last lecture. We add a key called message and in the value, instead of hardcoding, we pass the body of the Lambda response, the response we receive from the Lambda function. We can access the Lambda response body using Dollar Input DOT body. Just like that. Dollar Input is a predefined variable. And we’ll discuss more on this later in the course. For now, just save this. And back to the method execution screen, let’s test it locally before we deploy the API. Click on test. And now we have the JSON response. Wow! But we see the double quotes appearing twice. So to correct that, let’s go back once again to Integration Response and we can just remove these quotes and save. Let’s try it again. Click on Test. And there we go. Hello Serverless. Click it again and it should show some other message. Wow! It’s a great day today! And indeed it is. Right? Amazing! Now, before we can test this in the browser, we have to redeploy our API. So once you’ve completed testing locally, we can choose Deploy API from the Actions menu. Select a stage and Deploy. And now our API has been deployed. This is API root URL and we have to append it with our resource endpoint i.e. SLASH message. So, if we refresh this page, we should see a new response every time. And we can see a different message as we refresh the browser. World at my feet! Over the moon! Amazing! I hope you’re over the moon as well! So I hope this quick hands-on on AWS Lambda and API Gateway has you pumped up and excited and made you interested to learn even more about this amazing world of serverless computing. If it has, let’s continue further and we’ll dig into this even deeper. So, thank you for joining me here and I’ll see you in the next lecture in just a few seconds.