github.com/ezbercih/terraform@v0.1.1-0.20140729011846-3c33865e0839/website/source/intro/examples/count.markdown (about)

     1  ---
     2  layout: "intro"
     3  page_title: "Count"
     4  sidebar_current: "examples-count"
     5  ---
     6  
     7  # Count Example
     8  
     9  The count parameter on resources can simplify configurations
    10  and let you scale resources by simply incrementing a number.
    11  
    12  Additionally, variables can be used to expand a list of resources
    13  for use elsewhere.
    14  
    15  ## Command
    16  
    17  ```
    18   terraform apply \
    19      -var 'aws_access_key=YOUR_ACCESS_KEY' \
    20      -var 'aws_secret_key=YOUR_SECRET_KEY'
    21  ```
    22  
    23  ## Configuration
    24  
    25  ```
    26  variable "aws_access_key" {}
    27  variable "aws_secret_key" {}
    28  variable "aws_region" {
    29      default = "us-west-2"
    30  }
    31  
    32  # Ubuntu Precise 12.04 LTS (x64)
    33  variable "aws_amis" {
    34      default = {
    35          "eu-west-1": "ami-b1cf19c6",
    36          "us-east-1": "ami-de7ab6b6",
    37          "us-west-1": "ami-3f75767a",
    38          "us-west-2": "ami-21f78e11"
    39      }
    40  }
    41  
    42  # Specify the provider and access details
    43  provider "aws" {
    44      access_key = "${var.aws_access_key}"
    45      secret_key = "${var.aws_secret_key}"
    46      region = "${var.aws_region}"
    47  }
    48  
    49  resource "aws_elb" "web" {
    50    name = "terraform-example-elb"
    51  
    52    # The same availability zone as our instances
    53    availability_zones = ["${aws_instance.web.*.availability_zone}"]
    54  
    55    listener {
    56      instance_port = 80
    57      instance_protocol = "http"
    58      lb_port = 80
    59      lb_protocol = "http"
    60    }
    61  
    62    # The instances are registered automatically
    63    instances = ["${aws_instance.web.*.id}"]
    64  }
    65  
    66  
    67  resource "aws_instance" "web" {
    68    instance_type = "m1.small"
    69    ami = "${lookup(var.aws_amis, var.aws_region)}"
    70  
    71    # This will create 4 instances
    72    count = 4
    73  }
    74  
    75  output "address" {
    76    value = "Instances: ${aws_instance.web.*.id}"
    77  }
    78  ```