How to create a microservice with API Gateway, AWS Lambda and SAM CLI using Java 8

rizahorasan
3 min readDec 3, 2021

In this tutorial, we will see how to develop a microservice using AWS Serverless services, API Gateway, AWS Lambda and SAM (AWS Serverless Application Model) CLI. To get a running environment, I will use Docker for Windows, Java 8, Spring Tool Suite (STS), Power Shell and Postman on a Windows 10 machine. You will also need an AWS account (free tier).

  1. Create the lambda function locally. You can follow this tutorial.

2. Deploy the project into AWS. After deployment an API end point (which is backed by the lambda function) will be created for us.

2.1 In Power Shell, cd into the folder where template.yml is stored. In my case it is …\customer-microservice-prj.

2.2 run sam deploy –guided command. If you want to use the default option for any question, just hit enter.

sam deploy — guided

It may take around 5 minutes to complete. If everything is ok, you will see the following output.

3.1 Open Amazon Console, go to CloudFormation and click Stacks. You will see the created (by CloudFormation) stack in the list.

CloudFormation.Stacks

3.2 Click the CustomerMicroserviceRestAPI stack. You will see the created resources as a list.

Resources under the CustomerMicroserviceRestAPI stack

3.3 ServerlessRestAPI is the Rest endpoint for our service. We need the end point address for this API. Click the Physical ID link. Go to Stages → Prod. Copy the Invoke URL.

Invoke URL address.

4. Open Postman. Do not forget to add /customers/ to the URL. Send a POST request.

POST request to Customers API.

Now the customers API is ready.

5. Do not forget to delete the stack. run sam delete CustomerMicroserviceRestAPI command. You can find the stack name in the samconfig.toml file. This file is in the same folder as template.yaml file.

samconfig.toml

--

--

rizahorasan

software developer, software architect, software something.