This Terraform module deploys a simple web application on AWS using EC2 instances and an Application Load Balancer (ALB). The module is designed to be flexible and easy to use, enabling you to quickly set up a basic web application infrastructure.
- Features
- Requirements
- Usage
- Future Improvements
- Gotchas
- Creates EC2 instances for running the web application
- Sets up an Application Load Balancer to distribute incoming traffic to the instances
- Configures security groups for the instances and ALB
- Supports customizing the number of instances, instance type, and other parameters
- Supports multiple environments using Terraform workspaces
- Terraform v0.13 or higher
- AWS provider v3.0 or higher
Clone the repository:
git clone https://github.com/cristisulighetean/terraform-aws-simple-app.git
terraform workspace new dev
terraform workspace new prod
# List available workspaces
terraform workspace list
# Switch to an existing workspace
terraform workspace select prod
-
Provide the backend configuration variables in the backend-config folder
-
Create a new folder in the backend-infrastructure folder called
backend-config
-
Create two files in the backend-config folder called
dev.tfvars
andprod.tfvars
-
Fill out the
dev.tfvars
andprod.tfvars
files with the following variables:bucket = bucket_name key = key_name for the terraform.tfstate file region = region_name dynamodb_table = dynamodb_table_name for the terraform state locking
-
-
Provide the necessary variables for the
backend-infrastructure
project- Use the
dev.tfvars
andprod.tfvars
files from thebackend-config
folder, they will be used in the main.tf of the backend-infrastructure project
- Use the
-
Create the remote backend infrastructure
-
First, we have to run the project with the local backend to then switch to the remote backend. Make sure that the remote backend is commented out.
# backend "s3" { # encrypt = true # }
-
Deploy the necessary resources.
terraform workspace select dev terraform init terraform apply -var-file=backend-config/dev.tfvars
-
Uncomment the remote backend configuration from step 1 and initialize the remote backend.
terraform init -backend-config=backend-config/dev.tfvars
-
Repeat the steps above for the prod environment by replacing dev with prod.
-
-
Create the necessary files for the variables in the
web-app
folder-
Use the
backend-config
folder for the remote backend configuration. It should containdev.tfvars
andprod.tfvars
files. All the variables have to match to the previously created remote backend besides thekey
bucket = bucket_name key = key_name for the terraform.tfstate file region = region_name dynamodb_table = dynamodb_table_name for the terraform state locking
-
Create a new folder in the
web-app
folder calledvariables
. Inside thevariables
folder, create two files nameddev.tfvars
andprod.tfvars
. These files will be used for the variables needed for the root project. Provide the following variables in the respectivedev.tfvars
andprod.tfvars
filesregion = region_name web_app_config = { bucket_prefix = bucket_prefix_name domain = domain_name instance_type = instance_type_name environment = environment_name } db_credentials = { name = db_name user = db_user password = db_password instance_type = instance_type_name }
-
-
Deploy the web-app
- Make sure you are connected to the AWS account via the CLI
- Deploy the project for the dev environment
terraform workspace select dev terraform init -backend-config=backend-config/dev.tfvars terraform plan -var-file=variables/dev.tfvars terraform apply -var-file=variables/dev.tfvars
- Deploy the project for the prod environment
terraform workspace select prod terraform init -backend-config=backend-config/prod.tfvars terraform plan -var-file=variables/prod.tfvars terraform apply -var-file=variables/prod.tfvars
-
After deployment, update the name servers for your domain at your domain registrar to delegate DNS management to Route 53. To do this, go to your domain registrar's control panel, find the settings for managing DNS or name servers, and replace the existing name servers with the name servers provided by Route 53 in the NS record of your hosted zone.
Perform terraform destroy on both projects folders starting with the web-app
. Make sure to pass the appropriate -var-file
flag for the environment you're destroying. Check the AWS Management Console to ensure that everything was destroyed.
- Add support for Auto Scaling Groups to automatically scale the number of instances based on demand.
- Integrate with additional AWS services, such as RDS for database support or S3 for static asset storage.
- Improve the module's flexibility by allowing users to configure more parameters, such as custom security group rules.
- Enhance the health check settings of the target group for better instance health monitoring.
- Add support for HTTPS and SSL/TLS certificates for secure connections.
- Make sure to use a unique name for the security groups and load balancers, as duplicate names will cause errors.
- Ensure that the specified AMI ID is valid and available in the selected region.
- Make sure you are in the correct AWS region when looking up the deployed instances.
- When destroying the infrastructure, pass the appropriate -var-file flag for the environment being destroyed.
- Always ensure that you have selected the correct Terraform workspace before deploying or destroying resources.