Java Spring Boot in the AWS Cloud: Understanding EC2 and RDS Deployments with Terraform. Part 2

Part 2

  • Now we are going to setup our infraestructura and deploy our Java application. However, we need before to understand what is Terraform.

What is Terraform

Terraform is an open-source infrastructure as code software tool that provides a consistent and reliable way to provision and manage infrastructure resources. It enables users to define and deploy infrastructure resources across various cloud platforms by using a declarative configuration language. Terraform allows for efficient collaboration, version control, and scalability, making it a popular choice for automating infrastructure management. With Terraform, you can easily create, change, and version your infrastructure as code, bringing more flexibility and stability to your cloud environment.

Why Terraform matter here

Terraform is a key component that allows us to understand how to connect each cloud component. In addition, it allows us to understand our infrastructure in the same way that we understand our code. This means we can share our infrastructure as code and share it with others .

Application components.

You can find all the code here.

https://github.com/maxiplux/terraform-2023-2024

Terraform spring boot

This program reads the database connection string from environment settings. It operates using Java 11 and Spring 2.7. To achieve this, I am implementing Spring Data Rest. The database server is PostgreSQL

Transforming code into Infrastructure as a Service (IAAS)

We are going to explain each component here and how to create then using Terraform
The Firts
//  terraform init
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

# This resource generates a new TLS private key. The key is an RSA key with a length of 4096 bits.
resource "tls_private_key" "ssh" {
  algorithm = "RSA"
  rsa_bits  = "4096"
}
# This resource creates a new AWS key pair. The public key is the OpenSSH-formatted public key
# generated by the tls_priSvate_key resource above. The key pair is named "terraform-pem".
resource "aws_key_pair" "generated_key" {
  key_name   = "terraform-pem"
  public_key = tls_private_key.ssh.public_key_openssh


}
# This resource writes the private key generated by the tls_private_key resource to a file
# named "terraform.pem". The file's permissions are set to 0600, meaning the owner can read and write
# the file, but no other users can access it.
resource "local_file" "private_key" {
  content         = tls_private_key.ssh.private_key_pem
  filename        = "terraform.pem"
  file_permission = "0600"
}

Similar Posts