Automating AWS-EC2 via Terraform
Terraform is a famous open-source tool which uses the concept of Infrastructure As Code for the automation and provisioning of many cloud, Infrastructure or such services. It was developed by HashiCorp, it follows a Freemium business model. It delivers consistent workflows to provision, secure, connect, and run any infrastructure for any application.
Infrastructure As Code is a concept or a well-defined process of management and provisioning cloud based systems, data centers or such infrastructures through declarative configuration files, rather than physical hardware configuration or some other configuration management tools.
AWS-EC2 Creation using Terraform
Using terraform to provision AWS Elastic Cloud Compute instances is a quiet simple process to follow. In this example we will be creating an EC2 instance using our terraform configuration. We will be creating a very simple and basic EC2 instance using our example.
- Setup AWS Command Line Inerface (CLI)
First of all, one must have AWS CLI setup in local machine or the machine from where you are using terrform. To setup AWS CLI you can easily follow the steps provided by AWS official documentation here.
2. Installation of Terraform
We can easily get the desired terraform version according the operating system we are using from here. After installation we can verify the version using the simple command mentioned below;
$ terraform --version
Here is the output of the command
3. Writing the Terraform Configuration
Here we will be writing a terraform configuration;
# Create a new instance of the latest Ubuntu 20.04 on an
# t2.micro node with an AWS Tag naming it "HelloWorld"terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}# Configure the AWS Provider
provider "aws" {
region = "us-east-1"
}# Setting up the EC2 instace named as testingweb
resource "aws_instance" "testingweb" {
ami = "ami-0dba2cb6798deb6d8"
instance_type = "t2.micro"tags = {
Name = "HelloWorld"
}
}
- provider “aws”, this is to set the service provider, you are using. So here we are setting up EC2, so we have write aws here.
- resource “aws_instance” “testingweb” resource is to tell what service of AWS we are using, here we are using EC2 which is an AWS Instance so aws_instance would be hardcoded and testingweb is the name we have assign to it.
4. Execution of the Terraform Configuration
First of all for the execution of terraform config file, we must have to save our config using the extension .tf
Secondly for starting the execution, run the command
terraform init
This command initialize the directory, and the output will be as shown;
After the initialization step, second we have to validate whether our config file is actually correct or not, for this we can execute the command
terraform validate
Here is the output of this command
Once done with the validation step, Now comes the command for plan as shown;
terraform plan
It is used to create an execution plan that the config file will follow. It determines what actions are necessary to achieve the desired state specified in the configuration files. Output is attached here;
After the successful execution plan, now its time to finally apply the configuration using the command
terraform apply
One must run the command in the same directory as the example.tf
file you created. Output is here
This will ask to input the value, so type yes
in order to proceed and complete the execution. Executing the plan will take a few minutes since Terraform waits for the EC2 instance to become available.
After the completion of the apply command, your EC2 instance is ready, up and running, you can have a look at your AWS console in order to validate whether it is working or not.
Here you can see the AWS console window with the HelloWorld EC2 instance running.
Further Options to Explore
We have used very minimalist options in our terraform configuration filein order to just run the EC2 instance. There are alot of options for an EC2 instance which we can control and modify using our terraform configuration like managin security groups, applying roles, controlling the AMI, hardware configuration etc etc.
For this thing, here we can see in our picture that the options are available and can be used for further provisioning of our infrastructure according to our needs.
In a Nutshell
Concluding the thing, terraform is indeed a very powerful provisioning tool through which one can easily control, manage and update the whole infrastructure and cloud system using few simple configuration files.
Here is the code and config uploaded on git for usage and testing purposes. Happy coding.!