github.com/adamar/terraform@v0.2.2-0.20141016210445-2e703afdad0e/website/source/intro/getting-started/build.html.md (about) 1 --- 2 layout: "intro" 3 page_title: "Build Infrastructure" 4 sidebar_current: "gettingstarted-build" 5 --- 6 7 # Build Infrastructure 8 9 With Terraform installed, let's dive right into it and start creating 10 some infrastructure. 11 12 We'll build infrastructure on 13 [AWS](http://aws.amazon.com) for the getting started guide 14 since it is popular and generally understood, but Terraform 15 can [manage many providers](/docs/providers/index.html), 16 including multiple providers in a single configuration. 17 Some examples of this are in the 18 [use cases section](/intro/use-cases.html). 19 20 If you don't have an AWS account, 21 [create one now](http://aws.amazon.com/free/). 22 For the getting started guide, we'll only be using resources 23 which qualify under the AWS 24 [free-tier](http://aws.amazon.com/free/), 25 meaning it will be free. 26 If you already have an AWS account, you may be charged some 27 amount of money, but it shouldn't be more than a few dollars 28 at most. 29 30 <div class="alert alert-block alert-warning"> 31 <p> 32 <strong>Note:</strong> If you're not using an account that qualifies 33 under the AWS 34 <a href="http://aws.amazon.com/free/">free-tier</a>, 35 you may be charged to run these examples. The most you should 36 be charged should only be a few dollars, but we're not responsible 37 for any charges that may incur. 38 </p> 39 </div> 40 41 ## Configuration 42 43 The set of files used to describe infrastructure in Terraform is simply 44 known as a Terraform _configuration_. We're going to write our first 45 configuration now to launch a single AWS EC2 instance. 46 47 The format of the configuration files is 48 [documented here](/docs/configuration/index.html). 49 Configuration files can 50 [also be JSON](/docs/configuration/syntax.html), but we recommend only using JSON when the 51 configuration is generated by a machine. 52 53 The entire configuration is shown below. We'll go over each part 54 after. Save the contents to a file named `example.tf`. Verify that 55 there are no other `*.tf` files in your directory, since Terraform 56 loads all of them. 57 58 ``` 59 provider "aws" { 60 access_key = "ACCESS_KEY_HERE" 61 secret_key = "SECRET_KEY_HERE" 62 region = "us-east-1" 63 } 64 65 resource "aws_instance" "example" { 66 ami = "ami-408c7f28" 67 instance_type = "t1.micro" 68 } 69 ``` 70 71 Replace the `ACCESS_KEY_HERE` and `SECRET_KEY_HERE` with your 72 AWS access key and secret key, available from 73 [this page](https://console.aws.amazon.com/iam/home?#security_credential). 74 We're hardcoding them for now, but will extract these into 75 variables later in the getting started guide. 76 77 This is a complete configuration that Terraform is ready to apply. 78 The general structure should be intuitive and straightforward. 79 80 The `provider` block is used to configure the named provider, in 81 our case "aws." A provider is responsible for creating and 82 managing resources. Multiple provider blocks can exist if a 83 Terraform configuration is comprised of multiple providers, 84 which is a common situation. 85 86 The `resource` block defines a resource that exists within 87 the infrastructure. A resource might be a physical component such 88 as an EC2 instance, or it can be a logical resource such as 89 a Heroku application. 90 91 The resource block has two strings before opening the block: 92 the resource type and the resource name. In our example, the 93 resource type is "aws\_instance" and the name is "example." 94 The prefix of the type maps to the provider. In our case 95 "aws\_instance" automatically tells Terraform that it is 96 managed by the "aws" provider. 97 98 Within the resource block itself is configuration for that 99 resource. This is dependent on each resource provider and 100 is fully documented within our 101 [providers reference](/docs/providers/index.html). For our EC2 instance, we specify 102 an AMI for Ubuntu, and request a "t1.micro" instance so we 103 qualify under the free tier. 104 105 ## Execution Plan 106 107 Next, let's see what Terraform would do if we asked it to 108 apply this configuration. In the same directory as the 109 `example.tf` file you created, run `terraform plan`. You 110 should see output similar to what is copied below. We've 111 truncated some of the output to save space. 112 113 ``` 114 $ terraform plan 115 ... 116 117 + aws_instance.example 118 ami: "" => "ami-408c7f28" 119 availability_zone: "" => "<computed>" 120 instance_type: "" => "t1.micro" 121 key_name: "" => "<computed>" 122 private_dns: "" => "<computed>" 123 private_ip: "" => "<computed>" 124 public_dns: "" => "<computed>" 125 public_ip: "" => "<computed>" 126 security_groups: "" => "<computed>" 127 subnet_id: "" => "<computed>" 128 ``` 129 130 `terraform plan` shows what changes Terraform will apply to 131 your infrastructure given the current state of your infrastructure 132 as well as the current contents of your configuration. 133 134 If `terraform plan` failed with an error, read the error message 135 and fix the error that occurred. At this stage, it is probably a 136 syntax error in the configuration. 137 138 The output format is similar to the diff format generated by tools 139 such as Git. The output has a "+" next to "aws\_instance.example", 140 meaning that Terraform will create this resource. Beneath that, 141 it shows the attributes that will be set. When the value it is 142 going to is `<computed>`, it means that the value won't be known 143 until the resource is created. 144 145 ## Apply 146 147 The plan looks good, our configuration appears valid, so it's time to 148 create real resources. Run `terraform apply` in the same directory 149 as your `example.tf`, and watch it go! It will take a few minutes 150 since Terraform waits for the EC2 instance to become available. 151 152 ``` 153 $ terraform apply 154 aws_instance.example: Creating... 155 ami: "" => "ami-408c7f28" 156 instance_type: "" => "t1.micro" 157 158 Apply complete! Resources: 1 added, 0 changed, 0 destroyed. 159 160 ... 161 ``` 162 163 Done! You can go to the AWS console to prove to yourself that the 164 EC2 instance has been created. 165 166 Terraform also put some state into the `terraform.tfstate` file 167 by default. This state file is extremely important; it maps various 168 resource metadata to actual resource IDs so that Terraform knows 169 what it is managing. This file must be saved and distributed 170 to anyone who might run Terraform. We recommend simply putting it 171 into version control, since it generally isn't too large. 172 173 You can inspect the state using `terraform show`: 174 175 ``` 176 $ terraform show 177 aws_instance.example: 178 id = i-e60900cd 179 ami = ami-408c7f28 180 availability_zone = us-east-1c 181 instance_type = t1.micro 182 key_name = 183 private_dns = domU-12-31-39-12-38-AB.compute-1.internal 184 private_ip = 10.200.59.89 185 public_dns = ec2-54-81-21-192.compute-1.amazonaws.com 186 public_ip = 54.81.21.192 187 security_groups.# = 1 188 security_groups.0 = default 189 subnet_id = 190 ``` 191 192 You can see that by creating our resource, we've also gathered 193 a lot more metadata about it. This metadata can actually be referenced 194 for other resources or outputs, which will be covered later in 195 the getting started guide. 196 197 ## Provisioning 198 199 The EC2 instance we launched at this point is based on the AMI 200 given, but has no additional software installed. If you're running 201 an image-based infrastructure (perhaps creating images with 202 [Packer](http://www.packer.io)), then this is all you need. 203 204 However, many infrastructures still require some sort of initialization 205 or software provisioning step. Terraform supports 206 provisioners, 207 which we'll cover a little bit later in the getting started guide, 208 in order to do this. 209 210 ## Next 211 212 Congratulations! You've built your first infrastructure with Terraform. 213 You've seen the configuration syntax, an example of a basic execution 214 plan, and understand the state file. 215 216 Next, we're going to move on to [changing and destroying infrastructure](/intro/getting-started/change.html).