-
Notifications
You must be signed in to change notification settings - Fork 0
/
06._Provisioners.txt
77 lines (60 loc) · 2.21 KB
/
06._Provisioners.txt
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
Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.6
Creation-Date: 2023-10-26T09:17:32+02:00
====== 06. Provisioners ======
Provisioners are used to execute scripts on a local or remote machine as part of resource creation or destruction.
Scripts can be executed on resource creation (default) or destruction.
**Important: Use provisioners as a last resort. There are better alternatives for most situations.**
See: [[https://developer.hashicorp.com/terraform/language/resources/provisioners/syntax|Provisioners syntax]]
===== Remote exec =====
The ''remote-exec'' provisioner invokes a script on a remote resource after it is created.
The connection can use ''ssh'' or ''WinRM''.
'''
resource "aws_instance" "myec2" {
ami = "ami-0ca285d4c2cda3300"
instance_type = "t2.micro"
key_name = "terraform-key"
# default sg used here. Be sure that it allows port 80 and 22
connection {
type = "ssh"
user = "ec2-user"
private_key = file("./terraform-key.pem")
host = self.public_ip
}
provisioner "remote-exec" {
inline = [
# Updating with the latest command for Amazon Linux machine
"sudo yum install -y nginx",
"sudo systemctl start nginx"
]
}
}
'''
===== Local exec =====
The ''local-exec'' provisioner invokes a local executable after a resource is created.
'''
resource "aws_instance" "myec2" {
ami = "ami-082b5a644766e0e6f"
instance_type = "t2.micro"
provisioner "local-exec" {
command = "echo ${aws_instance.myec2.private_ip} >> ips.txt"
}
}
'''
→ With the ''local-exec'', the attribute name is ''command'', which is a ''string''.
===== On destroy =====
'''
# I agree, this example is a bit silly
provisioner "remote-exec" {
when = destroy
inline = [
"sudo yum -y remove nano"
]
}
'''
→ With the ''remote-exec'', the attribute name is ''inline'', which is a ''list(string)''.
===== Failure management =====
By default, provisioners that fail will also cause the terraform apply itself to fail.
The ''on_failure'' setting can be used to change this. The allowed values are:
* ''continue'' Ignore the error and continue with creation or destruction.
* ''fail'' Raise an error and stop applying (the default behavior)