I share with you some key steps to manage infrastructure as code.
I. Non-Variable
1. Install Terraform:
You can install Terraform by downloading the binary distribution for your operating system from the Terraform website. After downloading, extract the binary and add it to your system's PATH.
2. Define Infrastructure as Code (IaC):
Create a directory for your Terraform configuration files. Within this directory, define .tf
files that describe the infrastructure resources you want to provision. These files typically include:
- Provider Configuration: Declare the cloud provider you're using (e.g., AWS, Azure, Google Cloud) and any required authentication details.
- Resource Configuration: Define the infrastructure resources you want to create, such as virtual machines, networks, databases, and storage buckets.
3. Initialize Terraform:
Navigate to your Terraform configuration directory in the terminal and run terraform init
. This command initializes the directory and downloads any necessary plugins and modules.
4. Write Terraform Configuration:
Write your Terraform configuration files (.tf
) using the HashiCorp Configuration Language (HCL). Define providers, resources, variables, outputs, and any other necessary configurations.
5. Plan Infrastructure Changes:
Run terraform plan
to create an execution plan. Terraform examines your configuration and determines what actions are necessary to achieve the desired state. It does not execute the plan but shows you what will happen when you apply the configuration.
6. Apply Infrastructure Changes:
Once you review the plan and are satisfied with the proposed changes, you can apply the changes by running terraform apply
. Terraform will execute the plan and create, update, or delete resources as needed.
7. Maintain Infrastructure:
As your infrastructure evolves, continue to manage it using Terraform. Make changes to your Terraform configuration files as necessary and apply those changes using t
erraform apply
.
Additional Tips:
- Version Control: Store your Terraform configuration files in version control (e.g., Git) to track changes and collaborate with team members.
- Modules: Use Terraform modules to encapsulate reusable components of your infrastructure configuration.
- Variables and Outputs: Utilize variables to parameterize your configuration and outputs to extract useful information about your infrastructure.
Example Terraform Configuration:
Here's a simple example of a Terraform configuration file that provisions an AWS EC2 instance:
hclprovider "aws" {
region = "ap-southeast-1"
}
resource "aws_instance" "example" {ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro"
}
This configuration declares an AWS provider in the us-west-2
region and creates an EC2 instance using the specified AMI and instance type.
II. Variable
1. Create Terraform Variable Files:
Create separate Terraform variable files for each environment (e.g., dev.tfvars
, prod.tfvars in envs folder
). These files will contain environment-specific variable values.
Example dev.tfvars
:
hclregion = "us-west-2" instance_type = "t2.micro"
Example prod.tfvars
:
hclregion = "us-east-1" instance_type = "t2.large"
2. Reference Variables in Terraform Configuration:
Reference the variables defined in your variable files in your Terraform configuration files using interpolation syntax.
Example main.tf
:
hclprovider "aws" {
region = var.region
}
resource "aws_instance" "example" {ami = "ami-12345678"# AMI ID specific to your environmentinstance_type = var.instance_type}
3. Run Terraform Commands with Variable Files:
When running Terraform commands, specify the variable files for the appropriate environment using the -var-file
option.
For example, to apply changes to the development environment:
bashterraform apply -var-file=../envs/dev.tfvars
For the production environment:
bashterraform apply -var-file=
../envsprod.tfvars
3. Run Terraform Destroy
Once you're in the directory, execute the following command:
bashterraform destroy
Additional Tips:
- Use
.gitignore
to exclude sensitive variable files (e.g., *.tfvars
) from version control. - Define default variable values in your Terraform configuration files or in a
variables.tf
file to provide fallback values if variables are not defined in the variable files.
.gitignore
to exclude sensitive variable files (e.g., *.tfvars
) from version control.variables.tf
file to provide fallback values if variables are not defined in the variable files.