Merge pull request #2382 from sathiyas/examples-add-aws-asg
Examples add aws asg
This commit is contained in:
commit
3181a045a7
|
@ -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}}'
|
||||||
|
|
|
@ -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"]
|
||||||
|
}
|
||||||
|
}
|
|
@ -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}"
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
#!/bin/bash -v
|
||||||
|
apt-get update -y
|
||||||
|
apt-get install -y nginx > /tmp/nginx.log
|
|
@ -0,0 +1,42 @@
|
||||||
|
variable "aws_region" {
|
||||||
|
description = "The AWS region to create things in."
|
||||||
|
default = "us-east-1"
|
||||||
|
}
|
||||||
|
|
||||||
|
# ubuntu-trusty-14.04 (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"
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue