Skip to content

Instantly share code, notes, and snippets.

@mtb-xt
Created April 24, 2017 03:10
Show Gist options
  • Save mtb-xt/527d34904cf6b704d9afc78b28289ff1 to your computer and use it in GitHub Desktop.
Save mtb-xt/527d34904cf6b704d9afc78b28289ff1 to your computer and use it in GitHub Desktop.
resource "aws_cloudformation_stack" "autoscaling_group" {
name = "${var.name}"
on_failure = "${var.asg_on_failure}"
# CloudFormation templates are more verbose and harder to read than Terraform templates, especially when crammed into
# a Terraform template. Since they are JSON, comments are not supported either. The CloudFormation template below
# creates just a single resource: an Auto Scaling Group. It configures a CreationPolicy and UpdatePolicy for it and
# spits out its computed name as an output.
template_body = <<EOF
{
"Resources": {
"${var.name}": {
"Type": "AWS::AutoScaling::AutoScalingGroup",
"Properties": {
"LaunchConfigurationName": "${var.launch_configuration_name}",
"MinSize": "${var.min_size}",
"MaxSize": "${var.max_size}",
"VPCZoneIdentifier": [${join(",", formatlist("\"%s\"", compact(var.vpc_subnet_ids)))}],
"HealthCheckGracePeriod": ${var.health_check_grace_period},
"TerminationPolicies": ["OldestLaunchConfiguration", "OldestInstance"],
"Tags": [{
"Key": "Name",
"Value": "${var.name}",
"PropagateAtLaunch": true
}]
},
"CreationPolicy": {
"ResourceSignal": {
"Timeout": "${var.rolling_deploy_pause_time}",
"Count": ${var.creation_signal_count}
}
},
"UpdatePolicy": {
"AutoScalingRollingUpdate": {
"MinInstancesInService": ${var.rolling_deploy_min_instances_in_service},
"MaxBatchSize": ${var.rolling_deploy_max_batch_size},
"PauseTime": "${var.rolling_deploy_pause_time}",
${module.if_wait_on_resource_signals.result}
}
}
}
},
"Outputs": {
"AsgName": {
"Description": "The name of the auto scaling group",
"Value": {"Ref": "${var.name}"}
}
}
}
EOF
}
resource "aws_launch_configuration" "launch_configuration" {
name_prefix = "${var.name}-"
image_id = "${var.ami}"
instance_type = "${var.instance_type}"
iam_instance_profile = "${aws_iam_instance_profile.instance_profile.name}"
key_name = "${var.keypair_name}"
security_groups = ["${aws_security_group.lc_security_group.id}"]
user_data = "${data.template_file.user_data.rendered}"
lifecycle {
create_before_destroy = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment