linkedin
Integrate AWS Secrets Manager With Springboot

Microservices on Cloud are a buzzword nowadays. When on the cloud the Microservices offers a lot of advantages like scalability, performance, almost zero downtime etc. There are many cloud providers like AWS, Google Cloud, Azure etc., each of which provides an array of managed services to ease application development. There are developers also that can walk you through the operations.

 

AWS being a market leader offers a lot of services. In this tutorial, we will develop a Spring boot application that uses AWS service RDS (relational database) and Secrets Manager (for storing application secrets). Our application will connect to the RDS DB in the backend, the credentials to connect to the database are stored in Secrets Manager. Let us develop the java development services step by step. 

 

Configure RDS database in AWS.

 

First, we need to configure the RDS database in the AWS environment. RDS is a relational database service provided by AWS. Follow https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_CreateDBInstance.html tutorial to set up RDS DB. Choose the engine type as MySQL. You will end up creating a RDS schema please name it as "product-db". Please note the hostname of this DB.

Now we need to create the table required for running the application. For that we can connect to the RDS DB using an editor like MySQL workbench. Create a new connection in the workbench, with the credentials for RDS DB.

 

1

 

Now create a table using the following command.

 

CREATE TABLE `product` ( `name` varchar(30) NOT NULL, `quantity` int(11) NOT NULL,

  `id` bigint(20) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='product table';


Configure the AWS Secrets Manager 

 

Now let us create a Secrets Manager entry for storing the RDS DB credentials. Go to AWS console and search for Secrets Manager. Now click on 'Store a new secret'. Fill in the details in the wizard. Select the secrets type as ‘Credentials for RDS database’.

 

2

 

Select the RDS database below

 

picture3  

 

Click Next, then give it a name. Once we review and confirm, it will display the Java code to retrieve the secret in the program. Use this code to access the secrets while creating the DataSource in the application.

 

Spring boot application 

 

Now let's create a Spring boot application. Create a Spring boot application using https://start.spring.io/. Import the project into Eclipse. 

 

1. Add below dependencies in pom.xml. We have added the AWS java sdk dependency here.

          picture4

 

2. Create a Product entity as follows. This entity is mapped to ‘Product’ table in DB.

 

picture5

 

3. We use Spring Data JPA to access the data from DB. So, add a repository as follows.

 

picture6

 

4. Next, will create a service class with methods to access the data from a repository.

 

picture7

 

5. Now create a controller class, which acts as endpoints. We have PUT endpoint to create a Product record in DB. GET endpoints to retrieve the products. Also, a POST for updating the product.

 

picture8

 

6. Next, we will create a configuration class for JPA configuration.

            

picture9

 

picture10

 

picture11

 

picture12

 

 

We can access the AWS secret using the AWS SDK. The values from secrets are mapped to the AwsSecret object, which in turn used in configuring the data source.

 

Now do a mvn clean install.

 

To run this application, we need to provide the AWS credentials, so that it can connect to the RDS database. We need to get AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_DEFAULT_REGION for our AWS account.  In order to find this, go to My security credentials under your account details in AWS console. Under Access keys section we can create access keys for the account. Now in Eclipse we need to pass this as environment variables while running the SpringApplication class in the project. Configure as below.

 

picture13

 

Once the application is up invoke the PUT endpoint using Postman with the below JSON body.

 

{"name":"AC", "quantity":20}

 

picture14

 

Similarly GET request for /product lists all products.

 

picture15

 

 

Conclusion 

Nowadays the applications can be developed by making use of the managed services provided by the cloud providers. The RDS DB is a relational DB offering from AWS that is using MySQL or Postgress engine. We can connect to the RDS DB using any of the editors available. But the RDS DB is costly so we need to make sure that the resources are deleted after developing this sample application. The AWS secrets manager offers security for our configurations with assurance from Amazon. This application makes use of both the service to demonstrate how a solid service can be developed. Hire an IT expert if you want a flawless system.

 


In need of developers? Here’s where you can find the best pool of tech talents. With Cloud Employee, you can hire dedicated offshore developers across many technologies. Talk to us, learn more how Cloud Employee works, or see our Developer Pricing Guide.

 

Author Bio

Evan Gilbort is a content writer and love to write about the latest technological trends.




Hire A Developer Now!

Want to find out more? Fill out the form below and our team will contact you within 24 hours!
Unable to process your request. Unable to connect to the remote server. Please refresh and try again. You can also contact our hotline numbers > UK: +44 20 3289 6876 | AU: +61 285 996110 | US: +1 929 223 0231 | SE: +46 844 68 12 45 .

Join Our Newsletter!

Receive weekly newsletters on outsourcing, tech and exclusive promotions.

Are you a writer or blogger in the technology space looking to showcase your knowledge to an audience?

Lets talk

Featured Article



SidebarWidget-img-developer-guide

Download Our Developer Pricing Guide

We did an analysis on the difference between western and Philippines developer salaries. Uk, USA and Australia pricing comparisons available.

Download

Be up to date!

Sign up for our newsletters and get our latest outsourcing and tech news, and exclusive promotions.

How many hours do you want the developer to dedicate to working with you?

What skillsets are you looking to hire?

When do you need your developer to start ?