Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It allows you to define and provision infrastructure resources, such as servers, databases, and networking components, using a high-level configuration language (HCL). Terraform can manage infrastructure across various cloud providers (like AWS, Azure, and Google Cloud) as well as on-premises environments, making it a versatile tool for automating infrastructure management.
With Terraform, you can version your infrastructure, easily replicate environments, and use a declarative approach to define the desired state of your infrastructure, which Terraform then creates and maintains.
- Install Terraform
- Install Docker
- Create Docker Hub account
- Create an AWS account
- Download AWS CLI
- Clone the project e.g.
https://github.com/GabriellBP/terraform-example.git
- Create Docker Image and publish it to the Docker Hub:
- Go to
terraform-example/app/
- Run the command:
./mvnw install
- Update the Dockerfile if necessary with a different Java version and a different jar name (line 3: target/app-0.0.1-SNAPSHOT.jar)
- Run the command:
docker build -t <your-docker-hub-username>/terraform-example:latest --push .
- Or use the following command to build the image for more than one platform (arm and amd in this case):
docker buildx build --platform linux/amd64,linux/arm64 -t <your-docker-hub-username>/terraform-example:latest --push .
- M1/M2 has arm based while the linux is amd based, depending on your system and the system of the AWS machine, maybe you need to build the image for more than one platform
- To run the previous command you need to enable the docker contained image store
- Or use the following command to build the image for more than one platform (arm and amd in this case):
- You can check the image on your docker hub account or run the command:
docker run -p 8080:8080 <your-docker-hub-username>/terraform-example:latest
and see if the api is working properly:curl localhost:8080
- Go to
- Grant programmatic access
- Go to you AWS Account and create an AWS User in the IAM (Identity and Access Management) dashboard
- I added an Administrator Access policy for this example although it is not recommended, to make it easier - If you create an Administrator with full access rights, remember to delete it after you run this example
- Create and save the access keys (Local code)
- Connect local AWS CLI with remote AWS Account
- On your local run the command:
aws configure
- Fill the fields with your recent user access keys
- On your local run the command:
- Open the file
terraform-example/infra/start_docker.sh
and replacegabriellbp/terraform-example
with your docker hub image<your-docker-hub-username>/terraform-example
- Run the command
terraform init
(inside the folderterraform-example/infra/
) - Run the command
terraform plan
and check all changes that will be applied to your infra - Run the command
terraform apply
and write yes to confirm- if you get an error about the resource
keypair
you can delete it from theterraform-example/infra/main.tf
file or jump to step 15 (configure you ssh access) before apply the terraform changes and create your infra
- if you get an error about the resource
- Check the ec2 instance created in your AWS account under the instances page
- Click in the created instance to see more details, copy the public ipv4 DNS in your browser or execute the command
curl <your-public-ipv4-DNS
to see the message: Hello Terraform!- Rembember to use
http
and nothttps
- Rembember to use
- To Configure your SSH Access (Optional):
- Run the command
ssh-keygen
(check this for windows systems) - Open the file
terraform-example/infra/main.tf
and replace in the line 33 the commandfile("~/.ssh/id_ed25519.pub")
using your public key address - run the command
terraform apply
- You can check the key pair created on your Amazon Clound account under the page
Key Pairs
- To access the remote machine you can run the command
ssh ec2-user@<your-public-ipv4-DNS>
- Run the command
- To clean your environment and destroy your infra you can simply run the command
terraform destroy
under theterraform-example/infra/
folder- Confirm everything and then write yes in the command line
- Terraform
- Docker
- Java
- Spring
- AWS
- AWS EC2
- SSH access