From 9b416032466683d3573f59ca68257e78e3c8ab01 Mon Sep 17 00:00:00 2001 From: Sathiya Shunmugasundaram Date: Sun, 7 Jun 2015 19:28:46 -0400 Subject: [PATCH 1/3] initial commit, aws-asg example --- examples/aws-asg/README.md | 28 ++++++++++++ examples/aws-asg/main.tf | 84 +++++++++++++++++++++++++++++++++++ examples/aws-asg/outputs.tf | 12 +++++ examples/aws-asg/userdata.sh | 3 ++ examples/aws-asg/variables.tf | 41 +++++++++++++++++ 5 files changed, 168 insertions(+) create mode 100644 examples/aws-asg/README.md create mode 100644 examples/aws-asg/main.tf create mode 100644 examples/aws-asg/outputs.tf create mode 100644 examples/aws-asg/userdata.sh create mode 100644 examples/aws-asg/variables.tf diff --git a/examples/aws-asg/README.md b/examples/aws-asg/README.md new file mode 100644 index 000000000..3dda05a87 --- /dev/null +++ b/examples/aws-asg/README.md @@ -0,0 +1,28 @@ +# ASG example + +This example shows how to launch instances using Auto Scaling Groups. + +This creates a security group, launch configuration, auto scaling group and an ELB. The user data for launch configuration installs nginx and it listnes on port 80. + +The example uses latest Ubuntu AMIs. + +Make sure you change the list of availability zones that is applicable to your account and region. + +To run, configure your AWS provider as described in https://www.terraform.io/docs/providers/aws/index.html + +Running the example + +For planning phase + +terraform plan -var 'key_name={your_key_name}}' + +For apply phase + +terraform apply -var 'key_name={your_key_name}}' + +Once the stack is created, wait for few minsutes and test the stack by launching a browser with ELB url. + +To remove the stack + +terraform apply -var 'key_name={your_key_name}}' + diff --git a/examples/aws-asg/main.tf b/examples/aws-asg/main.tf new file mode 100644 index 000000000..6ced0aa78 --- /dev/null +++ b/examples/aws-asg/main.tf @@ -0,0 +1,84 @@ +# Specify the provider and access details +provider "aws" { + region = "${var.aws_region}" +} + +resource "aws_elb" "web-elb" { + name = "terraform-example-elb" + + # The same availability zone as our instances + availability_zones = ["${split(",", var.availability_zones)}"] + 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 + } + +} + +resource "aws_autoscaling_group" "web-asg" { + availability_zones = ["${split(",", var.availability_zones)}"] + name = "terraform-example-asg" + max_size = "${var.asg_max}" + min_size = "${var.asg_min}" + desired_capacity = "${var.asg_desired}" + force_delete = true + launch_configuration = "${aws_launch_configuration.web-lc.name}" + load_balancers = ["${aws_elb.web-elb.name}"] + #vpc_zone_identifier = ["${split(",", var.availability_zones)}"] + tag { + key = "Name" + value = "web-asg" + propagate_at_launch = "true" + } + } + +resource "aws_launch_configuration" "web-lc" { + name = "terraform-example-lc" + image_id = "${lookup(var.aws_amis, var.aws_region)}" + instance_type = "${var.instance_type}" + # Security group + security_groups = ["${aws_security_group.default.name}"] + user_data = "${file("userdata.sh")}" + key_name = "${var.key_name}" +} + +# Our default security group to access +# the instances over SSH and HTTP +resource "aws_security_group" "default" { + name = "terraform_example_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"] + } +} diff --git a/examples/aws-asg/outputs.tf b/examples/aws-asg/outputs.tf new file mode 100644 index 000000000..afd78f134 --- /dev/null +++ b/examples/aws-asg/outputs.tf @@ -0,0 +1,12 @@ +output "security_group" { + value = "${aws_security_group.default.id}" +} +output "launch_configuration" { + value = "${aws_launch_configuration.web-lc.id}" +} +output "asg_name" { + value = "${aws_autoscaling_group.web-asg.id}" +} +output "elb_name" { + value = "${aws_elb.web-elb.dns_name}" +} \ No newline at end of file diff --git a/examples/aws-asg/userdata.sh b/examples/aws-asg/userdata.sh new file mode 100644 index 000000000..77e340b3a --- /dev/null +++ b/examples/aws-asg/userdata.sh @@ -0,0 +1,3 @@ +#!/bin/bash -v +apt-get update -y +apt-get install -y nginx > /tmp/nginx.log diff --git a/examples/aws-asg/variables.tf b/examples/aws-asg/variables.tf new file mode 100644 index 000000000..4231b79fa --- /dev/null +++ b/examples/aws-asg/variables.tf @@ -0,0 +1,41 @@ +variable "aws_region" { + description = "The AWS region to create things in." + default = "us-east-1" +} + +# Ubuntu Precise 12.04 LTS (x64) +variable "aws_amis" { + default = { + "us-east-1" = "ami-5f709f34" + "us-west-2" = "ami-7f675e4f" + } +} + +variable "availability_zones" { + default = "us-east-1b,us-east-1c,us-east-1d,us-east-1e" + description = "List of availability zones, use AWS CLI to find your " +} + +variable "key_name" { + description = "Name of AWS key pair" +} + +variable "instance_type" { + default = "t2.micro" + description = "AWS instance type" +} + +variable "asg_min" { + description = "Min numbers of servers in ASG" + default = "1" +} + +variable "asg_max" { + description = "Max numbers of servers in ASG" + default = "2" +} + +variable "asg_desired" { + description = "Desired numbers of servers in ASG" + default = "1" +} \ No newline at end of file From 4e1660def5f1418fb8f04bb20ecf901271fbab03 Mon Sep 17 00:00:00 2001 From: Sathiya Shunmugasundaram Date: Sun, 7 Jun 2015 19:31:17 -0400 Subject: [PATCH 2/3] added newline --- examples/aws-asg/variables.tf | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/aws-asg/variables.tf b/examples/aws-asg/variables.tf index 4231b79fa..e78e7b2bd 100644 --- a/examples/aws-asg/variables.tf +++ b/examples/aws-asg/variables.tf @@ -38,4 +38,5 @@ variable "asg_max" { variable "asg_desired" { description = "Desired numbers of servers in ASG" default = "1" -} \ No newline at end of file +} + From 052c71950881bfa42a560dc72dbe12b3bc38446b Mon Sep 17 00:00:00 2001 From: Sathiya Shunmugasundaram Date: Tue, 9 Jun 2015 13:44:22 -0400 Subject: [PATCH 3/3] Update AMI description --- examples/aws-asg/variables.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/aws-asg/variables.tf b/examples/aws-asg/variables.tf index e78e7b2bd..b51450911 100644 --- a/examples/aws-asg/variables.tf +++ b/examples/aws-asg/variables.tf @@ -3,7 +3,7 @@ variable "aws_region" { default = "us-east-1" } -# Ubuntu Precise 12.04 LTS (x64) +# ubuntu-trusty-14.04 (x64) variable "aws_amis" { default = { "us-east-1" = "ami-5f709f34"