|
| 1 | +# Terraform Infra Pipeline |
| 2 | + |
| 3 | +A Spring Boot application with Terraform infrastructure as code and GitHub Actions CI/CD pipeline for automated deployment to AWS. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +The infrastructure is managed using Terraform and is deployed automatically to AWS using GitHub Actions workflows. The project supports both development and production environments. |
| 10 | + |
| 11 | +## Technologies Used |
| 12 | + |
| 13 | +- **Backend**: |
| 14 | + - Java 17 |
| 15 | + - Spring Boot 3.4.6 |
| 16 | + - Maven |
| 17 | + |
| 18 | +- **Infrastructure as Code**: |
| 19 | + - Terraform 1.8.3 |
| 20 | + - AWS S3 (for application resources and Terraform state) |
| 21 | + - AWS DynamoDB (for Terraform state locking) |
| 22 | + |
| 23 | +- **CI/CD**: |
| 24 | + - GitHub Actions |
| 25 | + |
| 26 | +## Project Structure |
| 27 | + |
| 28 | +``` |
| 29 | +├── .github/workflows/ # GitHub Actions workflow definitions |
| 30 | +│ ├── main.yml # Production deployment workflow |
| 31 | +│ └── terraform.yml # Reusable Terraform workflow |
| 32 | +├── infra/ # Terraform infrastructure code |
| 33 | +│ ├── envs/ # Environment-specific configurations |
| 34 | +│ │ ├── dev/ # Development environment |
| 35 | +│ │ └── prod/ # Production environment |
| 36 | +│ ├── backend.tf # Terraform backend configuration |
| 37 | +│ ├── destroy_config.json # Configuration for infrastructure destruction |
| 38 | +│ ├── main.tf # Main Terraform resources |
| 39 | +│ ├── provider.tf # AWS provider configuration |
| 40 | +│ └── variables.tf # Terraform variables |
| 41 | +└── src/ # Spring Boot application source code |
| 42 | + ├── main/ |
| 43 | + │ ├── java/ |
| 44 | + │ │ ├── com/api/terraform/pipeline/terraforminfrapipeline/ |
| 45 | + │ │ │ └── TerraformInfraPipelineApplication.java # Main application class |
| 46 | + │ │ ├── controller/ |
| 47 | + │ │ │ └── TesteController.java # REST controller |
| 48 | + │ │ └── dto/ |
| 49 | + │ │ └── response/ |
| 50 | + │ │ └── TimeResponse.java # Response DTO |
| 51 | + │ └── resources/ |
| 52 | + │ └── application.properties # Application configuration |
| 53 | + └── test/ # Test source code |
| 54 | +``` |
| 55 | + |
| 56 | +## API Endpoints |
| 57 | + |
| 58 | +- `GET /api/time`: Returns the current time in São Paulo, Brazil timezone in the format "dd/MM/yyyy HH:mm:ss" |
| 59 | + |
| 60 | +Example response: |
| 61 | +```json |
| 62 | +{ |
| 63 | + "time": "01/01/2023 12:00:00" |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## Infrastructure |
| 68 | + |
| 69 | +The project uses Terraform to manage AWS infrastructure: |
| 70 | + |
| 71 | +- Creates an S3 bucket with a name based on the environment (dev or prod) and a random suffix |
| 72 | +- Uses S3 for Terraform state storage |
| 73 | +- Uses DynamoDB for state locking |
| 74 | +- Deployed to the AWS sa-east-1 region (South America - São Paulo) |
| 75 | + |
| 76 | +## CI/CD Pipeline |
| 77 | + |
| 78 | +The project uses GitHub Actions for CI/CD: |
| 79 | + |
| 80 | +- **Production Deployment**: Triggered on pushes to the main branch |
| 81 | +- **Terraform Workflow**: A reusable workflow that handles Terraform operations: |
| 82 | + - Initializes Terraform with the appropriate backend configuration |
| 83 | + - Validates the Terraform configuration |
| 84 | + - Creates or selects the appropriate workspace based on the environment |
| 85 | + - Applies or destroys the infrastructure based on the configuration in `destroy_config.json` |
| 86 | + |
| 87 | +## Setup and Installation |
| 88 | + |
| 89 | +### Prerequisites |
| 90 | + |
| 91 | +- Java 17 or higher |
| 92 | +- Maven |
| 93 | +- AWS account with appropriate permissions |
| 94 | +- Terraform 1.8.3 or compatible version |
| 95 | + |
| 96 | +### Local Development |
| 97 | + |
| 98 | +1. Clone the repository: |
| 99 | + ```bash |
| 100 | + git clone https://github.com/yourusername/Terraform-Infra-Pipeline.git |
| 101 | + cd Terraform-Infra-Pipeline |
| 102 | + ``` |
| 103 | + |
| 104 | +2. Build the application: |
| 105 | + ```bash |
| 106 | + mvn clean install |
| 107 | + ``` |
| 108 | + |
| 109 | +3. Run the application: |
| 110 | + ```bash |
| 111 | + mvn spring-boot:run |
| 112 | + ``` |
| 113 | + |
| 114 | +4. Access the API at http://localhost:8080/api/time |
| 115 | + |
| 116 | +### Infrastructure Deployment |
| 117 | + |
| 118 | +To deploy the infrastructure manually: |
| 119 | + |
| 120 | +1. Navigate to the infra directory: |
| 121 | + ```bash |
| 122 | + cd infra |
| 123 | + ``` |
| 124 | + |
| 125 | +2. Initialize Terraform: |
| 126 | + ```bash |
| 127 | + terraform init \ |
| 128 | + -backend-config="bucket=your-state-bucket" \ |
| 129 | + -backend-config="key=Terraform-Infra-Pipeline" \ |
| 130 | + -backend-config="region=sa-east-1" \ |
| 131 | + -backend-config="dynamodb_table=your-lock-table" |
| 132 | + ``` |
| 133 | + |
| 134 | +3. Select or create a workspace: |
| 135 | + ```bash |
| 136 | + terraform workspace select dev || terraform workspace new dev |
| 137 | + ``` |
| 138 | +
|
| 139 | +4. Apply the configuration: |
| 140 | + ```bash |
| 141 | + terraform apply -var-file="./envs/dev/terraform.tfvars" |
| 142 | + ``` |
| 143 | +
|
| 144 | +## Infrastructure Destruction |
| 145 | +
|
| 146 | +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: |
| 147 | +
|
| 148 | +```json |
| 149 | +{ |
| 150 | + "dev": false, |
| 151 | + "prod": false |
| 152 | +} |
| 153 | +``` |
| 154 | +
|
| 155 | +## Contributing |
| 156 | +
|
| 157 | +1. Fork the repository |
| 158 | +2. Create a feature branch: `git checkout -b feature/your-feature-name` |
| 159 | +3. Commit your changes: `git commit -am 'Add some feature'` |
| 160 | +4. Push to the branch: `git push origin feature/your-feature-name` |
| 161 | +5. Submit a pull request |
| 162 | +
|
| 163 | +## License |
| 164 | +
|
| 165 | +This project is licensed under the MIT License - see the LICENSE file for details. |
0 commit comments