-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTerraform Basics.txt
More file actions
85 lines (63 loc) · 2.14 KB
/
Terraform Basics.txt
File metadata and controls
85 lines (63 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Terraform Basics
-----------------
- Allows me to write infrastructure-as-code
- Automation of my infrastructure
- Keep infrastructure in a certain state (e.g no less than 3 instances open)
- Make my infrastructure auditable
- Keep in versional control
- Ansible, Chef, Puppet focus on automating and config of "software"
- Terrafom focuses on "infrastructure"
- Works well with automation software like ansible to install software "after" the infrastructure
is provisioned
- Works with multiple cloud providers
$ terraform version
$ terraform init // run in new directory with your terraform file before you run "apply"
$ terraform validate // validates terraform files
$ terraform apply
$ terraform destroy // destroys the apply that you ran
$ terraform plan // show you the changes that your terraform file would make if applied
$ terraform plan -out tfplan // safe way to print the changes that are going to be made
$ terraform apply tfplan // changes that are going to be executed
$ terraform console // starts interactive console to evaluate expressions based on created objects
Make a directory for your terraform files
mkdir -p /terraform
cd /terraform
Create a terraform script
vim main.tf
provider "aws" {
access_key = "xxxxxx"
secret_key = "xxxxxx"
region = "us-east-1"
}
// "resource_type" "resource_name"
resource "aws_instance" "example" {
ami = "ami-011b3ccf1bd6db744"
instance_type = "t2.micro"
subnet_id = "subnet-ad7da4e7"
vpc_security_group_ids = ["sg-0ac5e15ddfe7cefe0"]
key_name = "macKey"
tags = {
Name = "Terraform 1"
}
}
output "ip"{
value = "${aws_instance.example.public_ip}"
}
Initialize Terraform
terraform init
Validate files
terraform validate
List providers in the folder
ls .terraform/plugins/linux_amd64
List providers in the folder
terraform providers
Generate execution plan and send to outfile
terraform plan -out=tfplan
Execute the plan
terrafrom apply tfplan
Show state
terraform show
Start Terraform Console and Get Resource Property Values
terraform console
> aws_instance.example.ami
> aws_instance.example.instance_type