Deploy AWS lambda as a Rest API (Java)

Aditi Aggarwal
5 min readDec 23, 2020

--

The aim of this article is to access AWS lambda as a REST API. To achieve this, we’ll be using AWS API gateway.

We are going to cover the following topics in this article.

  1. Create an AWS lambda function in java
  2. Create a rest API in AWS api gateway
  3. Integrate lambda function with the rest API
  4. Hit the API and get the response back from the lambda

Prerequisites:

  1. AWS account subscription
  2. AWS CLI
  3. Java8 or higher with an IDE (eclipse/ IntelliJ)

I am going to create the lambda function and API gateway resources using cloudformation template so that all of you can create it easily and watch it working. At the end of the article, you can find the link to the github repository.

For the purpose of this tutorial, I am going to create a simple POST request, which will expect firstName and the lastName in the request body and will return a custom greeting using the same along with a status in response.

This tutorial is to give a general idea about lambda and API gateway integration, you can modify the lambda code and API gateway resources as per your own accord.

STEP 1: Create and deploy resources in AWS console

Clone the github repository in your local machine and run the shell scripts present at the root of the repo. First run the create-bucket.sh file which will create a S3 bucket in your AWS account. Then execute the deploy.sh file which will generate the mvn build package and deploy the resources present in template.yml.

The template.yml. file contains two resources:

  1. Lambda function: It contains all the properties that our lambda function will possess. You can get more details on how to add/update parameters in this section by following AWS documentation.
  2. API gateway resource: These few lines of code are saving a lot of manual steps on AWS console. Follow each and every line in detail and take reference from AWS documentation to understand it better. I have defined the /greeting endpoint along with requestTemplates, responseParameters and others.

After executing the above mentioned shell scripts, go over to your AWS console and check lambda service and API Gateway service. You should see both the resources with the name lambda-as-rest-api

STEP 2: Test the API resource

  1. Go over to AWs console -> API Gateway -> lambda-as-rest-api resource
  2. Go to Resources -> POST -> Click on Test

3. Add the request body in the json format as shown in the image and click on test.

4. You might expect an error Invalid Permissions on lambda function . This is because we have create the resources and linked them but did not provide explicit permission to the API gateway so it can access our lambda function. To do that, either you can add API gateway permission in template.yml or you can follow the steps below.

5. Go over to Resources -> /greeting -> Integration Request. You should see a screen something like this. Click on the small pen icon beside Lambda function.

6. Once you click on the pen icon, you should see a check mark besides the lambda function name. Click on that.

7. You should get a popup saying You are about to give permission to API gateway to invoke your lambda function. That is what we wanted right! Go ahead and click on OK!

8. If you check the lambda function in AWS console, you should see API gateway icon in the Designer section. This means we have successfully added the API gateway permission. Now is the time to test again!

9. Go back to the test console with same request body and this time you should see a 200 response with response body:

{“greeting”: “Hola!! <firstName> <lastName>”,“status”: “OK”}

10. You can hit this API using postman as well. Go to Stages and copy this invoke URL. Now we can use this in a curl request or postman.

11. I have used postman to see the results. You can see the attached curl request. Make sure you add your actual credentials here.

12. In the Authorization section, select AWS signature and add your access key, secret key and session token etc. and then hit the request. If you skip this step, you will get a 403.

STEP 3: (Optional) Create a custom domain name

If you want a custom domain name then you need to follow these two additional steps:

  1. Create a custom domain name in AWS API gateway
  2. Create a Route 53 record corresponding to this custom domain name

And you can have any domain name you want.

STEP 4: Cleanup the resources

Make sure you cleanup the resources you have created else AWS might bill you for the same. Since we used cloudformation template to deploy our resources, simply go over to the Cloudformation service in AWS portal and delete the stack that we deployed.

Link to the github repo: https://github.com/aditi2205/lambda-as-rest-api

--

--

Aditi Aggarwal
Aditi Aggarwal

Written by Aditi Aggarwal

Software-Engineer 2 @Intuit | Indira Gandhi Delhi Technical University

Responses (1)