Skip to content

Deploying API

Pre-requisites

You need to have JDK 17 and Maven 3.5.3 installed

Creating API

For creating a REST API you will use the Java Springboot framework

  1. Since Springboot itself is a separate topic of discussion, for the ease of explaining how to deploy APIs in cloud you will use the example spring-rest-api
  2. Clone this repo to your local directory
  3. Open the folder in your favorite IDE
  4. Any springboot project contains following structure,
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── c2c
│   │   │           └── dynamo
│   │   │               ├── DemoApplication.java
│   │   │               ├── DynamoDBConfig.java
│   │   │               ├── ProductController.java
│   │   │               ├── model
│   │   │               │   └── Products.java
│   │   │               └── repositories
│   │   │                   └── ProductRepository.java
│   │   └── resources
│   │       ├── application.properties
│   │       ├── static
│   │       └── templates
│   └── test
│       └── java
│           └── com
│               └── c2c
│                   └── dynamo
│                       └── DemoApplicationTests.java
├── Dockerfile
├── mvnw
├── mvnw.cmd
├── pom.xml
  1. Inorder to compile this program and create an executable jar. Issue the below command,
shell
mvn clean install
  1. This will create a target directory which will contain the dynamo-1.0.0.jar
  2. This jar file can then be run to start the API server listening on port 8080
shell
java -jar target/dynamo-1.0.0.jar
  1. Running the curl command curl http://localhost:8080/products should give result []. Add some items in the table to see the proper response
  2. This API reads the data from the DynamoDB table you created earlier in the lab
  3. Lets see how you can take this to EC2 instance and run this successfully

Packaging API

As you have seen earlier, you need to install JDK17 and Maven and other dependent packages used by project to connect to AWS DynamoDB. You cannot do this everywhere this service needs to run.

This issue is addressed by containers where it packages all the dependencies as image that can be just downloaded and run immediately. Just like the nginx server you run on lab

  1. To convert your application into a image you need to provide the instructions to docker what needs to be added. This is given in the form of Dockerfile
text
FROM openjdk:17
VOLUME /tmp
COPY target/dynamo-1.0.0.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
EXPOSE 8080
  1. Instructions say that
    • Download the JDK17 from openjdk
    • Mount the /tmp volume
    • Copy the jar file from target directory to image
    • Run the jar file
    • Expose the port 8080
  2. In order to build this image from the instruction file,
shell
docker build -t ACCOUNT_NAME/spring-rest-api .
  1. This image needs to available in a place where the EC2 instance can download from, hence you publish this image to DockerHub repository which is publicly accessible. If this is your first time it will prompt you for your DockerHub credentials.
shell
docker push ACCOUNT_NAME/spring-rest-api

Running API

  1. Now login to your EC2 instance and make sure docker is up and running using command docker ps. If its not running refer the Docker instrutions

  2. Once the docker is up and running, you can now download the image you published earlier and start the image

shell
docker pull ACCOUNT_NAME/spring-rest-api
docker run -it --rm -d -p 8080:8080 ACCOUNT_NAME/spring-rest-api
  1. You can now access your API using your instance public IP http://PUBLIP-IP:8080/products and you should be able to see the items from your DynamoDB table
Hint

If you get any access denied error while reading from DynamoDB, remember you need to provide permission using roles for EC2 to talk to DynamoDB service.

Tada! You just published a API for anyone to use 😃

Power of Containers

You just experienced the power of containers where you build your application once and anyone can use them without worrying about dependencies in their system. It just works and not just on your machine 😉

Released under the MIT License. Some of the contents are generated using Gen AI