DEV Community 👩‍💻👨‍💻

Pedro Emanoel
Pedro Emanoel

Posted on

How to build microsservices with Spring Boot and AWS with Fargate and DOCKER - Part 6

Image description


What is RDS?
Relational Database Service, is a service in cloud disponibilized by amazon to facility the scalonament and management of databases, to app uses. With RDS we can use sundry relational databases, like MYSQL, SQL SERVER, MaRIADB, POSTGRESQL, etc...

Now, we gonna create a RDS Stack in AWS CDK application.
Image description

First add the dependency in POM.XML

Enter fullscreen mode Exit fullscreen mode
package com.myorg;


import java.util.Collections;

public class RdsStack extends Stack {

    public RdsStack(final Construct scope, final String id, Vpc vpc) {
        this(scope, id, null, vpc);

    public RdsStack(final Construct scope, final String id, final StackProps props, Vpc vpc) {
        super(scope, id, props);

        //Cloud Formation Parameter, we're going use that to send parameters to RDS in our template.
        CfnParameter dabasePassword = CfnParameter.Builder.create(this, "databasePassword")
                .description("password for our database RDS")

        //Configuring Security Group to use port 3306
        ISecurityGroup iSecurityGroup = SecurityGroup.fromSecurityGroupId(this, id, vpc.getVpcDefaultSecurityGroup());
        iSecurityGroup.addEgressRule(Peer.anyIpv4(), Port.tcp(3306));

        DatabaseInstance databaseInstance = DatabaseInstance.Builder
                .create(this, "Rds01")
                //identifier our database to see in aws console
                //instance of our database
                //database credentials
                //instance type, as i use aws educate i just can use the minimal configuration
                .instanceType(InstanceType.of(InstanceClass.BURSTABLE2, InstanceSize.SMALL))
                .allocatedStorage(5) //storage
                //securityGroups and Vpc subnet

        // exporting our parameters, if others stacks need

        //exporting the database endpoint
        CfnOutput.Builder.create(this, "rds-endpoint")

        //export the password
        CfnOutput.Builder.create(this, "rds-password")

Enter fullscreen mode Exit fullscreen mode

first, we configurate the security group to can use the 3306 port, remember, just services inside VPC can see our database, external access not.

Then, lets change the CdkApp class

package com.myorg;


import java.util.Arrays;

public class AwsSpringCdkApp {
    public static void main(final String[] args) {
        App app = new App();

        VpcStack vpcStack = new VpcStack(app, "Vpc");

        ClusterStack clusterStack = new ClusterStack(app, "Cluster", null, vpcStack.getVpc());

        RdsStack rdsStack = new RdsStack(app, "Rds", vpcStack.getVpc());

        Service01Stack service01Stack = new Service01Stack(app, "Service01", clusterStack.getCluster());
        service01Stack.addDependency(rdsStack); // depends rds

Enter fullscreen mode Exit fullscreen mode

Next, we need change the Service01Stack to add RDS parameters

package com.myorg;


import java.util.HashMap;
import java.util.Map;

public class Service01Stack extends Stack {

    // Specify the cluster
    public Service01Stack(final Construct scope, final String id, Cluster cluster) {
        this(scope, id, null, cluster);

    public Service01Stack(final Construct scope, final String id, final StackProps props, Cluster cluster) {
        super(scope, id, props);

        *Adding Database Username, Endpoint and password in our enviroments
        Map<String, String> envVariables = new HashMap<>();
        envVariables.put("SPRING_DATASOURCE_URL", "jdbc:mariadb://"
                + Fn.importValue("rds-endpoint")
                + ":3306/aws_spring01?createDatabaseIfNotExist=true");
        envVariables.put("SPRING_DATASOURCE_USERNAME", "admin");
        envVariables.put("SPRING_DATASOURCE_PASSWORD", Fn.importValue("rds-password"));

        ApplicationLoadBalancedFargateService service01 = ApplicationLoadBalancedFargateService
                .create(this, "ALB-01")
                .cpu(512) // how much cpu we need use
                .desiredCount(2) // how much instances we want
                        //Container informations
                                .image(ContainerImage.fromRegistry("pedrospiet/aws_training_01:1.0.0")) //Your repository on dockerHub
                                                .logGroup(LogGroup.Builder.create(this, "Service01LogGroup")
                                .environment(envVariables) // adding enviroments

        service01.getTargetGroup().configureHealthCheck(new HealthCheck.Builder()

     /*   ScalableTaskCount scalableTaskCount = service01.getService().autoScaleTaskCount(EnableScalingProps.builder()

        scalableTaskCount.scaleOnCpuUtilization("Service01AutoScaling", CpuUtilizationScalingProps.builder()

Enter fullscreen mode Exit fullscreen mode

Now, we make deploy with the command below:

cdk deploy --parameters Rds:databasePassword={your_password} Vpc Cluster Rds Service01
Image description

It Works.

You can see the diferences in RDS console on AWS.

bye bye.

Top comments (0)

🌚 Life is too short to browse without dark mode