github.com/ezbercih/terraform@v0.1.1-0.20140729011846-3c33865e0839/website/source/intro/examples/consul.html.markdown (about) 1 --- 2 layout: "intro" 3 page_title: "Consul Example" 4 sidebar_current: "examples-consul" 5 --- 6 7 # Consul Example 8 9 [Consul](http://www.consul.io) is a tool for service discovery, configuration 10 and orchestration. The Key/Value store it provides is often used to store 11 application configuration and information about the infrastructure necessary 12 to process requests. 13 14 Terraform provides a [Consul provider](/docs/providers/consul/index.html) which 15 can be used to interface with Consul from inside a Terraform configuration. 16 17 For our example, we use the [Consul demo cluster](http://demo.consul.io) 18 to both read configuration and store information about a newly created EC2 instance. 19 The size of the EC2 instance will be determind by the "tf\_test/size" key in Consul, 20 and will default to "m1.small" if that key does not exist. Once the instance is created 21 the "tf\_test/id" and "tf\_test/public\_dns" keys will be set with the computed 22 values for the instance. 23 24 Before we run the example, use the [Web UI](http://demo.consul.io/ui/#/nyc1/kv/) 25 to set the "tf\_test/size" key to "t1.micro". Once that is done, 26 copy the configuration into a configuration file ("consul.tf" works fine). 27 Either provide the AWS credentials as a default value in the configuration 28 or invoke `apply` with the appropriate variables set. 29 30 Once the `apply` has completed, we can see the keys in Consul by 31 visiting the [Web UI](http://demo.consul.io/ui/#/nyc1/kv/). We can see 32 that the "tf\_test/id" and "tf\_test/public\_dns" values have been 33 set. 34 35 We can now teardown the infrastructure following the 36 [instructions here](/intro/getting-started/destroy.html). Because 37 we set the 'delete' property of two of the Consul keys, Terraform 38 will cleanup those keys on destroy. We can verify this by using 39 the Web UI. 40 41 The point of this example is to show that Consul can be used with 42 Terraform both to enable dynamic inputs, but to also store outputs. 43 44 Inputs like AMI name, security groups, puppet roles, bootstrap scripts, 45 etc can all be loaded from Consul. This allows the specifics of an 46 infrastructure to be decoupled from its overall architecture. This enables 47 details to be changed without updating the Terraform configuration. 48 49 Outputs from Terraform can also be easily stored in Consul. One powerful 50 features this enables is using Consul for inventory management. If an 51 application relies on ELB for routing, Terraform can update the application's 52 configuration directly by setting the ELB address into Consul. Any resource 53 attribute can be stored in Consul, allowing an operator to capture anything 54 useful. 55 56 57 ## Command 58 59 ``` 60 terraform apply \ 61 -var 'aws_access_key=YOUR_KEY' \ 62 -var 'aws_secret_key=YOUR_KEY' 63 ``` 64 65 ## Configuration 66 67 ``` 68 # Declare our variables, require access and secret keys 69 variable "aws_access_key" {} 70 variable "aws_secret_key" {} 71 variable "aws_region" { 72 default = "us-east-1" 73 } 74 75 # AMI's from http://cloud-images.ubuntu.com/locator/ec2/ 76 variable "aws_amis" { 77 default = { 78 "eu-west-1": "ami-b1cf19c6", 79 "us-east-1": "ami-de7ab6b6", 80 "us-west-1": "ami-3f75767a", 81 "us-west-2": "ami-21f78e11", 82 } 83 } 84 85 # Setup the Consul provisioner to use the demo cluster 86 provider "consul" { 87 address = "demo.consul.io:80" 88 datacenter = "nyc1" 89 } 90 91 # Setup an AWS provider 92 provider "aws" { 93 access_key = "${var.aws_access_key}" 94 secret_key = "${var.aws_secret_key}" 95 region = "${var.aws_region}" 96 } 97 98 # Setup a key in Consul to provide inputs 99 resource "consul_keys" "input" { 100 key { 101 name = "size" 102 path = "tf_test/size" 103 default = "m1.small" 104 } 105 } 106 107 # Setup a new AWS instance using a dynamic ami and 108 # instance type 109 resource "aws_instance" "test" { 110 ami = "${lookup(var.aws_amis, var.aws_region)}" 111 instance_type = "${consul_keys.input.var.size}" 112 } 113 114 # Setup a key in Consul to store the instance id and 115 # the DNS name of the instance 116 resource "consul_keys" "test" { 117 key { 118 name = "id" 119 path = "tf_test/id" 120 value = "${aws_instance.test.id}" 121 delete = true 122 } 123 key { 124 name = "address" 125 path = "tf_test/public_dns" 126 value = "${aws_instance.test.public_dns}" 127 delete = true 128 } 129 } 130 ```