From e10d658b09bd2a7aa6bbc7979cd0fabd5ec97a76 Mon Sep 17 00:00:00 2001 From: Sathiya Shunmugasundaram Date: Tue, 9 Jun 2015 16:12:33 -0400 Subject: [PATCH 1/3] Initial commit aws sample for ELB --- examples/aws-elb/README.md | 11 +++ examples/aws-elb/main.tf | 123 ++++++++++++++++++++++++++++++++++ examples/aws-elb/outputs.tf | 3 + examples/aws-elb/userdata.sh | 3 + examples/aws-elb/variables.tf | 17 +++++ 5 files changed, 157 insertions(+) create mode 100644 examples/aws-elb/README.md create mode 100644 examples/aws-elb/main.tf create mode 100644 examples/aws-elb/outputs.tf create mode 100644 examples/aws-elb/userdata.sh create mode 100644 examples/aws-elb/variables.tf diff --git a/examples/aws-elb/README.md b/examples/aws-elb/README.md new file mode 100644 index 0000000000..680792f5d5 --- /dev/null +++ b/examples/aws-elb/README.md @@ -0,0 +1,11 @@ +# ELB with stickiness Example + +The example launches a web server, installs nginx, creates an ELB for isnatnce. It also creates security groups for elb/instance + +To run, configure your AWS provider as described in https://www.terraform.io/docs/providers/aws/index.html + +Running the example + +run `terraform apply -var 'key_name={your_key_name}}'` + +Give couple of mins for userdata to insatll nginx, and then type the ELB DNS Name from outputs in your browser and see tegh nginx welcome page \ No newline at end of file diff --git a/examples/aws-elb/main.tf b/examples/aws-elb/main.tf new file mode 100644 index 0000000000..6542a91eff --- /dev/null +++ b/examples/aws-elb/main.tf @@ -0,0 +1,123 @@ +# Specify the provider and access details +provider "aws" { + region = "${var.aws_region}" +} + +# Our default security group to access +# the instances over SSH and HTTP +resource "aws_security_group" "default" { + name = "instance_sg" + description = "Used in the terraform" + + # SSH access from anywhere + ingress { + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + # HTTP access from anywhere + ingress { + from_port = 80 + to_port = 80 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + # outbound internet access + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } +} + +# Our elb security group to access +# the ELB over HTTP +resource "aws_security_group" "elb" { + name = "elb_sg" + description = "Used in the terraform" + + # HTTP access from anywhere + ingress { + from_port = 80 + to_port = 80 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + # outbound internet access + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + +} + + +resource "aws_elb" "web" { + name = "example-elb" + + # The same availability zone as our instance + availability_zones = ["${aws_instance.web.availability_zone}"] + security_groups = ["${aws_security_group.elb.id}"] + listener { + instance_port = 80 + instance_protocol = "http" + lb_port = 80 + lb_protocol = "http" + } + + health_check { + healthy_threshold = 2 + unhealthy_threshold = 2 + timeout = 3 + target = "HTTP:80/" + interval = 30 + } + + # The instance is registered automatically + instances = ["${aws_instance.web.id}"] + + cross_zone_load_balancing = true + idle_timeout = 400 + connection_draining = true + connection_draining_timeout = 400 + +} + +resource "aws_lb_cookie_stickiness_policy" "default" { + name = "lbpolicy" + load_balancer = "${aws_elb.web.id}" + lb_port = 80 + cookie_expiration_period = 600 +} + +resource "aws_instance" "web" { + + instance_type = "t2.micro" + + # Lookup the correct AMI based on the region + # we specified + ami = "${lookup(var.aws_amis, var.aws_region)}" + + # The name of our SSH keypair you've created and downloaded + # from the AWS console. + # + # https://console.aws.amazon.com/ec2/v2/home?region=us-west-2#KeyPairs: + # + key_name = "${var.key_name}" + + # Our Security group to allow HTTP and SSH access + security_groups = ["${aws_security_group.default.name}"] + + user_data = "${file("userdata.sh")}" + #Instance tags + tags { + Name = "elb-example" + } +} diff --git a/examples/aws-elb/outputs.tf b/examples/aws-elb/outputs.tf new file mode 100644 index 0000000000..5977837f33 --- /dev/null +++ b/examples/aws-elb/outputs.tf @@ -0,0 +1,3 @@ +output "address" { + value = "${aws_elb.web.dns_name}" +} diff --git a/examples/aws-elb/userdata.sh b/examples/aws-elb/userdata.sh new file mode 100644 index 0000000000..1ec5a00277 --- /dev/null +++ b/examples/aws-elb/userdata.sh @@ -0,0 +1,3 @@ +#!/bin/bash -v +apt-get update -y +apt-get install -y nginx > /tmp/nginx.log \ No newline at end of file diff --git a/examples/aws-elb/variables.tf b/examples/aws-elb/variables.tf new file mode 100644 index 0000000000..150ffa36c8 --- /dev/null +++ b/examples/aws-elb/variables.tf @@ -0,0 +1,17 @@ +variable "key_name" { + description = "Name of the SSH keypair to use in AWS." +} + +variable "aws_region" { + description = "AWS region to launch servers." + default = "us-east-1" +} + +# ubuntu-trusty-14.04 (x64) +variable "aws_amis" { + default = { + "us-east-1" = "ami-5f709f34" + "us-west-2" = "ami-7f675e4f" + } +} + From 774756fb4c39d71c471c1bc2889c6d047fa4ae09 Mon Sep 17 00:00:00 2001 From: Sathiya Shunmugasundaram Date: Tue, 9 Jun 2015 16:19:17 -0400 Subject: [PATCH 2/3] Typo corretion --- examples/aws-elb/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/aws-elb/README.md b/examples/aws-elb/README.md index 680792f5d5..0f7db18cca 100644 --- a/examples/aws-elb/README.md +++ b/examples/aws-elb/README.md @@ -8,4 +8,4 @@ Running the example run `terraform apply -var 'key_name={your_key_name}}'` -Give couple of mins for userdata to insatll nginx, and then type the ELB DNS Name from outputs in your browser and see tegh nginx welcome page \ No newline at end of file +Give couple of mins for userdata to insatll nginx, and then type the ELB DNS Name from outputs in your browser and see the nginx welcome page From c1831832510e5f26c31aff70387f0267d43fab98 Mon Sep 17 00:00:00 2001 From: Sathiya Shunmugasundaram Date: Tue, 9 Jun 2015 16:22:02 -0400 Subject: [PATCH 3/3] New line --- examples/aws-elb/userdata.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/aws-elb/userdata.sh b/examples/aws-elb/userdata.sh index 1ec5a00277..8a058c7861 100644 --- a/examples/aws-elb/userdata.sh +++ b/examples/aws-elb/userdata.sh @@ -1,3 +1,4 @@ #!/bin/bash -v apt-get update -y -apt-get install -y nginx > /tmp/nginx.log \ No newline at end of file +apt-get install -y nginx > /tmp/nginx.log +