Behind the Scenes: How Terraform Works to Deploy AWS EC2 Instances

Terraform Work Flow:                  The user needs to create the Terraform configuration file, the terraform configuration file needs to b...


Terraform Work Flow:

                 The user needs to create the Terraform configuration file, the terraform configuration file needs to be in the .tf file.

The Terraform file defines the provision and creation of the resources. 

The second input source is a terraformed core that keeps an up-to-date state. How the current setup of the whole infrastructure looks like.

The Terraform core takes the input and figures the plan of what needs to be done.

It compares the state and checks that the current state is the configuration that you desired in the end result.

It figures out what needs to be done to get a desired state in the configuration file.


Providers: 

           This is the second component of Architecture. It gives the possibility to create infrastructure on different levels.

It has hundred of providers for the different technologies, and each provider gives access to the users to use the resources.

For example, Aws provides give access to hundred of AWS resources like Ec2 instance, user, etc.

Kubernetes providers access the commodities, resources like services, deployments, namespace, etc.


How to write terraform files, and provision the infrastructure in AWS?.

            I am going to create the directory for my purpose. inside the directory, we can add the terraform file.

command:  mkdir terraform

Now inside the directory, we are going to create the file called terraform.tf, now using any editor to

open the file to add the terraform codes on it.

commands: vi terraform.tf


How to add the Terraform code(HCL):

                           The Terraform registry itself gives the support to write the code for all the providers.

Now open the terraform file, and use the syntax from the terraforming registry. Now change the code for our used case.


provider "aws" {

  region     = "us-west-2"

  access_key = "my-access-key"

  secret_key = "my-secret-key"

}

We can give the region as per our requirement, here I am going to use the “us-east-2”, for access and
secret keys, need to take it from the IAM AWS service.

Now we are using the Terraform file to create the AWS resources, so we need AWS access.

The AWS has an IAM service, we need to use the user’s access key and secret key of the user.

If not aware of the IAM please check out our blog to create the user in IAM.

Our Demo code looks like this.


Now we are going to create the EC2 instance using the Terraform.

Using the Terraform registry reference, we are creating the ec2 instance.

Now use the below code to create only ec2-instance with default aws options.


resource "aws_instance" "foo" {

  ami           = "ami-005e54dee72cc1d00" # us-west-2

  instance_type = "t2.micro"

}

“Foo” needs to change the region, we are using us-east-Ohio, so change the region to “us-east-2” 

For AMI id varies depending on the AMI we are choosing, now I am going to choose amazon Linux

existing AMI, So give that AMI id in the code. Instance type basic free tire t2.micro fine for us. 

Once the code is updated save the terraform file.


As of now, Our terraform file was ready, now we are going to use that terraform file to create the ec2 instance using the terraform commands

Initialize the terraform using the command  terraform init


After the initialize, Apply the plan command to create the desired state of the infrastructure 

Command: terraform plan

Now apply the changes to the aws infrastructure as defined in the plan.

command: terraform apply

After issuing the terraform apply commander it will ask the option like the do you want to perform these actions, we need to give the yes command to run the apply command.


We must check our aws, whether the ec2-instance was created or not.

In our requirement, I want to create the ec2-instance in Ohio-us-east-2.


Ec2-instance was created using terraform successfully.

Now we don’t want the created instance so going to destroy the ec2-instance using the command “terraform destroy”

After issuing the destroy command, It will ask, do you really want to destroy all resources,

give yes to destroy the infrastructure created before.



Cross-check this operation in aws, The previously created AWS instance was destroyed successfully

In the Next Blog, we will see how to create AWS VPC, Subnet, S3, RDS using Terraform.

---------------------------------------!!!! Happy Learning with Techiev !!!!!!!!----------------------------------

------------------------Subscribe our Youtube Channel by clicking the below link---------------------- ----------------------------!!https://www.youtube.com/@techieview729!!---------------------







 

Name

AWS,14,Devops,23,linux,9,
ltr
item
Techie View: Behind the Scenes: How Terraform Works to Deploy AWS EC2 Instances
Behind the Scenes: How Terraform Works to Deploy AWS EC2 Instances
https://1.bp.blogspot.com/-bu_HXAF_lGk/YNBZBlTrNRI/AAAAAAAAAYA/rrIDasM54QcQqFIJpWllDqHlupiKitTDACLcBGAsYHQ/s320/aws%2Btf.jpeg
https://1.bp.blogspot.com/-bu_HXAF_lGk/YNBZBlTrNRI/AAAAAAAAAYA/rrIDasM54QcQqFIJpWllDqHlupiKitTDACLcBGAsYHQ/s72-c/aws%2Btf.jpeg
Techie View
https://www.techiev.com/2021/06/how-terraform-works-deploying-aws-ec2.html
https://www.techiev.com/
https://www.techiev.com/
https://www.techiev.com/2021/06/how-terraform-works-deploying-aws-ec2.html
true
7013663511659419322
UTF-8
Loaded All Posts Not found any posts VIEW ALL View Full Article Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy