This Terraform project automates the creation of an EC2 instance on AWS. It also demonstrates the usage of Terraform workspaces for managing multiple environments.
This Terraform project defines the infrastructure needed to create an EC2 instance on AWS. It uses Terraform workspaces to manage separate environments such as development, staging, and production.
Before you begin, ensure you have the following prerequisites installed:
- Terraform CLI
- AWS CLI configured with appropriate access credentials
- AWS account
The project structure is as follows:
terraform-ec2-instance/
├── main.tf
├── variables.tf
└── terraform.tfvars
main.tf
: Contains the Terraform configuration for creating the EC2 instance.variables.tf
: Defines input variables used in the Terraform configuration.terraform.tfvars
: Specifies the values for input variables.
Terraform workspaces are useful for managing multiple environments (such as development, staging, and production) within a single Terraform configuration. Workspaces allow you to maintain separate state files for each environment while using the same configuration code. This helps in organizing and managing infrastructure resources more efficiently.
Modify the variables.tf
and terraform.tfvars
files to customize your EC2 instance configuration. Key variables include:
region
: AWS region where the EC2 instance will be created.ami
: ID of the Amazon Machine Image (AMI) to use for the EC2 instance.instance_type
: Type of EC2 instance to launch (e.g., t2.micro).key_name
: Name of the SSH key pair used to access the instance.
Refer to the Terraform documentation for more information on configuring EC2 instances and input variables.
Follow these steps to deploy the EC2 instance using Terraform:
-
Navigate to the project directory:
cd terraform-ec2-instance/
-
Initialize Terraform:
terraform init
Create separate Terraform workspaces for each environment:
-
Development:
terraform workspace new development
-
Staging:
terraform workspace new staging
-
Production:
terraform workspace new production
-
Select the desired workspace (e.g., development, staging, production):
terraform workspace select development
-
Review the Terraform plan:
terraform plan
-
Apply the Terraform configuration to create the EC2 instance:
terraform apply
To destroy the resources created by Terraform and clean up:
-
Select the appropriate workspace:
terraform workspace select development
-
Destroy the infrastructure:
terraform destroy
-
Repeat the above steps for other workspaces as needed.
Benefits of Terraform Workspaces:
- Isolation: Each workspace maintains its own state file, allowing changes to be scoped to that environment only.
- Efficiency: Simplifies management of multiple environments with shared configuration code.
- Safety: Reduces the risk of accidentally modifying resources in the wrong environment.
- Consistency: Ensures consistent configurations across environments while allowing for environment-specific customization.