Provisioning Java Spring Boot App Infrastructure with Terraform
Fast recall
Quick revision
Core answer
Whether you can describe end-to-end infrastructure provisioning for a real Spring Boot application, covering compute, networking, database, and secrets management.
Describing only the compute layer (EC2 or ECS) without covering VPC networking, security groups, database connectivity, and environment variable injection.
Walk through the full stack: VPC, ALB, ECS Fargate task with the Spring Boot container, RDS in a private subnet, and Secrets Manager for credentials injected as environment variables — all wired together with proper IAM roles.
Interview-ready guide
Interview answer
Provisioning production infrastructure for a Spring Boot application with Terraform means declaring the compute, database, networking, and load balancing resources that the application needs, then managing them as versioned, reviewable code.
Compute and networking
- For ECS Fargate: declare a
aws_ecs_cluster,aws_ecs_task_definition(with CPU, memory, container image, environment variables, and log driver configuration), andaws_ecs_service(with desired count, load balancer attachment, and network configuration referencing the VPC and private subnets) - The task definition references the ECR repository URL for the container image —
aws_ecr_repository.payment_service.repository_urlcreates a dependency between the repository and the task definition; Terraform creates the ECR repository before the task definition - Use private subnets for the ECS tasks and public subnets only for the ALB — the tasks should not be directly reachable from the internet; traffic enters through the ALB, which terminates HTTPS and forwards to tasks on port 8080
Database
aws_db_instancedeclares an RDS instance withengine,instance_class,allocated_storage,db_name,username, andpassword; the password should come from AWS Secrets Manager or be a sensitive variable — never hardcoded in the HCL- The RDS instance goes in a separate security group that only accepts connections from the ECS task's security group —
aws_security_group_rule "allow_from_ecs"withsource_security_group_id = aws_security_group.ecs_tasks.idenforces this at the network layer - Use
aws_db_subnet_groupwith private subnets andpublicly_accessible = false— the database should be unreachable from outside the VPC under any circumstances
Spring Boot environment variables
- Pass database URL and credentials to the ECS task via AWS Secrets Manager references in the task definition —
valueFrom = aws_secretsmanager_secret_version.db_password.arninjects the secret value at task startup without hardcoding it in the task definition or state file - Terraform outputs the ALB DNS name and RDS endpoint so application configuration (DNS records, other Terraform configurations) can reference them without hardcoding:
output "alb_dns" { value = aws_lb.main.dns_name }
Provisioning Java Spring Boot App Infrastructure with Terraform
Direct Answer
Provision a Spring Boot app on AWS ECS Fargate with: VPC and private subnets, ALB, ECS cluster and task definition (with the Docker image), RDS in private subnet, and Secrets Manager for credentials.
Common Mistakes & Pitfalls
Describing only the compute layer (EC2 or ECS) without covering VPC networking, security groups, database connectivity, and environment variable injection.
Check your understanding
Answer these without looking back at the article.
- How do you handle database credentials in Terraform for ECS tasks?
- What is the difference between ECS EC2 and ECS Fargate?
- How do you implement autoscaling for an ECS Spring Boot service?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.