In this post, we will learn the basics of Terraform. We will launch a AWS EC2 instance using terraform.
In this post, we will learn the basics of Terraform. We will launch a AWS EC2 instance using terraform.
What is Terraform?
HashiCorp Terraform is an open source infrastructure as code (IaC) software tool that allows DevOps engineers to programmatically provision the physical resources an application requires to run.
Read more here: https://www.terraform.io/intro
HashiCorp Terraform is an open source infrastructure as code (IaC) software tool that allows DevOps engineers to programmatically provision the physical resources an application requires to run.
Read more here: https://www.terraform.io/intro
Prerequisite:
For this tutorial, you will need.
- AWS Account
- Download and install Terraform.
Let’s start
Log in to AWS and create an IAM Role which we will use with terraform. Get the access_key and access_secret this is required.
Create a new project directory. Inside the directory create a file ec2.tf and past the following code.
provider "aws" {
region = "us-west-2"
access_key = "<YOUR_ACCESS_KEY>"
secret_key = "<YOUR_SECRET_KEY>"
}resource "aws_instance" "myec2" {
ami = "ami-0ca285d4c2cda3300"
instance_type = "t2.micro"tags = {
Name = "terraform"
}
}
Now let’s understand the code. We have two blocks here. The provider block and resource block.
Provider:
Providers are a logical abstraction of an upstream API. They are responsible for understanding API interactions and exposing resources.
Check the available providers from: https://registry.terraform.io/browse/providers
Here in the code, we are using AWS as the provider. The terraform needs to authenticate with AWS in order to create EC2. For authentication, we are providing aws access_key and secret_key.
Resource:
In short resources are the services offered by the provider. For example AWS provides aws instance, aws_alb etc.
You can see the list of resources for AWS from: https://registry.terraform.io/providers/hashicorp/aws/latest/docs
Since we want to deploy EC2 instance. We are using aws_instance resource.
For this tutorial, you will need.
- AWS Account
- Download and install Terraform.
Let’s start
Log in to AWS and create an IAM Role which we will use with terraform. Get the access_key and access_secret this is required.
Create a new project directory. Inside the directory create a file ec2.tf and past the following code.
provider "aws" {
region = "us-west-2"
access_key = "<YOUR_ACCESS_KEY>"
secret_key = "<YOUR_SECRET_KEY>"
}resource "aws_instance" "myec2" {
ami = "ami-0ca285d4c2cda3300"
instance_type = "t2.micro"tags = {
Name = "terraform"
}
}
Now let’s understand the code. We have two blocks here. The provider block and resource block.
Provider:
Providers are a logical abstraction of an upstream API. They are responsible for understanding API interactions and exposing resources.
Check the available providers from: https://registry.terraform.io/browse/providers
Here in the code, we are using AWS as the provider. The terraform needs to authenticate with AWS in order to create EC2. For authentication, we are providing aws access_key and secret_key.
Resource:
In short resources are the services offered by the provider. For example AWS provides aws instance, aws_alb etc.
You can see the list of resources for AWS from: https://registry.terraform.io/providers/hashicorp/aws/latest/docs
Since we want to deploy EC2 instance. We are using aws_instance resource.
Creating Resource:
Now let’s start creating aws ec2 instance.
Open terminal and go to the project dir and run the following command.
terraform init
This should successfully initialize the terraform.
Next lets see how the terraform will create the EC2 instance. Go ahead and run
terraform plan
Terraform will print out the plan of how it is going to provision the resources.
Have a look at the plan and if you are okay with it. Lets create the resource.
terraform apply
This should create the AWS EC2. You can verify this by going to aws console instances page.
Now let’s start creating aws ec2 instance.
Open terminal and go to the project dir and run the following command.
terraform init
This should successfully initialize the terraform.
Next lets see how the terraform will create the EC2 instance. Go ahead and run
terraform plan
Terraform will print out the plan of how it is going to provision the resources.
Have a look at the plan and if you are okay with it. Lets create the resource.
terraform apply
This should create the AWS EC2. You can verify this by going to aws console instances page.
Destroying Resource:
Let’s destroy the AWS EC2 instance we created.
terraform destroy
This will destroy the resources created earlier.
Verify this by going to aws consol instances page.
Let’s destroy the AWS EC2 instance we created.
terraform destroy
This will destroy the resources created earlier.
Verify this by going to aws consol instances page.
Comments
Post a Comment