This repository is a study case for provisioning simple infrastructure with Terraform and deploying basic Spring Boot application on AWS.
- Requirements
- Infrastructure
- Running Locally
- Pack app
- Build app image
- Preparing entrypoint
- Creating programmatic user (Terraform)
- Configuring AWS CLI
- Generating SSH Key Pair
- Attach your IP to SSH Rule
- Set up Terraform
- Formatting and validating file
- Planning and provisioning resources
- Accessing application
- Destroying infrastructure provisioned
- Next Steps with Terraform
- Links references
- OpenJdk v21
- Apache Maven
- AWS CLI
- Terraform CLI
- Make (optional)
- EC2 Instance
- Key pair for SSH access
- Security Groups
- Allow Http
- Allow Https
- Allow SSH access to EC2 (from a IPs list)
- User data file to prepare and run our application
./mvnw spring-boot:runcurl -i http://localhost:8080Accessing the application url, must be possible see the instant datetime.
{"datetime": "2025-11-09T00:00:00"}In root path, run the following command:
make pack-appmake build-image# before push image, do login
docker login ...
make push-imageIn infra/userdata.sh replace <docker-username> to your real docker username.
- Access the AWS Console
- Go to
IAMsection. - In left sidebar click on
User Groupsand click on Create User Group. - In create user group page, give a name to group of user.
- In persmission section, select only
AmazonEC2FullAccessand click on create user group. - In left sidebar click on
Usersand click on Create User. - In create user page, give a name to new user and click on next.
- In permissions page keep selected
Add user to group, select the group of user created previously and click on next and finally, Create User. - With user create, click on him to see details and following click on Create Access Key.
- In access key page, select the use case, something like
Local CodeorCLI, mark confirmation checkbox, click on next and finally Create Access Key.
- With the keys of access and secret, run
aws configurein tyour terminal - Provide access key of programmatic user, press enter
- Provide secret key of programmatic user, press enter
- Provide a region where the resources are been created
- Ignore output, press enter
ssh-keygen -t rsa -b 4096 -C "EC2 Instance"If you save out of default path or with another name, you must be change the ssh key path/name on infra/main.tf file.
In the infra/main.tf just replace all occurrences of <your-ip> by your real IP.
From this section onwards, all commands must be executed in the
infrafolder.
With Terraform installed run the following command in root path of this project, to set up terraform with AWS.
terraform init# format the file
terraform fmt
# check for errors of syntax and etc.
terraform validate# visualize the Terraform plan before to applying
terraform plan
# provision resources on cloud provider
terraform applyIn EC2 page, click on EC2 instance created by Terraform, search by Public IPV4 address and click on it, to access the application.
The app url must be access without https, because is not fully configured.
ssh -i \
~/.ssh/id_rsa \ # private ssh key path
ec2-user@<public-ip-instance>terraform destroy- Store the Terraform state in a private AWS S3 Bucket (automatically)
- Create a specficic user to IAC (Terraform) and used it to provision infra
- Provision ECR private repository to store all app images instead store on Docker Hub
This project is under MIT License.