github.com/emc-cmd/terraform@v0.7.8-0.20161101145618-f16309630e7c/examples/aws-elb/main.tf (about)

     1  # Specify the provider and access details
     2  provider "aws" {
     3    region = "${var.aws_region}"
     4  }
     5  
     6  # Our default security group to access
     7  # the instances over SSH and HTTP
     8  resource "aws_security_group" "default" {
     9    name        = "instance_sg"
    10    description = "Used in the terraform"
    11  
    12    # SSH access from anywhere
    13    ingress {
    14      from_port   = 22
    15      to_port     = 22
    16      protocol    = "tcp"
    17      cidr_blocks = ["0.0.0.0/0"]
    18    }
    19  
    20    # HTTP access from anywhere
    21    ingress {
    22      from_port   = 80
    23      to_port     = 80
    24      protocol    = "tcp"
    25      cidr_blocks = ["0.0.0.0/0"]
    26    }
    27  
    28    # outbound internet access
    29    egress {
    30      from_port   = 0
    31      to_port     = 0
    32      protocol    = "-1"
    33      cidr_blocks = ["0.0.0.0/0"]
    34    }
    35  }
    36  
    37  # Our elb security group to access
    38  # the ELB over HTTP
    39  resource "aws_security_group" "elb" {
    40    name        = "elb_sg"
    41    description = "Used in the terraform"
    42  
    43    # HTTP access from anywhere
    44    ingress {
    45      from_port   = 80
    46      to_port     = 80
    47      protocol    = "tcp"
    48      cidr_blocks = ["0.0.0.0/0"]
    49    }
    50  
    51    # outbound internet access
    52    egress {
    53      from_port   = 0
    54      to_port     = 0
    55      protocol    = "-1"
    56      cidr_blocks = ["0.0.0.0/0"]
    57    }
    58  }
    59  
    60  resource "aws_elb" "web" {
    61    name = "example-elb"
    62  
    63    # The same availability zone as our instance
    64    availability_zones = ["${aws_instance.web.availability_zone}"]
    65    security_groups    = ["${aws_security_group.elb.id}"]
    66  
    67    listener {
    68      instance_port     = 80
    69      instance_protocol = "http"
    70      lb_port           = 80
    71      lb_protocol       = "http"
    72    }
    73  
    74    health_check {
    75      healthy_threshold   = 2
    76      unhealthy_threshold = 2
    77      timeout             = 3
    78      target              = "HTTP:80/"
    79      interval            = 30
    80    }
    81  
    82    # The instance is registered automatically
    83    instances = ["${aws_instance.web.id}"]
    84  
    85    cross_zone_load_balancing   = true
    86    idle_timeout                = 400
    87    connection_draining         = true
    88    connection_draining_timeout = 400
    89  }
    90  
    91  resource "aws_lb_cookie_stickiness_policy" "default" {
    92    name                     = "lbpolicy"
    93    load_balancer            = "${aws_elb.web.id}"
    94    lb_port                  = 80
    95    cookie_expiration_period = 600
    96  }
    97  
    98  resource "aws_instance" "web" {
    99    instance_type = "t2.micro"
   100  
   101    # Lookup the correct AMI based on the region
   102    # we specified
   103    ami = "${lookup(var.aws_amis, var.aws_region)}"
   104  
   105    # The name of our SSH keypair you've created and downloaded
   106    # from the AWS console.
   107    #
   108    # https://console.aws.amazon.com/ec2/v2/home?region=us-west-2#KeyPairs:
   109    #
   110    key_name = "${var.key_name}"
   111  
   112    # Our Security group to allow HTTP and SSH access
   113    security_groups = ["${aws_security_group.default.name}"]
   114  
   115    user_data = "${file("userdata.sh")}"
   116  
   117    #Instance tags
   118    tags {
   119      Name = "elb-example"
   120    }
   121  }