A quick Spring Boot application deploy practice with a PostgreSQL connection using AWS services like Elastick Beanstalk and RDS.
/aws-test
Is a simple Spring Boot application with two endpoints/aws-test-dev
Have all files to do local test/aws-test-prod
Have all files to structure the deploy
Let's do all the steps to do a local test and deploy the application on AWS.
First of all, let's compile de Spring Boot application. Make sure that there's a local PostgreSQL server instance to the application to do connection and have a successful build.
cd aws-test
mvn clean package
So, switch to the /aws-test-dev
folder and let's run a local application with docker environment.
cd aws-test-dev
sh ./build.sh
docker-compose up
Access http://localhost
and see the follow message: Hello, baby!
Switch to the /aws-test-prod
and let's build the production image. We will push this image to the Dockerhub. Because, the EB instance will be configured with a Docker environment.
Pay attention to the Dockerrun.aws.json
. The file will be uploaded to the EB instance. It had a config to the EB instance recognize our Dockerhub image.
cd /aws-test-prod
sh ./build.sh
Okay, we finished the project config steps. Now, we need to create the RDS and EB instances and config them.
- Create a database
- Use the Standard Create
- Select the PostgreSQL
- Select the Free tier template
- Define your credentials
- In Additional connectivity configuration, make sure to select Yes for the Publicly accessible. So, later, we will can have a connection with a PostgreSQL client and manipulate the RDS instance.
- Create a database called
awstest
on Additional configuration - So, click on Create database and wait... (This operation may take a long time)
- After the creation, edit the Security group inbound to accept all connections.
- Use a PostgreSQL client to connect with the instance and create a table called
usuario
with the follow simple schema:
create table usuario (
id integer,
nome varchar,
primary key(id)
)
- Run the follow SQL to popule the table
insert into usuario(id, nome) values (1, 'Natan');
insert into usuario(id, nome) values (2, 'Jonas');
- Create environment
- Select the Web server environment
- Define the Environment informations
- Choose Docker as platform
- Upload the
Dockerrun.aws.json
file - Wait the creation
- Go to configuration
->
software->
click on Modify - Set the follow environment properties (key | value):
SPRING_DATASOURCE_URL | jdbc:postgresql://<your-rds-host>:5432/awstest
SPRING_DATASOURCE_USERNAME | <your-rds-user>
SPRING_DATASOURCE_PASSWORD | <your-rds-password>
- Apply the config and wait the instance restart
In the end, access your EB instance link and see the message: Hello, baby!
In additional, access the /usuarios
to see the inserted users.