From d1c1f6c3060989606bafd7142b822e402866c5cf Mon Sep 17 00:00:00 2001 From: Scott Lowe Date: Tue, 6 Jun 2017 06:06:11 -0600 Subject: [PATCH 1/3] First attempt at a "static" ASG-ELB config Add files for creating a "static" (not heavily parameterized) config with an Auto Scaling group and an Elastic Load Balancer. Functional, but doesn't work as expected. More testing needed. Signed-off-by: Scott Lowe --- terraform/aws/asg-elb/data.tf | 16 ++++++++++ terraform/aws/asg-elb/main.tf | 47 ++++++++++++++++++++++++++++++ terraform/aws/asg-elb/provider.tf | 3 ++ terraform/aws/asg-elb/variables.tf | 19 ++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 terraform/aws/asg-elb/data.tf create mode 100644 terraform/aws/asg-elb/main.tf create mode 100644 terraform/aws/asg-elb/provider.tf create mode 100644 terraform/aws/asg-elb/variables.tf diff --git a/terraform/aws/asg-elb/data.tf b/terraform/aws/asg-elb/data.tf new file mode 100644 index 0000000..57bf5e5 --- /dev/null +++ b/terraform/aws/asg-elb/data.tf @@ -0,0 +1,16 @@ +data "aws_ami" "atomic_ami" { + most_recent = true + owners = ["410186602215"] + filter { + name = "name" + values = ["CentOS Atomic Host 7*"] + } + filter { + name = "virtualization-type" + values = ["hvm"] + } +} + +data "aws_vpc" "default_vpc" { + default = true +} diff --git a/terraform/aws/asg-elb/main.tf b/terraform/aws/asg-elb/main.tf new file mode 100644 index 0000000..0c650b2 --- /dev/null +++ b/terraform/aws/asg-elb/main.tf @@ -0,0 +1,47 @@ +# Create an ELB +resource "aws_elb" "elb" { + name = "elb-test" + subnets = ["subnet-69e39630","subnet-e0702d97","subnet-c4e9d6a1"] + cross_zone_load_balancing = true + connection_draining = true + security_groups = ["${var.secgrp}"] + + listener { + instance_port = "22" + instance_protocol = "tcp" + lb_port = "22" + lb_protocol = "tcp" + } + + tags { + Name = "elb-test" + } +} + +# Create a launch configuration +resource "aws_launch_configuration" "lc" { + name = "lc-test" + image_id = "${data.aws_ami.atomic_ami.id}" + instance_type = "${var.flavor}" + security_groups = ["${var.secgrp}"] + key_name = "${var.keypair}" + + lifecycle { + create_before_destroy = true + } +} + +# Create an Auto Scaling group +resource "aws_autoscaling_group" "asg" { + name = "asg-test" + availability_zones = ["us-west-2a","us-west-2b","us-west-2c"] + min_size = "2" + max_size = "2" + desired_capacity = "2" + launch_configuration = "${aws_launch_configuration.lc.name}" + load_balancers = ["elb-test"] + + lifecycle { + create_before_destroy = true + } +} diff --git a/terraform/aws/asg-elb/provider.tf b/terraform/aws/asg-elb/provider.tf new file mode 100644 index 0000000..d7c130e --- /dev/null +++ b/terraform/aws/asg-elb/provider.tf @@ -0,0 +1,3 @@ +provider "aws" { + region = "${var.user_region}" +} diff --git a/terraform/aws/asg-elb/variables.tf b/terraform/aws/asg-elb/variables.tf new file mode 100644 index 0000000..5108f1c --- /dev/null +++ b/terraform/aws/asg-elb/variables.tf @@ -0,0 +1,19 @@ +variable "user_region" { + type = "string" + description = "AWS region in which all resources will be created" +} + +variable "keypair" { + type = "string" + description = "AWS SSH keypair to use to connect to instances" +} + +variable "flavor" { + type = "string" + description = "AWS type to use when creating instances" +} + +variable "secgrp" { + type = "string" + description = "ID of VPC security group to use" +} From 2aec01ac6405c105f6411f0e41febd6c7a22737c Mon Sep 17 00:00:00 2001 From: Scott Lowe Date: Thu, 7 Sep 2017 22:03:37 -0600 Subject: [PATCH 2/3] Udpate Terraform configuration Update the Terraform configuration to properly register the ASG instances with the ELB Signed-off-by: Scott Lowe --- terraform/aws/asg-elb/main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terraform/aws/asg-elb/main.tf b/terraform/aws/asg-elb/main.tf index 0c650b2..5b9f721 100644 --- a/terraform/aws/asg-elb/main.tf +++ b/terraform/aws/asg-elb/main.tf @@ -39,7 +39,7 @@ resource "aws_autoscaling_group" "asg" { max_size = "2" desired_capacity = "2" launch_configuration = "${aws_launch_configuration.lc.name}" - load_balancers = ["elb-test"] + load_balancers = ["${aws_elb.elb.name}"] lifecycle { create_before_destroy = true From f9f765d5525a1b368c6ad321b24d7151ff89745d Mon Sep 17 00:00:00 2001 From: Scott Lowe Date: Thu, 7 Sep 2017 23:08:10 -0600 Subject: [PATCH 3/3] Configure outputs and documentation Add output.tf to output the DNS name of the ELB. Add README.md to document how to use the environment. Signed-off-by: Scott Lowe --- terraform/aws/asg-elb/README.md | 37 +++++++++++++++++++++++++++++++++ terraform/aws/asg-elb/output.tf | 3 +++ 2 files changed, 40 insertions(+) create mode 100644 terraform/aws/asg-elb/README.md create mode 100644 terraform/aws/asg-elb/output.tf diff --git a/terraform/aws/asg-elb/README.md b/terraform/aws/asg-elb/README.md new file mode 100644 index 0000000..4def1f0 --- /dev/null +++ b/terraform/aws/asg-elb/README.md @@ -0,0 +1,37 @@ +# Auto Scaling Group with Elastic Load Balancer + +These files provide an example of a simple configuration with an Auto Scaling Group (ASG) and an Elastic Load Balancer (ELB). + +## Contents + +* **data.tf**: This Terraform configuration file provides information to Terraform on which AWS AMIs to use. + +* **main.tf**: This Terraform configuration file creates an Elastic Load Balancer (ELB), Launch Configuration (LC), and Auto Scaling Group (ASG). + +* **output.tf**: This Terraform configuration outputs information about the resources created (the ELB DNS name, specifically). + +* **provider.tf**: This Terraform configuration files configures the AWS provider. + +* **README.md**: The file you're currently reading. + +* **variables.tf**: This Terraform configuration file specifies variables that Terraform will need in order to create the desired AWS infrastructure. + +## Instructions + +These instructions assume that you have an AWS account, that Terraform is installed and working correctly, and that you've appropriately supplied the necessary AWS credentials to Terraform. + +1. Place the files from the `terraform/aws/asg-elb` directory of this GitHub repository into a directory on your local system. You can clone the entire "learning-tools" repository (using `git clone`) or just download the specific files from the `terraform/aws/asg-elb` folder. + +2. Create a file named `terraform.tfvars` and populate it with the name of the AWS keypair you'd like to use for the instances, the AWS region to use, the ID of the security group you'd like to use, and the flavor of the instances (such as "t2.micro"). Refer to [this URL](https://www.terraform.io/intro/getting-started/variables.html) for specific details on the syntax of this file. You can refer to the contents of `variables.tf` for the names of the variables that need to be defined. + +3. Run `terraform plan` to have Terraform examine the current infrastructure and determine what changes are necessary to realize the desired configuration. + +4. Run `terraform apply` to have Terraform make the changes necessary to realize the desired configuration. + +5. When Terraform is done running, it will output the DNS name of the Elastic Load Balancer (ELB). You can now use this DNS name to make load-balanced SSH connections to the back-end instances. + +Enjoy! + +## License + +This content is licensed under the MIT License. diff --git a/terraform/aws/asg-elb/output.tf b/terraform/aws/asg-elb/output.tf new file mode 100644 index 0000000..a44639f --- /dev/null +++ b/terraform/aws/asg-elb/output.tf @@ -0,0 +1,3 @@ +output "elb_dns_name" { + value = ["${aws_elb.elb.dns_name}"] +}