From 0eab70ac8be279d89807df9ed02df30c011b8d8d Mon Sep 17 00:00:00 2001 From: yeer Date: Fri, 1 Nov 2019 12:03:39 +0800 Subject: [PATCH 1/8] allow ignore changes for some fields like desired_capacity --- main.tf | 1 + variables.tf | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/main.tf b/main.tf index 5ed16c4..8fc578b 100644 --- a/main.tf +++ b/main.tf @@ -179,6 +179,7 @@ resource "aws_launch_template" "container_instance" { resource "aws_autoscaling_group" "container_instance" { lifecycle { create_before_destroy = true + ignore_changes = "${var.ignore_changes}" } name = "${coalesce(var.autoscaling_group_name, local.autoscaling_group_name)}" diff --git a/variables.tf b/variables.tf index aa8262d..f9585b5 100644 --- a/variables.tf +++ b/variables.tf @@ -102,3 +102,8 @@ variable "enabled_metrics" { variable "subnet_ids" { type = "list" } + +variable "ignore_changes" { + type = "list" + default = [] +} From 050321bae8429deecb3a9438112d9cd2ebe4e36f Mon Sep 17 00:00:00 2001 From: yeer Date: Fri, 1 Nov 2019 12:15:45 +0800 Subject: [PATCH 2/8] some documentation --- CHANGELOG.md | 1 + README.md | 1 + 2 files changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index afa59d8..5e326b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Add CI support via CircleCI. - Add repository metadata support via Probot Settings. - Use top level `owners` attributes for `aws_ami` (requires AWS provider 2.0.0+). +- Allow specified `ignore_changes` for autoacaling group. ## 2.0.0 diff --git a/README.md b/README.md index f194cae..1e38123 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,7 @@ We will reevaluate things when [Terraform 0.12](https://www.hashicorp.com/blog/t - `max_size` - Maximum number of EC2 instances in cluster (default: `1`) - `enabled_metrics` - A list of metrics to gather for the cluster - `subnet_ids` - A list of subnet IDs to launch cluster instances +- `ignore_changes` - A list of fields that want be be ignore changes for autoscaling group. for example, "desired_capacity" (default: `[]`) - `project` - Name of project this cluster is for (default: `Unknown`) - `environment` - Name of environment this cluster is targeting (default: `Unknown`) From e1ffba9f47030278cfe9e26678d36ad6cde44c58 Mon Sep 17 00:00:00 2001 From: yeer Date: Tue, 24 Dec 2019 13:45:45 +0800 Subject: [PATCH 3/8] Revert "allow ignore changes for some fields like desired_capacit" --- CHANGELOG.md | 1 - README.md | 1 - main.tf | 1 - variables.tf | 5 ----- 4 files changed, 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e326b1..afa59d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,6 @@ - Add CI support via CircleCI. - Add repository metadata support via Probot Settings. - Use top level `owners` attributes for `aws_ami` (requires AWS provider 2.0.0+). -- Allow specified `ignore_changes` for autoacaling group. ## 2.0.0 diff --git a/README.md b/README.md index 1e38123..f194cae 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,6 @@ We will reevaluate things when [Terraform 0.12](https://www.hashicorp.com/blog/t - `max_size` - Maximum number of EC2 instances in cluster (default: `1`) - `enabled_metrics` - A list of metrics to gather for the cluster - `subnet_ids` - A list of subnet IDs to launch cluster instances -- `ignore_changes` - A list of fields that want be be ignore changes for autoscaling group. for example, "desired_capacity" (default: `[]`) - `project` - Name of project this cluster is for (default: `Unknown`) - `environment` - Name of environment this cluster is targeting (default: `Unknown`) diff --git a/main.tf b/main.tf index 8fc578b..5ed16c4 100644 --- a/main.tf +++ b/main.tf @@ -179,7 +179,6 @@ resource "aws_launch_template" "container_instance" { resource "aws_autoscaling_group" "container_instance" { lifecycle { create_before_destroy = true - ignore_changes = "${var.ignore_changes}" } name = "${coalesce(var.autoscaling_group_name, local.autoscaling_group_name)}" diff --git a/variables.tf b/variables.tf index f9585b5..aa8262d 100644 --- a/variables.tf +++ b/variables.tf @@ -102,8 +102,3 @@ variable "enabled_metrics" { variable "subnet_ids" { type = "list" } - -variable "ignore_changes" { - type = "list" - default = [] -} From 458ddd97bd279cfda8e64bf20b53f5557392aaa9 Mon Sep 17 00:00:00 2001 From: yeer Date: Tue, 24 Dec 2019 13:51:39 +0800 Subject: [PATCH 4/8] change syntax for block_device_mappings --- main.tf | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/main.tf b/main.tf index 5ed16c4..59326b6 100644 --- a/main.tf +++ b/main.tf @@ -140,14 +140,16 @@ data "aws_ami" "user_ami" { } resource "aws_launch_template" "container_instance" { - block_device_mappings { - device_name = "${var.lookup_latest_ami ? join("", data.aws_ami.ecs_ami.*.root_device_name) : join("", data.aws_ami.user_ami.*.root_device_name)}" - - ebs { - volume_type = "${var.root_block_device_type}" - volume_size = "${var.root_block_device_size}" + block_device_mappings = [ + { + device_name = "${var.lookup_latest_ami ? join("", data.aws_ami.ecs_ami.*.root_device_name) : join("", data.aws_ami.user_ami.*.root_device_name)}" + + ebs = [{ + volume_type = "${var.root_block_device_type}" + volume_size = "${var.root_block_device_size}" + }] } - } + ] credit_specification { cpu_credits = "${var.cpu_credit_specification}" From db795669e1aa6850c44296abda1a0defcc13810f Mon Sep 17 00:00:00 2001 From: yeer Date: Mon, 30 Dec 2019 12:13:52 +0800 Subject: [PATCH 5/8] support conditional device mapping, be able to provision ecs instance with data volume attached --- locals.tf | 31 +++++++++++++++++++++++++++++++ main.tf | 11 +---------- variables.tf | 16 ++++++++++++++++ 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/locals.tf b/locals.tf index 9c1dff6..3aa318c 100644 --- a/locals.tf +++ b/locals.tf @@ -4,4 +4,35 @@ locals { security_group_name = "sgContainerInstance" ecs_for_ec2_service_role_name = "${var.environment}ContainerInstanceProfile" ecs_service_role_name = "ecs${title(var.environment)}ServiceRole" + + root_device = { + device_name = "${var.lookup_latest_ami ? join("", data.aws_ami.ecs_ami.*.root_device_name) : join("", data.aws_ami.user_ami.*.root_device_name)}" + + ebs = [{ + volume_type = "${var.root_block_device_type}" + volume_size = "${var.root_block_device_size}" + }] + } + + data_device = { + device_name = "${var.data_block_device_name}" + + ebs = [{ + volume_type = "${var.data_block_device_type}" + volume_size = "${var.data_block_device_size}" + }] + } + + voloume_devices_without_data = [ + "${local.root_device}", + ] + + voloume_devices_with_data = [ + "${local.root_device}", + "${local.data_device}", + ] + + // https://github.com/hashicorp/terraform/issues/12453#issuecomment-311611817 + volume_end_index = "${var.enable_data_block_device ? 2 : 1}" + voloume_devices = "${slice(local.voloume_devices_with_data, 0, local.volume_end_index)}" } diff --git a/main.tf b/main.tf index 59326b6..d785fbd 100644 --- a/main.tf +++ b/main.tf @@ -140,16 +140,7 @@ data "aws_ami" "user_ami" { } resource "aws_launch_template" "container_instance" { - block_device_mappings = [ - { - device_name = "${var.lookup_latest_ami ? join("", data.aws_ami.ecs_ami.*.root_device_name) : join("", data.aws_ami.user_ami.*.root_device_name)}" - - ebs = [{ - volume_type = "${var.root_block_device_type}" - volume_size = "${var.root_block_device_size}" - }] - } - ] + block_device_mappings = ["${local.voloume_devices}"] credit_specification { cpu_credits = "${var.cpu_credit_specification}" diff --git a/variables.tf b/variables.tf index aa8262d..1dc9357 100644 --- a/variables.tf +++ b/variables.tf @@ -48,6 +48,22 @@ variable "root_block_device_size" { default = "8" } +variable "enable_data_block_device" { + default = 0 +} + +variable "data_block_device_name" { + default = "/dev/xvdb" +} + +variable "data_block_device_type" { + default = "gp2" +} + +variable "data_block_device_size" { + default = "50" +} + variable "instance_type" { default = "t2.micro" } From 5795d1e351077b38d206b8f40bbe0ab7177c1be9 Mon Sep 17 00:00:00 2001 From: yeer Date: Mon, 30 Dec 2019 12:42:20 +0800 Subject: [PATCH 6/8] fix typo --- locals.tf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/locals.tf b/locals.tf index 3aa318c..3d7b1bb 100644 --- a/locals.tf +++ b/locals.tf @@ -23,16 +23,16 @@ locals { }] } - voloume_devices_without_data = [ + volume_devices_without_data = [ "${local.root_device}", ] - voloume_devices_with_data = [ + volume_devices_with_data = [ "${local.root_device}", "${local.data_device}", ] // https://github.com/hashicorp/terraform/issues/12453#issuecomment-311611817 volume_end_index = "${var.enable_data_block_device ? 2 : 1}" - voloume_devices = "${slice(local.voloume_devices_with_data, 0, local.volume_end_index)}" + volume_devices = "${slice(local.volume_devices_with_data, 0, local.volume_end_index)}" } From ad93bcd488f2aedd610ddea467daa103fed3847f Mon Sep 17 00:00:00 2001 From: yeer Date: Mon, 30 Dec 2019 14:00:02 +0800 Subject: [PATCH 7/8] fix typo --- main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.tf b/main.tf index d785fbd..8068cc6 100644 --- a/main.tf +++ b/main.tf @@ -140,7 +140,7 @@ data "aws_ami" "user_ami" { } resource "aws_launch_template" "container_instance" { - block_device_mappings = ["${local.voloume_devices}"] + block_device_mappings = ["${local.volume_devices}"] credit_specification { cpu_credits = "${var.cpu_credit_specification}" From b3a2a3345e13e5411453f62367970bcd6f9849dd Mon Sep 17 00:00:00 2001 From: yeer Date: Mon, 30 Dec 2019 17:13:07 +0800 Subject: [PATCH 8/8] support delete_on_termination options --- locals.tf | 1 + variables.tf | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/locals.tf b/locals.tf index 3d7b1bb..41061a0 100644 --- a/locals.tf +++ b/locals.tf @@ -20,6 +20,7 @@ locals { ebs = [{ volume_type = "${var.data_block_device_type}" volume_size = "${var.data_block_device_size}" + delete_on_termination = "${var.data_block_device_delete_on_termination}" }] } diff --git a/variables.tf b/variables.tf index 1dc9357..60acc1b 100644 --- a/variables.tf +++ b/variables.tf @@ -64,6 +64,10 @@ variable "data_block_device_size" { default = "50" } +variable "data_block_device_delete_on_termination" { + default = 1 +} + variable "instance_type" { default = "t2.micro" }