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

     1  ---
     2  layout: "intro"
     3  page_title: "Basic Two-Tier AWS Architecture"
     4  sidebar_current: "examples-aws"
     5  ---
     6  
     7  # Basic Two-Tier AWS Architecture
     8  
     9  This provides a template for running a simple two-tier architecture on Amazon
    10  Web services.
    11  
    12  The basic premise is you have stateless app servers running behind
    13  an ELB serving traffic.
    14  
    15  To simplify the example, this intentionally ignores deploying and
    16  getting your application onto the servers. However, you could do so either via
    17  [provisioners](/docs/provisioners/index.html) and a configuration
    18  management tool, or by pre-baking configured AMIs with
    19  [Packer](http://www.packer.io).
    20  
    21  After you run `terraform apply` on this configuration, it will
    22  automatically output the DNS address of the ELB. After your instance
    23  registers, this should respond with the default nginx web page.
    24  
    25  The configuration file contains comments describing each
    26  resource.
    27  
    28  ## Command
    29  
    30  ```
    31   terraform apply \
    32      -var 'aws_access_key=YOUR_ACCESS_KEY' \
    33      -var 'aws_secret_key=YOUR_SECRET_KEY' \
    34      -var 'key_path=/path/to/key/pair.pem' \
    35      -var 'key_name=keypair-name'
    36  ```
    37  
    38  ## Configuration
    39  
    40  ```
    41  variable "aws_access_key" {}
    42  variable "aws_secret_key" {}
    43  variable "key_path" {}
    44  variable "key_name" {}
    45  variable "aws_region" {
    46      default = "us-west-2"
    47  }
    48  
    49  # Ubuntu Precise 12.04 LTS (x64)
    50  variable "aws_amis" {
    51      default = {
    52          "eu-west-1": "ami-b1cf19c6",
    53          "us-east-1": "ami-de7ab6b6",
    54          "us-west-1": "ami-3f75767a",
    55          "us-west-2": "ami-21f78e11"
    56      }
    57  }
    58  
    59  # Specify the provider and access details
    60  provider "aws" {
    61      access_key = "${var.aws_access_key}"
    62      secret_key = "${var.aws_secret_key}"
    63      region = "${var.aws_region}"
    64  }
    65  
    66  # Our default security group to access
    67  # the instances over SSH and HTTP
    68  resource "aws_security_group" "default" {
    69      name = "terraform_example"
    70      description = "Used in the terraform"
    71  
    72      # SSH access from anywhere
    73      ingress {
    74          from_port = 22
    75          to_port = 22
    76          protocol = "tcp"
    77          cidr_blocks = ["0.0.0.0/0"]
    78      }
    79  
    80      # HTTP access from anywhere
    81      ingress {
    82          from_port = 80
    83          to_port = 80
    84          protocol = "tcp"
    85          cidr_blocks = ["0.0.0.0/0"]
    86      }
    87  }
    88  
    89  
    90  resource "aws_elb" "web" {
    91    name = "terraform-example-elb"
    92  
    93    # The same availability zone as our instance
    94    availability_zones = ["${aws_instance.web.availability_zone}"]
    95  
    96    listener {
    97      instance_port = 80
    98      instance_protocol = "http"
    99      lb_port = 80
   100      lb_protocol = "http"
   101    }
   102  
   103    # The instance is registered automatically
   104    instances = ["${aws_instance.web.id}"]
   105  }
   106  
   107  
   108  resource "aws_instance" "web" {
   109    # The connection block tells our provisioner how to
   110    # communicate with the resource (instance)
   111    connection {
   112      # The default username for our AMI
   113      user = "ubuntu"
   114  
   115      # The path to your keyfile
   116      key_file = "${var.key_path}"
   117    }
   118  
   119    instance_type = "m1.small"
   120  
   121    # Loookup the correct AMI based on the region
   122    # we specified
   123    ami = "${lookup(var.aws_amis, var.aws_region)}"
   124  
   125    # The name of our SSH keypair you've created and downloaded
   126    # from the AWS console.
   127    #
   128    # https://console.aws.amazon.com/ec2/v2/home?region=us-west-2#KeyPairs:
   129    #
   130    key_name = "${var.key_name}"
   131  
   132    # Our Security group to allow HTTP and SSH access
   133    security_groups = ["${aws_security_group.default.name}"]
   134  
   135    # We run a remote provisioner on the instance after creating it.
   136    # In this case, we just install nginx and start it. By default,
   137    # this should be on port 80
   138    provisioner "remote-exec" {
   139      inline = [
   140          "sudo apt-get -y update",
   141          "sudo apt-get -y install nginx",
   142          "sudo service nginx start",
   143      ]
   144    }
   145  }
   146  
   147  output "address" {
   148    value = "${aws_elb.web.dns_name}"
   149  }
   150  ```