A Spring Boot application with Terraform infrastructure as code and GitHub Actions CI/CD pipeline for automated deployment to AWS.
This project demonstrates a simple Spring Boot REST API with Terraform infrastructure as code for AWS resources. The application provides a time service endpoint that returns the current time in São Paulo, Brazil timezone.
The infrastructure is managed using Terraform and is deployed automatically to AWS using GitHub Actions workflows. The project supports both development and production environments.
-
Backend:
- Java 17
- Spring Boot 3.4.6
- Maven
-
Infrastructure as Code:
- Terraform 1.8.3
- AWS S3 (for application resources and Terraform state)
- AWS DynamoDB (for Terraform state locking)
-
CI/CD:
- GitHub Actions
├── .github/workflows/ # GitHub Actions workflow definitions
│ ├── main.yml # Production deployment workflow
│ └── terraform.yml # Reusable Terraform workflow
├── infra/ # Terraform infrastructure code
│ ├── envs/ # Environment-specific configurations
│ │ ├── dev/ # Development environment
│ │ └── prod/ # Production environment
│ ├── backend.tf # Terraform backend configuration
│ ├── destroy_config.json # Configuration for infrastructure destruction
│ ├── main.tf # Main Terraform resources
│ ├── provider.tf # AWS provider configuration
│ └── variables.tf # Terraform variables
└── src/ # Spring Boot application source code
├── main/
│ ├── java/
│ │ ├── com/api/terraform/pipeline/terraforminfrapipeline/
│ │ │ └── TerraformInfraPipelineApplication.java # Main application class
│ │ ├── controller/
│ │ │ └── TesteController.java # REST controller
│ │ └── dto/
│ │ └── response/
│ │ └── TimeResponse.java # Response DTO
│ └── resources/
│ └── application.properties # Application configuration
└── test/ # Test source code
GET /api/time: Returns the current time in São Paulo, Brazil timezone in the format "dd/MM/yyyy HH:mm:ss"
Example response:
{
"time": "01/01/2023 12:00:00"
}The project uses Terraform to manage AWS infrastructure:
- Creates an S3 bucket with a name based on the environment (dev or prod) and a random suffix
- Uses S3 for Terraform state storage
- Uses DynamoDB for state locking
- Deployed to the AWS sa-east-1 region (South America - São Paulo)
The project uses GitHub Actions for CI/CD:
- Production Deployment: Triggered on pushes to the main branch
- Terraform Workflow: A reusable workflow that handles Terraform operations:
- Initializes Terraform with the appropriate backend configuration
- Validates the Terraform configuration
- Creates or selects the appropriate workspace based on the environment
- Applies or destroys the infrastructure based on the configuration in
destroy_config.json
- Java 17 or higher
- Maven
- AWS account with appropriate permissions
- Terraform 1.8.3 or compatible version
-
Clone the repository:
git clone https://github.com/yourusername/Terraform-Infra-Pipeline.git cd Terraform-Infra-Pipeline -
Build the application:
mvn clean install
-
Run the application:
mvn spring-boot:run
-
Access the API at http://localhost:8080/api/time
To deploy the infrastructure manually:
-
Navigate to the infra directory:
cd infra -
Initialize Terraform:
terraform init \ -backend-config="bucket=your-state-bucket" \ -backend-config="key=Terraform-Infra-Pipeline" \ -backend-config="region=sa-east-1" \ -backend-config="dynamodb_table=your-lock-table"
-
Select or create a workspace:
terraform workspace select dev || terraform workspace new dev
-
Apply the configuration:
terraform apply -var-file="./envs/dev/terraform.tfvars"
To control whether infrastructure should be destroyed or applied, modify the destroy_config.json file. Set values to true to destroy the environment or false to apply/maintain the infrastructure:
{
"dev": false,
"prod": false
}- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature-name - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin feature/your-feature-name - Submit a pull request