Started with Terraform

Started with Terraform.Why do we need to use terraform?.


Started with Terraform.Before, if we did operation and maintenance, then we must know linux well, because you always have to log in and create various necessary resources (now although there is terraform, linux is also very important). Now with the continuous rise of cloud platforms (AWS, AliCloud , Azure), terraform came into being. What about terraform -> "Infrastructure as Code", we can use code to manage our resources, including creating Resources, modify resources, view resources, delete resources. It's like we hired a steward who helps us manage all the resources of our environment.
the three advantages given by the terraform official website are listed below:
1.Platform Agnostic (applies to multiple platforms)
2.Status management (there is a special file to record the status of the last resource we created, whether it was successful or not)
3.Increase developer information (in fact, it is to simplify the operation of developers and save time)
Terraform consists of several parts
•Started with Terraform.Providers
Started with Terraform.Terraform is just a construction tool. All cloud platforms will open corresponding interfaces, and we can call this interface "Providers". All providers can view the following
URL, https://www .terraform.io/docs/providers/index.html , because I use terraform myself, it is combined with AWS, so I also list the address of AWS Provider, https://www.terraform.io/docs/providers /aws/index.html , the code sample is as follows :
•provider " aws " {
•profile = "default"
•region = "us-east-1"
}
•Resources
A resource code block is equivalent to comparing a physical resource on a cloud platform. It defines a corresponding infrastructure, all required resources and related parameters, which can be found at https://www.terraform.io/docs/ The providers/aws/index.html URL can be viewed in the components on the left.
Example:
resource " aws_instance " "example" {
ami = "ami-2757f631"
instance_type = "t 2.micro "
}
•Provisions
uses an image-based resource, some operations need to be done when the resource is initially initialized. For example, we can install some necessary software at this time and so on.
example is as follows :
resource " aws_instance " "example" {
ami = "ami-b374d5a5"
instance_type = "t 2.micro "

provisioner "local-exec" {
command = "echo ${ aws_instance. example. public _ip } > ip_address.txt"
}
}
will output our ip address to a txt file, at the beginning of our instance creation.
In addition to using "local-exec", we can also use "remote-exec", for example, we can connect the current server to another server in the instance, for example :
•Modules
extracts the repeated resources from multiple resources and can be used as a module
code sample is as follows
module "consul" {
source = " hashicorp /consul/ aws "
num_servers = "3"
}
•Started with Terraform.Data Sources
allow us to compute data, so that they can be used elsewhere in the configuration, and this data is available outside the current terraform workfile. For example, we can get the data we need from the remote state.
Note: Data Sources are limited to what we use to read files. The
code sample is as follows :
# Find the latest available AMI that is tagged with Component = web
data " aws_ami " "web" {
filter {
name = "state"
values = [ "available" ]
}

filter {
name = " tag:Component "
values = [ "web" ]
}

most_recent = true
}
Each data source exposes one or more outputs, and then we can get the output data in the form of data... , for example:
resource " aws_instance " "web" {
ami = data.aws_ami.web.id
instance_type = "t 1.micro "
}
There can be many types of Data Sources, for example:
•aws_ami
•template_file
•terraform_remote_state

Started with Terraform.What are the commands that Terraform executes?


.tf under the working folder . There is no order of execution. If necessary, you can add the depend on attribute.
1.terraform init
command will first install the configuration and data required by the corresponding provider, which is equivalent to the preparation work before execution. All downloaded configurations will be saved in a hidden directory under the working directory.
2.The terraform plan
is drilled in advance, and it is not actually executed. It will list information about all resources that are about to be created and deleted.
3.terraform apply
actually executes the file. So how does terraform know the status of our last execution, for example, a resource was created before, and now another resource is created, how can terraform ensure that the first resource will not be created repeatedly? The reason is that he records the last state in a file ending with .tfstate , which can be stored locally or remotely, such as Aws' S3 and so on.
4.Terraform destroy
really deletes resources
5.terraform show
shows what's in our .tfstate end file.

Started with Terraform.Terraform input properties


Since it is in the field of software development, the input of variables will naturally be involved, so that our code can become more flexible and reusable.
We can create a new file called "variables.tf" (but in fact, we can define any name that ends with .tf , as I mentioned earlier, as long as it is in the working directory)
Definition example:

Started with Terraform.Terraform output properties

Since it is in the field of software development, the input of variables will naturally be involved, so that our code can become more flexible and reusable.
We can create a new file called "variables.tf" (but in fact, we can define any name that ends with .tf , as I mentioned earlier, as long as it is in the working directory)
Definition example:

Started with Terraform.Terraform output properties


IP address of the instance , then we can use the output code block, which can be used for indirect output using terraform apply, or directly output through terraform output
example is as follows :
output " ip " {
value = aws_eip.ip.public_ip _
}
If it comes to output, then we have to talk about how to get the attributes of the output. The code is as follows:
// define a data block
data " terraform_remote_state " " vpc " {
backend = "remote"

config = {
organization = " hashicorp "
workspaces = {
name = " vpc -prod"
}
}
}

# Terraform >= 0.12
resource " aws_instance " "foo" {
# Use the remote code block just defined, the last subnet_id should be the attribute name output in other resources
subnet_id = data.terraform_remote_state.vpc.outputs.subnet_id _
}

# Terraform <= 0.11
resource " aws_instance " "foo" {
# Use the remote code block just defined, the last subnet_id should be the attribute name output in other resources
subnet_id = "${data.terraform_remote_state.vpc.subnet_id}"
}