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
- 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
- Clone this repo to your local directory
- Open the folder in your favorite IDE
- 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
- Inorder to compile this program and create an executable
jar
. Issue the below command,
mvn clean install
- This will create a target directory which will contain the
dynamo-1.0.0.jar
- This jar file can then be run to start the API server listening on port
8080
java -jar target/dynamo-1.0.0.jar
- Running the curl command
curl http://localhost:8080/products
should give result[]
. Add some items in the table to see the proper response - This API reads the data from the DynamoDB table you created earlier in the lab
- 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
- 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
FROM openjdk:17
VOLUME /tmp
COPY target/dynamo-1.0.0.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
EXPOSE 8080
- 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
- In order to build this image from the instruction file,
docker build -t ACCOUNT_NAME/spring-rest-api .
- 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.
docker push ACCOUNT_NAME/spring-rest-api
Running API
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 instrutionsOnce the docker is up and running, you can now download the image you published earlier and start the image
docker pull ACCOUNT_NAME/spring-rest-api
docker run -it --rm -d -p 8080:8080 ACCOUNT_NAME/spring-rest-api
- 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 😉