github.com/jmbataller/terraform@v0.6.8-0.20151125192640-b7a12e3a580c/website/source/intro/getting-started/provision.html.md (about) 1 --- 2 layout: "intro" 3 page_title: "Provision" 4 sidebar_current: "gettingstarted-provision" 5 description: |- 6 You're now able to create and modify infrastructure. This page introduces how to use provisioners to run basic shell scripts on instances when they're created. 7 --- 8 9 # Provision 10 11 You're now able to create and modify infrastructure. This page 12 introduces how to use provisioners to run basic shell scripts on 13 instances when they're created. 14 15 If you're using an image-based infrastructure (perhaps with images 16 created with [Packer](https://www.packer.io)), then what you've 17 learned so far is good enough. But if you need to do some initial 18 setup on your instances, provisioners let you upload files, 19 run shell scripts, etc. 20 21 ## Defining a Provisioner 22 23 To define a provisioner, modify the resource block defining the 24 "example" EC2 instance to look like the following: 25 26 ``` 27 resource "aws_instance" "example" { 28 ami = "ami-aa7ab6c2" 29 instance_type = "t1.micro" 30 31 provisioner "local-exec" { 32 command = "echo ${aws_instance.example.public_ip} > file.txt" 33 } 34 } 35 ``` 36 37 This adds a `provision` block within the `resource` block. Multiple 38 `provision` blocks can be added to define multiple provisioning steps. 39 Terraform supports 40 [multiple provisioners](/docs/provisioners/index.html), 41 but for this example we use the "local-exec" provisioner. 42 43 The "local-exec" provisioner executes a command locally on the machine 44 running Terraform. We're using this provisioner versus the others so 45 we don't have to worry about specifying any 46 [connection info](/docs/provisioners/connection.html) right now. 47 48 ## Running Provisioners 49 50 Provisioners are run only when a resource is _created_. They 51 are not a replacement for configuration management and changing 52 the software of an already-running server, and are instead just 53 meant as a way to bootstrap a server. For configuration management, 54 you should use Terraform provisioning to invoke a real configuration 55 management solution. 56 57 Make sure that your infrastructure is 58 [destroyed](/intro/getting-started/destroy.html) if it isn't already, 59 then run `apply`: 60 61 ``` 62 $ terraform apply 63 aws_instance.example: Creating... 64 ami: "" => "ami-aa7ab6c2" 65 instance_type: "" => "t1.micro" 66 aws_eip.ip: Creating... 67 instance: "" => "i-213f350a" 68 69 Apply complete! Resources: 2 added, 0 changed, 0 destroyed. 70 ``` 71 72 Terraform will output anything from provisioners to the console, 73 but in this case there is no output. However, we can verify 74 everything worked by looking at the "file.txt" file: 75 76 ``` 77 $ cat file.txt 78 54.192.26.128 79 ``` 80 81 It contains the IP, just as we asked! 82 83 ## Failed Provisioners and Tainted Resources 84 85 If a resource successfully creates but fails during provision, 86 Terraform will error and mark the resource as "tainted." A 87 resource that is tainted has been physically created, but can't 88 be considered safe to use since provisioning failed. 89 90 When you generate your next execution plan, Terraform will remove 91 any tainted resources and create new resources, attempting to 92 provision again. It does not attempt to restart provisioning on the 93 same resource because it isn't guaranteed to be safe. 94 95 Terraform does not automatically roll back and destroy the resource 96 during the apply when the failure happens, because that would go 97 against the execution plan: the execution plan would've said a 98 resource will be created, but does not say it will ever be deleted. 99 But if you create an execution plan with a tainted resource, the 100 plan will clearly state that the resource will be destroyed because 101 it is tainted. 102 103 ## Next 104 105 Provisioning is important for being able to bootstrap instances. 106 As another reminder, it is not a replacement for configuration 107 management. It is meant to simply bootstrap machines. If you use 108 configuration management, you should use the provisioning as a way 109 to bootstrap the configuration management utility. 110 111 In the next section, we start looking at [variables as a way to 112 parameterize our configurations](/intro/getting-started/variables.html).