Table of contents
- Introduction
- Task 1: Create a Terraform configuration file to define a resource of AWS EC2 instance, Azure storage account, Google Compute Engine, etc. (anyone)
- Task 2: Check state files before running plan and apply commands & Use validate command to validate your tf file for errors and provide the Output generated by each commands.
- Task 3: Add a provisioner to the configuration file to configure the resource after it is created and use Terraform commands to apply for changes and destroy to remove resources.
- Task 4: Add lifecycle management configurations to the configuration file to control the creation, modification, and deletion of the resource and use Terraform commands to apply the changes.
- Conclusion
Introduction
Welcome back to Terraweek Day-3! In today's session, we're going to dive into the world of Infrastructure as Code (IaC) with Terraform. Specifically, we'll focus on creating an AWS EC2 instance using Terraform.
Task 1: Create a Terraform configuration file to define a resource of AWS EC2 instance, Azure storage account, Google Compute Engine, etc. (anyone)
Terraform uses configuration files written in HashiCorp Configuration Language (HCL) to define infrastructure resources. Terraform should be installed on your computer and also set up your AWS credentials.
Let's start by creating a Terraform configuration file, commonly named main.tf
, to define our EC2 instance resource.
create a new file: main.tf
start by specifying the AWS provider in your configuration file:
# main.tf
provider "aws" {
region = "eu-west-1" # Replace with your desired AWS region
}
resource "aws_instance" "demo_instance" {
ami = "ami-01dd271720c1ba44f" # Replace with your desired AMI ID
instance_type = "t2.micro"
tags = {
Name = "new_instance"
}
}
In the code( instance provisioning using code) above:
We specify the AWS provider with the desired region.
We define an EC2 instance using the
aws_instance
resource, naming it "my_ec2_instance."We specify the Amazon Machine Image (AMI) and instance type for our EC2 instance. Feel free to replace these values with your preferences.
Task 2: Check state files before running plan and apply commands & Use validate command to validate your tf file for errors and provide the Output generated by each commands.
Before we proceed, it's essential to check the state files and validate our configuration. Terraform maintains a state file that contains the current state of your infrastructure.
Checking State Files
To check the current state of your infrastructure, you can use the terraform show
command. Run the following command in your terminal:
terraform show
This will display the current state information for your resources.
Validation
It's also a good practice to validate your Terraform configuration for errors before applying it. Use the terraform validate
command:
terraform validate
This Result means that your Terraform configuration is free of syntax errors.
If there are any syntax or configuration errors in your main.tf
file, Terraform will report them.
Run terraform plan
and terraform apply
Terraform plan
Run terraform plan
. Terraform will create an execution plan.
Terraform apply
If the apply
command is run without any options it will run a terraform plan
first, ask the user to confirm the planned actions, and then execute those changes once confirmed.
Terraform will notify you on the command line once the EC2 instance is completed:
now, check in the AWS account if the instance has been created
Task 3: Add a provisioner to the configuration file to configure the resource after it is created and use Terraform commands to apply for changes and destroy to remove resources.
Provisioners in Terraform allow you to configure resources after they are created. Let's add a simple shell script provisioner to our EC2 instance.
# main.tf (Updated)
resource "aws_instance" "my_ec2_instance" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
provisioner "local-exec" {
command = "echo 'Hello from Terraform provisioner' > /tmp/terraform_provisioner.txt"
}
}
In the updated configuration, we've added a local-exec provisioner that runs a shell command to create a file on the EC2 instance.
To apply these changes, run:
terraform apply
And to remove the resources:
terraform destroy
Task 4: Add lifecycle management configurations to the configuration file to control the creation, modification, and deletion of the resource and use Terraform commands to apply the changes.
Terraform provides lifecycle management settings to control the behavior of resources during creation, modification, and deletion. Let's add some lifecycle management configurations to our EC2 instance.
# main.tf (Updated)
resource "aws_instance" "my_ec2_instance" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
lifecycle {
create_before_destroy = true
ignore_changes = [tags]
}
}
In this updated configuration:
create_before_destroy
ensures that a new EC2 instance is created before the old one is destroyed during updates.ignore_changes
specifies that changes to the EC2 instance's tags should be ignored.
To apply these lifecycle changes, run:
terraform apply
And to destroy the resources:
terraform destroy
Conclusion
Congratulations! You've successfully created an AWS EC2 instance using Terraform, added a provisioner to configure it, and implemented lifecycle management settings. Terraform's power lies in its ability to manage infrastructure as code, making it easier to create, update, and maintain your resources in the cloud.
Keep exploring Terraform to unleash its full potential in automating and managing your infrastructure efficiently. Stay tuned for more Terraweek sessions!