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