Previous Next
This is the second blog post of terraform series. In this blog post, we are going to discuss, how to create/update/delete resource on AWS using the simple terraform configuration file.
In this Blog, we will use “HCL(Hashicorp configuration language)” to
write to terraform configuration files, which is recommended by Hashicorp.
Extension of configuration file written in HCL (Hashicorp configuration language)
is “.tf”.
But terraform Configuration files can also be written JSON-compatible format as well. This is mostly used, when we generate the configuration file programmatically using automation tool/code. Extension of terraform configuration file written in JSON-compatible format “.tf.json”.
We are going to cover below topics of terraform.
Terraform providers
Terraform provider resources
Terraform Configuration file
Terraform basic command
terraform init
terraform validate
terraform plan
terraform apply
terraform destroy
terraform fmt
Terraform Providers
Problem
We have a requirement to automate life cycle of Cloud resource
from different cloud like AWS, Azure, GCP etc. To achieve this, we have to understand
each Cloud provider API/Command line, which is a very complex task, As every
Infrastructure provider has its own API and command line and way of consumption
is different.
Solution
Terraform have a in-built integration with different
Infrastructure provider, provider can be IaaS, SaaS and PaaS. If you are using
terraform then You just need to provide the provider name from which you want
to manage the resource in terraform configuration file, all the complexity of
API calling will be taken care by terraform.
In simple terms, The Infrastructure providers, which terraform
have integration plugins are called Terraform
provider.
Terraform supports more than 100 Infrastructure providers.
terraform categorize the provider list as per their type and market use.
Major
Cloud: Azure, AWS, GCP, Oracle, VMware etc.
Cloud:
Nutanix, Softlayer etc
Infrastructure
software : chef ,
Kubernetes , RabbitMQ etc
Network
: Cisco ACI, Infoblox , F5
BIG-IP etc
VCS
: Bitbucket,GitHub ,GitLab
Monitoring
and System Management : Datadog ,
Auth0 etc
Database
: InfluxDB ,MongoDB
Atlas,MySQL,PostgreSQL
Miscellaneous: Cobbler, TLS etc
Community:
These plugins has been created by vendor/user and not officially maintained
by Hashicorp. Like Jira, SQL etc.
To check all supported terraform provider details. Please go to
below url.
https://www.terraform.io/docs/providers/index.html
We need to provide the provider details in terraform configuration
file. The way to define provider in configuration file, will be discussed in later
section of this blog.
Below is the simple example to define AWS provider in terraform
configuration file.
Terraform provider resources
Now we have understanding about terraform provider. Infrastructure
resources which you want to manage using terraform configuration file are
called provider resources. Like EC2, Load balancer in AWS.
So to create/update/delete any Infrastructure resources using
terraform configuration file, you have to specify the terraform provider name
and resource name.
Below is the simple example to define AWS EC2 Instance in
terraform configuration file.
Terraform
Configuration file
As we discussed, terraform have its own configuration language
called HCL (Hashicop configuration language). Extension of terraform
configuration file is “.tf”
Now, we are going to create simple configuration file to create
the EC2 Instance on AWS.
Step: 1 Create a new
directory on your system.
# mkdir ec2instnace
Step: 2 Navigate
Inside the created directory
# cd ec2instnace
Step: 3 create
file name myec2example.tf with below
sample code. remember extension should be “.tf”
Terraform basic command
Once terraform configuration file is created and saved, then go to
directory where terraform configuration file exist (cd DirectoryName) and
execute below commands.
Command -1
# terraform init
This command is used to pull the required plugin from Hashicorp
site and copy into to your local directory. If Internet is not working on
system, this command will get failed.
After successful execution, this command creates one more folder in your directory, which contains the required plugin. The name of folder is “. terraform”
Command -2
# terraform validate
This command is used to validate basic syntax check of terraform
configuration files in a present directory.
Command -3
# terraform plan
This command is used to verify the behavior of terraform
configuration file and provides the overall picture to developer, i.e how many
resources are going to delete, update and create using this configuration file.
This command just provides the picture and does not perform any action for
resource creation/ updation /deletion on infrastructure.
You find + , - , -/+ and ~ symbol in against the resource in command output, below is the meaning of each symbol.
+ resource will be added.
- resource will be destroyed.
-/+
resource will be destroyed and then added again.
~ resource will be changed
Command -4
# terraform apply
If you are satisfied with terraform plan output, then you can use
“Terraform apply” command to create/update/delete resource.
This command shows action summary on terminal window like
terraform plan and ask to confirm the change with “yes” option.
After entering “yes” terraform create/update/delete the resource as per the configuration files.
Command -5
# terraform destroy
This command is used to delete the resource, which were created
from terraform. If you execute the destroy command in same directory, then
terraform will delete the created resources.
For this terraform use state file, if you check your working directory
then you will find one new file is automatic created after ‘terraform apply”
command, file name is terraform.tfstate.
This is called terraform state file.
Terraform use this state file to maintain the resource state,
never modify this file. we will discuss “state file” concept in upcoming blog
post.
Command -6
# terraform fmt
Sometimes Proper alignment of code is not done by developers, due
to which, it becomes very difficult to understand by other developers. The terraform fmt command is used to format
and style terraform configuration file, so that it will be in proper readable
format.
Contents of configuration file before “terraform fmt” Command
Contents
of configuration file after “terraform fmt” Command
Nice content
ReplyDelete