Skip to content

A reference project that combines Spring Boot with Terraform to automatically provision an AWS EC2 instance with a configured Security Group. Perfect for quickly bootstrapping Java applications with Infrastructure as Code (IaC).

License

Notifications You must be signed in to change notification settings

caioneves05/terraform-boilerplate

Repository files navigation

Terraform Boilerplate

This project is a boilerplate that integrates a Java (Spring Boot) application with infrastructure as code using Terraform.

Overview

  • Backend: Java 17+ with Spring Boot
  • Infrastructure: Terraform (infraestructure/ folder)
  • Dependency Management: Maven
  • Containerization: Dockerfile included

Project Structure

├── Dockerfile                  # Application containerization
├── infraestructure/            # Terraform files for infrastructure
│   └── main.tf
├── src/
│   ├── main/
│   │   ├── java/caioneves05/terraform_boilerplate/app/  # Java source code
│   │   │   ├── TerraformBoilerplateApplication.java
│   │   │   └── controller/ApiController.java
│   │   │   └── dtos/TimestampResponse.java
│   │   └── resources/           # Configurations and static resources
│   │       ├── application.properties
│   │       ├── static/
│   │       └── templates/
│   └── test/java/caioneves05/terraform_boilerplate/     # Automated tests
│       └── TerraformBoilerplateApplicationTests.java
├── pom.xml                     # Maven configuration
├── LICENSE
├── HELP.md
└── readme.md                   # This file

Prerequisites

  • Java 17+
  • Maven 3.8+
  • Docker (optional)
  • Terraform 1.0+

How to Run the Application

  1. Build:
    ./mvnw clean package
  2. Run:
    java -jar target/terraform-boilerplate-0.0.1-SNAPSHOT.jar
  3. Via Docker:
    docker build -t terraform-boilerplate .
    docker run -p 8080:8080 terraform-boilerplate

How to Run the Tests

./mvnw test

Infrastructure with Terraform

  1. Go to the infraestructure/ folder:
    cd infraestructure
  2. Initialize Terraform:
    terraform init
  3. Preview the execution plan:
    terraform plan
  4. Apply the infrastructure:
    terraform apply

Attention: Review the Terraform files before applying them in production.

Best Practices for AWS Credentials with Terraform

Do not hardcode the profile parameter in the Terraform provider block.

Hardcoding the AWS profile in your Terraform configuration can reduce portability and flexibility, especially in collaborative or automated environments. Instead, prefer the following approaches:

1. Use Environment Variables

Set the AWS profile before running Terraform:

export AWS_PROFILE=your-sso-profile

This allows Terraform to use the correct credentials without changing the code.

2. Use SSO with AWS CLI

If you use AWS SSO, authenticate with:

aws sso login --profile your-sso-profile

Then, set the profile as shown above.

3. (Optional) Use a Terraform Variable for the Profile

For advanced use, you can define a variable in your Terraform code:

variable "aws_profile" {
  type        = string
  default     = null
  description = "AWS CLI profile to use."
}

provider "aws" {
  region  = "us-east-2"
  profile = var.aws_profile
}

And run:

terraform plan -var="aws_profile=your-sso-profile"

Summary:

  • Avoid hardcoding the profile in the provider block.
  • Prefer environment variables or variables in Terraform for flexibility and security.
  • This makes your code more portable and CI/CD friendly.

License

See the LICENSE file for more details.

About

A reference project that combines Spring Boot with Terraform to automatically provision an AWS EC2 instance with a configured Security Group. Perfect for quickly bootstrapping Java applications with Infrastructure as Code (IaC).

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published