Commit cfa1d35f authored by A-Gordon's avatar A-Gordon

initial commit

parents
image: alpine:latest
variables:
TERRAFORM_URL: "https://releases.hashicorp.com/terraform/0.7.1/terraform_0.7.1_linux_amd64.zip"
before_script:
- apk update && apk add ca-certificates && update-ca-certificates && apk add openssl
- wget -O /tmp/terraform.zip $TERRAFORM_URL
- unzip /tmp/terraform.zip -d /usr/local/bin
test:
script: terraform validate
tf_mod_aws_asg
==============
A Terraform module for creating an auto scaling group and it's dependencies.
The module generates the launch configuration, the auto scaling group and two autoscaling policies.
1.) Adding a module resource to your template, e.g. `main.tf`
```
module "asg" {
source = "../../../../steamhaus/tf_mod_aws_ec2"
lc_name = "${var.name}-app-lc-"
ami_id = "${data.aws_ami.app.id}"
iam_instance_profile = "${module.cloudwatch_logs.profile_id}"
key_name = "Bastion"
security_groups = ["${aws_security_group.app.id}"]
user_data = "${data.template_file.userdata_app.rendered}"
asg_name = "${var.name}-app"
load_balancers = ["${module.elb.elb_id}"]
vpc_zone_identifier = ["${module.vpc.private_subnets}"]
}
```
\ No newline at end of file
#############################################################################################################
# Variables
#############################################################################################################
# Launch Configuration Variables
variable "lc_name" {}
variable "ami_id" {
description = "The AMI to use with the launch configuration"
}
variable "instance_type" {
default = "m3.medium"
}
variable "iam_instance_profile" {
description = "The IAM role the launched instance will use"
}
variable "key_name" {
description = "The SSH public key name (in EC2 key-pairs) to be injected into instances"
default = "Bastion"
}
variable "security_groups" {
description = "ID of SG the launched instance will use"
type = "list"
}
variable "user_data" {
description = "The path to a file with user_data for the instances"
}
# Auto-Scaling Group
variable "asg_name" {}
variable "asg_minimum_number_of_instances" {
description = "The minimum number of instances we want in the ASG"
default = "2"
}
variable "asg_maximum_number_of_instances" {
description = "The maximum number of instances we want in the ASG"
default = "2"
}
variable "asg_desired_number_of_instances" {
description = "The desired number of instances we want in the ASG"
default = "2"
}
variable "health_check_grace_period" {
description = "Number of seconds for a health check to time out"
default = 300
}
variable "health_check_type" {
description = "The health check used by the ASG to determine health"
default = "ELB"
}
variable "load_balancers" {
description = "A list of ELB names the ASG should associate instances with"
type = "list"
}
variable "vpc_zone_identifier" {
type = "list"
}
# Auto-Scaling policies
variable "scale_up_scaling_adjsutment" {
default = "100"
}
variable "scale_up_cooldown" {
default = "60"
}
variable "scale_down_scaling_adjsutment" {
default = "-50"
}
variable "scale_down_cooldown" {
default = "120"
}
resource "aws_launch_configuration" "default" {
lifecycle { create_before_destroy = true }
name_prefix = "${var.lc_name}"
image_id = "${var.ami_id}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
iam_instance_profile = "${var.iam_instance_profile}"
security_groups = ["${var.security_groups}"]
user_data = "${var.user_data}"
}
resource "aws_autoscaling_group" "default" {
lifecycle { create_before_destroy = true }
name = "${var.asg_name}"
launch_configuration = "${aws_launch_configuration.default.name}"
load_balancers = ["${var.load_balancers}"]
vpc_zone_identifier = ["${var.vpc_zone_identifier}"]
min_elb_capacity = "${var.asg_minimum_number_of_instances}"
min_size = "${var.asg_minimum_number_of_instances}"
max_size = "${var.asg_maximum_number_of_instances}"
desired_capacity = "${var.asg_desired_number_of_instances}"
health_check_grace_period = "${var.health_check_grace_period}"
health_check_type = "${var.health_check_type}"
tag {
key = "Name"
value = "${var.asg_name}"
propagate_at_launch = true
}
tag {
key = "Launch config"
value = "${aws_launch_configuration.default.name}"
propagate_at_launch = true
}
}
resource "aws_autoscaling_policy" "scale-up" {
name = "${var.asg_name}-scale-up"
adjustment_type = "PercentChangeInCapacity"
scaling_adjustment = "${var.scale_up_scaling_adjsutment}"
cooldown = "${var.scale_up_cooldown}"
autoscaling_group_name = "${aws_autoscaling_group.default.name}"
}
resource "aws_autoscaling_policy" "scale-down" {
name = "${var.asg_name}-scale-down"
adjustment_type = "PercentChangeInCapacity"
scaling_adjustment = "${var.scale_down_scaling_adjsutment}"
cooldown = "${var.scale_down_cooldown}"
autoscaling_group_name = "${aws_autoscaling_group.default.name}"
}
#############################################################################################################
# Outputs
#############################################################################################################
output "launch_config_id" {
value = "${aws_launch_configuration.default.id}"
}
output "asg_id" {
value = "${aws_autoscaling_group.default.id}"
}
output "asg_name" {
value = "${aws_autoscaling_group.default.name}"
}
output "autoscaling_policy_scale_up_arn" {
value = "${aws_autoscaling_policy.scale-up.arn}"
}
output "autoscaling_policy_scale_down_arn" {
value = "${aws_autoscaling_policy.scale-down.arn}"
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment