github.com/tkak/terraform@v0.5.4-0.20150712180941-7f738dc27225/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 61 62 resource "aws_elb" "web" { 63 name = "example-elb" 64 65 # The same availability zone as our instance 66 availability_zones = ["${aws_instance.web.availability_zone}"] 67 security_groups = ["${aws_security_group.elb.id}"] 68 listener { 69 instance_port = 80 70 instance_protocol = "http" 71 lb_port = 80 72 lb_protocol = "http" 73 } 74 75 health_check { 76 healthy_threshold = 2 77 unhealthy_threshold = 2 78 timeout = 3 79 target = "HTTP:80/" 80 interval = 30 81 } 82 83 # The instance is registered automatically 84 instances = ["${aws_instance.web.id}"] 85 86 cross_zone_load_balancing = true 87 idle_timeout = 400 88 connection_draining = true 89 connection_draining_timeout = 400 90 91 } 92 93 resource "aws_lb_cookie_stickiness_policy" "default" { 94 name = "lbpolicy" 95 load_balancer = "${aws_elb.web.id}" 96 lb_port = 80 97 cookie_expiration_period = 600 98 } 99 100 resource "aws_instance" "web" { 101 102 instance_type = "t2.micro" 103 104 # Lookup the correct AMI based on the region 105 # we specified 106 ami = "${lookup(var.aws_amis, var.aws_region)}" 107 108 # The name of our SSH keypair you've created and downloaded 109 # from the AWS console. 110 # 111 # https://console.aws.amazon.com/ec2/v2/home?region=us-west-2#KeyPairs: 112 # 113 key_name = "${var.key_name}" 114 115 # Our Security group to allow HTTP and SSH access 116 security_groups = ["${aws_security_group.default.name}"] 117 118 user_data = "${file("userdata.sh")}" 119 #Instance tags 120 tags { 121 Name = "elb-example" 122 } 123 }