Wednesday, May 5, 2021

Day2 - Terraform - Creating an EC2 instance using terraform

 github.com/zealvora/terraform-beginner-to-advanced-resource

👉  What is terraform registry...?
- Terraform registry enables the distrubution of terraform modules which are reusable configurations.
In fact, it is the place where we store all the information about the service providers and modules.
All the provider information is stored in Terraform registry. We can get example, code/documentaton details of all supported applications.

👉 What is the role of terraform init command...?
- The terraform init command is used to initialize the working directory containing terraform written code (or config files), and on the basis of that code, it will download the respective provider plugin like aws, gcp etc. This is the first command you run after writing your terraform configuration code.
To use a provider or module from the registry, simply add it to your configuration using the "terraform init" command, and Terraform will download anything it needs automatically.

👉 What are providers and resources in terraform...?
- Providers are cloud platform/applications that provides Infrastructure-as-a-Service for eg, aws, gcp that are supported by terraform. They come with plugins which is used to interact with the remote system.
The providers are the one who provide their services to us.

Resources are the sub-services provided by the cloud provides such as VPC, EC2, EBS in terraform. Resources are the most important element in the terraform, each resource block describes one or more infrastructure objects. Terraform resources are implemented by provider plugins. The terraform registry is the main directory of the publicly available terraform providers.

👉 what is the difference between imperative and declarative language...?
- Imperative language is like a Procedural language where steps plays an important role whereas in declarative language we are more focus on the final output rather than it's steps.
So, you can say, imperative language simply describes the control flow of computation and remmebers the state. Declarative programming simply expresses the logic of computation and have no knowledge of state of program.
Thats being said, you basically specify all the information that need to perform all the activity. Even the code is already implemented or executed earlier, if you run again, it will run and go throug the build process. These code may lack the intelligence. whicle in case of declrative language, you give some information about the infrascture, they will understand what to do. Once you execute the code, it will do everything it need to do. But if you rerun again, it will not execute if there is no change on the code. They are intelligent enough the tell you that the code you already executed has not new change.

 day2 - Terraform 5-5-2021

TF -> AWS (cloud) -> Provision server or other job

Code
-----
-----
-----
-----

write instruction (program)
- automation

Go to the site below to find the providers TF supports

https://registry.terraform.io

- broswer providers
- select aws


go to your windows machine
> mkdir terraform-ws
> cd terraform-ws
> mkdir basic; cd basic
> notepad first.tf
provider "aws {}    #

we are telling terraform, we want to do something on aws.
- detech aws, auto detech the provider and download the plugin.
- if you run this program again and again innfuture, you don't have to download


> dir
> terraform init    # you run this command first time.
they will automatically download the plugin

> cd .terraform\providers\registry.terraform.io\hashicorp\aws\3.38.0\windows_aws64
you will find the plugin this place
> dir

---------------------------------
something to know about aws
- Go to aws and create an account and copy seccret key and access key
- AWS works on region
- EC2 - Instance/OS/VM/server (azure also has simiar name)
  - inside the server, you have sub-services
    (volue, netwrok, load balance, security and more)
    (for os you need RAM/CPU/HD/Network ....)
  in terraform term, these sub-services are called resources.

- lets go to ec2 dashboard and
- click on instance
- click on launch new instance
- select OS (select amazon linux-2 -> copy ami-id...
- instance type - copy the type name t2.micro
- next configure instance detail, leave default
- next -> tag -> name=os3
- next security (leave default)
- click and launch
- proceed without a key pair (default setting)
These are the basic steps and we collect these steps. we have to convert these steps into terraform code.

everything you do manuall graphically, you have to copy every single step into code.

How do we know what keyword do they use on terraform.
First we collect the required fields. and go to terraform.

go to aws providers.
click on terraform aws documentation
click on AWS provider

on the right side, you see authentication
- static
you will see an example..


go to ec2 resources on left side
- go through and look for aws instance
- click on it, you will see  
  Resource: aws_instance
it says provides and go through the examples...

you are look for arguements..
- look for AMI
  - it says ami is required keyword
  - public_ip_address is optional .
  - look for fields required fields or optional ones.

- go down to tags
  - its an optional field as well

you can also click on example on right side to find more...

go to aws IAM and create a user. (give power use access power)
-
-----------------------------------------------

back to the basic on TF
> notepad terraform.tf

provider "aws" {

region = "ap-south-1"
access_key = "ABCHDDHHFHIE77lhHHGSLSLDH"
secret_key = "AJKLFJKLJDKJFKDJI3338kjjh"

}

resource "ec2 instance" {

image id ="ami-01938d8s003ds88ss"
instance type = "t2.micro"
tags Name = os4


}
 
-------------------------
now, correct the terms

> notepad terraform.tf
provider "aws" {

region = "ap-south-1"
access_key = ""
secret_key = ""

}

resource "ec2 instance" "os1" {    # os1 is resource name

ami ="ami-01938d8s003ds88ss"
instance_type = "t2.micro"
tags = {
  Name = "My first OS RHEL"
    }
}
 
Note: tag is a map (dictionary) and defined in key/value format.

terraform creates and also manages the respources.

state: currect/desire state

Now, run this code

> terraform init
> terrafrom apply    # executing the code

apply checks the code, plan code and executes
this will basically create an instance on AWS.

it will prompt, just type yes and press enter

go to aws console, you will see the instance running..



declarative language


let go ahead and make change to code

> notepad terraform.tf
provider "aws" {

region = "ap-south-1"
access_key = ""
secret_key = ""

}

resource "ec2 instance" "os1" {    # os1 is resource name

ami ="ami-01938d8s003ds88ss"
instance_type = "t2.micro"
tags = {
  Name = "My first OS New"
    }
}
> terraform apply
@prompt say: yes

it will change the name..

lets say we want to change t2.micro to other one?
yes, how?

go to instance -> action -> instance -? change instance state
it grayed out.

to change, you have to shutdown the instance, and change the instance type.


but for terraform, lets just change the name


> notepad terraform.tf
provider "aws" {

region = "ap-south-1"
access_key = ""
secret_key = ""

}

resource "ec2 instance" "os1" {    # os1 is resource name

ami ="ami-01938d8s003ds88ss"
instance_type = "t2.small"
tags = {
  Name = "My first OS New"
    }
}
> terraform apply




No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...