0% found this document useful (0 votes)
2 views

Terraform

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Terraform

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1. What are the most useful Terraform commands?

Some of the most useful Terraform commands are:

terraform init - initializes the current directory


terraform refresh - refreshes the state file
terraform output - views Terraform outputs
terraform apply - applies the Terraform code and builds stuff
terraform destroy - destroys what has been built by Terraform
terraform graph - creates a DOT-formatted graph
terraform plan - a dry run to see what Terraform will do

++++++++++++++++++++++++++++++++++++++++++++
# To Use User Data in Terraform Script

userdata.sh --> File Name

to write in main.tf file.

user_data = base64encode(file("userdata.sh"))

++++++++++++++++++++++++++++++++++++++++
Modules:

provider "aws" {
region = "us-east-1"
}

module "ec2_instance" {
source = "./modules/ec2_instance"
ami_value = "ami-053b0d53c279acc90" # replace this
instance_type_value = "t2.micro"
subnet_id_value = "subnet-019ea91ed9b5252e7". # replace this
}

+++++++++++++++++++++++++++++++++++
https://www.youtube.com/watch?v=-4IMy5ihiiU&list=PLdpzxOOAlwvI0O4PeKVV1-
yJoX2AqIWuf&index=9&ab_channel=Abhishek.Veeramalla

Terraform Import

1. export the configuration --> Create the state file of the instance->

1 provider "aws" {
2 region = "us-east-1"
3 }
4
5 import {
6 id="i-0573763ef5312afd6"
7
8 to= aws_instance.example
9 }

> Terraform plan -generate-config-out=generated_resources.tf

2. Paste the output, by removing import command and use the output of the
generated_resources.tf to make a terraform main file. This will create the state
file.

> Terraform import aws_instance.example i-0573763ef5312afd6


7. What is Terraform D?
Terraform D is a plugin used on most in-service systems and Windows.
Terraform init by default searches next directories for plugins.

10. Define null resource in Terraform.


null_resource implements standard resource library, but no further action is taken.
The triggers argument allows an arbitrary set of values that will cause the
replacement of resources when changed.

36. Give the terraform configuration for creating a single EC2 instance on AWS.
This is the Terraform configuration for creating a single EC2 instance on AWS:

provider “aws” {
region = “”}
resource “aws_instance”
“example” {
ami = ""
instance_type = ""
tags {
Name = "example"}

37. How will you upgrade plugins on Terraform?


Run ‘terraform init’ with ‘-upgrade’ option.
This command rechecks the releases.hashicorp.com to find new acceptable provider
versions.
It also downloads available provider versions. “.terraform/plugins/<OS>_<ARCH>” is
the automatic downloads directory.

24. Which command destroys Terraform managed infrastructure?


The given command is used for this purpose:

terraform destroy [options] [dir]

Configuration Loader :

Model types in package configs represent the top-level configuration structure.


configs.Config represents an entire configuration
(the root module and all of its child modules). Although the configs package offers
some low-level functionality for creating configuration objects,
the major entry point is via configload.Loader, which is found in the sub-package
configload. When a configuration is loaded by a backend,
a loader takes care of all the complexities of installing child modules (during
terraform init) and then locating those modules again.
It takes the path to a root module and loads all of the child modules in a
recursive manner to create a single configs.

State Manager:

The state manager is in charge of storing and retrieving snapshots of a workspace's


Terraform state. E
ach manager is an implementation of a subset of the statemgr package's interfaces,
with most practical managers implementing the entire set of statemgr.
Full's operations . The smaller interfaces are mostly for use in other function
signatures to be specific about what actions the function might perform
on the state manager; there's no reason to design a state manager that doesn't
implement all of statemgr. Full.

How to configure profile:


these commands will run from AWS CLI:

1. --> aws configure --profile terraform-user


Here we have create a user terraform-user in AWS console and provided
with permission
AWS will ask for AWS secretkey and access key
2. check is profile created
--> aws s3 ls --profile terraform-user
This will list out all the output ( If you have permission for s3)

Now your first block will be like this:

provider "aws" {
region = "us-east-1"
profile = "terraform-user"
}

You might also like