From 38b71c009ce933c974879711a6971a0dbb7d1071 Mon Sep 17 00:00:00 2001 From: Pedro Carmezim Date: Mon, 18 Apr 2022 16:32:36 +0100 Subject: [PATCH 1/7] initial add simple example --- .../launch_configuration/destroy.sh | 2 + .../launch_configuration/main.tf | 69 +++++++++++++++++++ .../launch_configuration/run.sh | 2 + aws/aws_autoscalling_group/simple/destroy.sh | 2 + aws/aws_autoscalling_group/simple/main.tf | 67 ++++++++++++++++++ aws/aws_autoscalling_group/simple/run.sh | 2 + 6 files changed, 144 insertions(+) create mode 100755 aws/aws_autoscalling_group/launch_configuration/destroy.sh create mode 100644 aws/aws_autoscalling_group/launch_configuration/main.tf create mode 100755 aws/aws_autoscalling_group/launch_configuration/run.sh create mode 100755 aws/aws_autoscalling_group/simple/destroy.sh create mode 100644 aws/aws_autoscalling_group/simple/main.tf create mode 100755 aws/aws_autoscalling_group/simple/run.sh diff --git a/aws/aws_autoscalling_group/launch_configuration/destroy.sh b/aws/aws_autoscalling_group/launch_configuration/destroy.sh new file mode 100755 index 00000000..0f65744a --- /dev/null +++ b/aws/aws_autoscalling_group/launch_configuration/destroy.sh @@ -0,0 +1,2 @@ +#!/bin/bash +../../../bin/destroy.sh aws diff --git a/aws/aws_autoscalling_group/launch_configuration/main.tf b/aws/aws_autoscalling_group/launch_configuration/main.tf new file mode 100644 index 00000000..66e52af4 --- /dev/null +++ b/aws/aws_autoscalling_group/launch_configuration/main.tf @@ -0,0 +1,69 @@ + +# Summary: Create a simple Autoscalling Group + +# Documentation: https://www.terraform.io/docs/language/settings/index.html +terraform { + required_version = ">= 1.0.0" + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 3.38" + } + } +} + +# Documentation: https://www.terraform.io/docs/language/providers/requirements.html +provider "aws" { + region = "us-east-1" + profile = "terraform-examples" + default_tags { + tags = { + cs_terraform_examples = "aws_db_cluster_snapshot/simple" + } + } +} + + + +# Documentation: +resource "aws_autoscaling_group" "this" { + name_prefix = var.name_prefix != "" ? "${var.name_prefix}-" : null + name = var.name != "" ? var.name : null + + launch_configuration = var.launch_configuration + vpc_zone_identifier = var.vpc_zone_identifier + max_size = var.max_size + min_size = var.min_size + desired_capacity = var.desired_capacity + + load_balancers = var.load_balancers + health_check_grace_period = var.health_check_grace_period + health_check_type = var.health_check_type + + min_elb_capacity = var.min_elb_capacity + wait_for_elb_capacity = var.wait_for_elb_capacity + target_group_arns = var.target_group_arns + default_cooldown = var.default_cooldown + force_delete = var.force_delete + termination_policies = var.termination_policies + suspended_processes = var.suspended_processes + placement_group = var.placement_group + enabled_metrics = var.enabled_metrics + metrics_granularity = var.metrics_granularity + wait_for_capacity_timeout = var.wait_for_capacity_timeout + protect_from_scale_in = var.protect_from_scale_in + + dynamic "tag" { + for_each = var.tags + content { + key = tag.key + value = tag.value + propagate_at_launch = true + } + } + + lifecycle { + create_before_destroy = true + } + +} \ No newline at end of file diff --git a/aws/aws_autoscalling_group/launch_configuration/run.sh b/aws/aws_autoscalling_group/launch_configuration/run.sh new file mode 100755 index 00000000..b84d2277 --- /dev/null +++ b/aws/aws_autoscalling_group/launch_configuration/run.sh @@ -0,0 +1,2 @@ +#!/bin/bash +../../../bin/apply.sh aws diff --git a/aws/aws_autoscalling_group/simple/destroy.sh b/aws/aws_autoscalling_group/simple/destroy.sh new file mode 100755 index 00000000..0f65744a --- /dev/null +++ b/aws/aws_autoscalling_group/simple/destroy.sh @@ -0,0 +1,2 @@ +#!/bin/bash +../../../bin/destroy.sh aws diff --git a/aws/aws_autoscalling_group/simple/main.tf b/aws/aws_autoscalling_group/simple/main.tf new file mode 100644 index 00000000..7ceb0f36 --- /dev/null +++ b/aws/aws_autoscalling_group/simple/main.tf @@ -0,0 +1,67 @@ + + +# Summary: Create a simple Autoscalling Group + +# Documentation: https://www.terraform.io/docs/language/settings/index.html +terraform { + required_version = ">= 1.0.0" + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 3.38" + } + } +} + +# Documentation: https://www.terraform.io/docs/language/providers/requirements.html +provider "aws" { + region = "us-east-1" + profile = "terraform-examples" + default_tags { + tags = { + cs_terraform_examples = "aws_autoscaling_group/simple" + } + } +} + +# Documentation: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami +# Explanation: Get most_recent version of Ubuntu Image +data "aws_ami" "changeme_aws_ami" { + most_recent = true + + # Explanation: Canonical now publishes official Ubuntu images on the Amazon cloud. + # Check supported versions here : https://uec-images.ubuntu.com/locator/ + filter { + name = "name" + values = ["ubuntu/images/hvm-ssd/ubuntu-hirsute-21.04-amd64-server-*"] + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + + # Explanation: There are different Public providers (owners), like Canonical, AWS and others . Check here: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/finding-an-ami.html + # Some examples of owners: 099720109477 - Canonical and 679593333241 - AWS + owners = ["099720109477"] +} + +# Documentation: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template +resource "aws_launch_template" "changeme_aws_launch_template" { + name_prefix = "changeme-aws-launch-template" + image_id = data.aws_ami.changeme_aws_ami.id + instance_type = "t2.micro" +} + +# Documentation: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_group +resource "aws_autoscaling_group" "changeme_aws_autoscaling_group" { + availability_zones = ["us-east-1a"] + desired_capacity = 1 + max_size = 2 + min_size = 1 + + launch_template { + id = aws_launch_template.changeme_aws_launch_template.id + version = "$Latest" + } +} \ No newline at end of file diff --git a/aws/aws_autoscalling_group/simple/run.sh b/aws/aws_autoscalling_group/simple/run.sh new file mode 100755 index 00000000..b84d2277 --- /dev/null +++ b/aws/aws_autoscalling_group/simple/run.sh @@ -0,0 +1,2 @@ +#!/bin/bash +../../../bin/apply.sh aws From 7724a25d0740073cb8f095ae99d8171f62d98578 Mon Sep 17 00:00:00 2001 From: Pedro Carmezim Date: Mon, 18 Apr 2022 16:49:31 +0100 Subject: [PATCH 2/7] add launch configuration on aws_asg --- INDEX.md | 1 + .../launch_configuration/main.tf | 70 +++++++++---------- aws/aws_autoscalling_group/simple/main.tf | 2 +- 3 files changed, 37 insertions(+), 36 deletions(-) diff --git a/INDEX.md b/INDEX.md index 88021edf..bc1b41e8 100644 --- a/INDEX.md +++ b/INDEX.md @@ -2,6 +2,7 @@ | Feature | Links/Related Features | | ------------- |:-------------:| +| `aws_autoscaling_group` | [aws](aws/aws_autoscaling_group)

[simple](aws/aws_autoscaling_group/simple)

[launch_configuration](aws/aws_autoscaling_group/launch_configuration) | | `aws_docdb_cluster` | [aws](aws/aws_docdb_cluster)

[simple](aws/aws_docdb_cluster/simple) | | `aws_db_cluster_snapshot` | [aws](aws/aws_db_cluster_snapshot)

[simple](aws/aws_db_cluster_snapshot/simple) | | `aws_db_instance` | [aws](aws/aws_db_instance)

[simple](aws/aws_db_instance/simple)

[postgres](aws/aws_db_instance/postgres)

[restore_db_from_snapshot](aws/aws_db_instance/restore_db_from_snapshot)

[db_snapshot](aws/aws_db_instance/db_snapshot) | diff --git a/aws/aws_autoscalling_group/launch_configuration/main.tf b/aws/aws_autoscalling_group/launch_configuration/main.tf index 66e52af4..047443ac 100644 --- a/aws/aws_autoscalling_group/launch_configuration/main.tf +++ b/aws/aws_autoscalling_group/launch_configuration/main.tf @@ -1,5 +1,5 @@ -# Summary: Create a simple Autoscalling Group +# Summary: Create a simple Autoscaling Group # Documentation: https://www.terraform.io/docs/language/settings/index.html terraform { @@ -18,52 +18,52 @@ provider "aws" { profile = "terraform-examples" default_tags { tags = { - cs_terraform_examples = "aws_db_cluster_snapshot/simple" + cs_terraform_examples = "aws_autoscaling_group/launch_configuration" } } } +# Documentation: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami +# Explanation: Get most_recent version of Ubuntu Image +data "aws_ami" "changeme_aws_ami" { + most_recent = true + # Explanation: Canonical now publishes official Ubuntu images on the Amazon cloud. + # Check supported versions here : https://uec-images.ubuntu.com/locator/ + filter { + name = "name" + values = ["ubuntu/images/hvm-ssd/ubuntu-hirsute-21.04-amd64-server-*"] + } -# Documentation: -resource "aws_autoscaling_group" "this" { - name_prefix = var.name_prefix != "" ? "${var.name_prefix}-" : null - name = var.name != "" ? var.name : null - - launch_configuration = var.launch_configuration - vpc_zone_identifier = var.vpc_zone_identifier - max_size = var.max_size - min_size = var.min_size - desired_capacity = var.desired_capacity + filter { + name = "virtualization-type" + values = ["hvm"] + } - load_balancers = var.load_balancers - health_check_grace_period = var.health_check_grace_period - health_check_type = var.health_check_type + # Explanation: There are different Public providers (owners), like Canonical, AWS and others . Check here: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/finding-an-ami.html + # Some examples of owners: 099720109477 - Canonical and 679593333241 - AWS + owners = ["099720109477"] +} - min_elb_capacity = var.min_elb_capacity - wait_for_elb_capacity = var.wait_for_elb_capacity - target_group_arns = var.target_group_arns - default_cooldown = var.default_cooldown - force_delete = var.force_delete - termination_policies = var.termination_policies - suspended_processes = var.suspended_processes - placement_group = var.placement_group - enabled_metrics = var.enabled_metrics - metrics_granularity = var.metrics_granularity - wait_for_capacity_timeout = var.wait_for_capacity_timeout - protect_from_scale_in = var.protect_from_scale_in +# Documentation: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/launch_configuration +resource "aws_launch_configuration" "changeme_aws_launch_configuration" { + name_prefix = "changeme-" + image_id = data.aws_ami.changeme_aws_ami.id + instance_type = "t2.micro" - dynamic "tag" { - for_each = var.tags - content { - key = tag.key - value = tag.value - propagate_at_launch = true - } + lifecycle { + create_before_destroy = true } +} + +# Documentation: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_group +resource "aws_autoscaling_group" "changeme_aws_autoscaling_group" { + name = "changeme_aws_autoscaling_group" + launch_configuration = aws_launch_configuration.changeme_aws_launch_configuration.name + min_size = 1 + max_size = 2 lifecycle { create_before_destroy = true } - } \ No newline at end of file diff --git a/aws/aws_autoscalling_group/simple/main.tf b/aws/aws_autoscalling_group/simple/main.tf index 7ceb0f36..0071c721 100644 --- a/aws/aws_autoscalling_group/simple/main.tf +++ b/aws/aws_autoscalling_group/simple/main.tf @@ -1,6 +1,6 @@ -# Summary: Create a simple Autoscalling Group +# Summary: Create a simple Autoscaling Group # Documentation: https://www.terraform.io/docs/language/settings/index.html terraform { From 24e3dc2469d1d9b5c3a4bb74d8564665f5ee9db5 Mon Sep 17 00:00:00 2001 From: Pedro Carmezim Date: Mon, 18 Apr 2022 17:12:11 +0100 Subject: [PATCH 3/7] add aws_asg with launch_configuration --- .../launch_configuration/main.tf | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/aws/aws_autoscalling_group/launch_configuration/main.tf b/aws/aws_autoscalling_group/launch_configuration/main.tf index 047443ac..dcf87bd9 100644 --- a/aws/aws_autoscalling_group/launch_configuration/main.tf +++ b/aws/aws_autoscalling_group/launch_configuration/main.tf @@ -23,6 +23,17 @@ provider "aws" { } } + +# Documentation: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc +data "aws_vpc" "changeme_default_aws_vpc" { + default = true +} + +# Documentation: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet_ids +data "aws_subnet_ids" "changeme_aws_subnet_ids" { + vpc_id = data.aws_vpc.changeme_default_aws_vpc.id +} + # Documentation: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami # Explanation: Get most_recent version of Ubuntu Image data "aws_ami" "changeme_aws_ami" { @@ -58,12 +69,14 @@ resource "aws_launch_configuration" "changeme_aws_launch_configuration" { # Documentation: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_group resource "aws_autoscaling_group" "changeme_aws_autoscaling_group" { - name = "changeme_aws_autoscaling_group" + name = "changeme-aws-autoscaling-group" launch_configuration = aws_launch_configuration.changeme_aws_launch_configuration.name min_size = 1 max_size = 2 + vpc_zone_identifier = data.aws_subnet_ids.changeme_aws_subnet_ids.ids lifecycle { create_before_destroy = true } -} \ No newline at end of file +} + From 87b4cd1869e52b063cd0496d844f9405570afec4 Mon Sep 17 00:00:00 2001 From: b4ld Date: Wed, 20 Apr 2022 15:24:33 +0100 Subject: [PATCH 4/7] spell check on asg folder --- .../launch_configuration/destroy.sh | 0 .../launch_configuration/main.tf | 0 .../launch_configuration/run.sh | 0 .../simple/destroy.sh | 0 .../simple/main.tf | 0 .../simple/run.sh | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename aws/{aws_autoscalling_group => aws_autoscaling_group}/launch_configuration/destroy.sh (100%) rename aws/{aws_autoscalling_group => aws_autoscaling_group}/launch_configuration/main.tf (100%) rename aws/{aws_autoscalling_group => aws_autoscaling_group}/launch_configuration/run.sh (100%) rename aws/{aws_autoscalling_group => aws_autoscaling_group}/simple/destroy.sh (100%) rename aws/{aws_autoscalling_group => aws_autoscaling_group}/simple/main.tf (100%) rename aws/{aws_autoscalling_group => aws_autoscaling_group}/simple/run.sh (100%) diff --git a/aws/aws_autoscalling_group/launch_configuration/destroy.sh b/aws/aws_autoscaling_group/launch_configuration/destroy.sh similarity index 100% rename from aws/aws_autoscalling_group/launch_configuration/destroy.sh rename to aws/aws_autoscaling_group/launch_configuration/destroy.sh diff --git a/aws/aws_autoscalling_group/launch_configuration/main.tf b/aws/aws_autoscaling_group/launch_configuration/main.tf similarity index 100% rename from aws/aws_autoscalling_group/launch_configuration/main.tf rename to aws/aws_autoscaling_group/launch_configuration/main.tf diff --git a/aws/aws_autoscalling_group/launch_configuration/run.sh b/aws/aws_autoscaling_group/launch_configuration/run.sh similarity index 100% rename from aws/aws_autoscalling_group/launch_configuration/run.sh rename to aws/aws_autoscaling_group/launch_configuration/run.sh diff --git a/aws/aws_autoscalling_group/simple/destroy.sh b/aws/aws_autoscaling_group/simple/destroy.sh similarity index 100% rename from aws/aws_autoscalling_group/simple/destroy.sh rename to aws/aws_autoscaling_group/simple/destroy.sh diff --git a/aws/aws_autoscalling_group/simple/main.tf b/aws/aws_autoscaling_group/simple/main.tf similarity index 100% rename from aws/aws_autoscalling_group/simple/main.tf rename to aws/aws_autoscaling_group/simple/main.tf diff --git a/aws/aws_autoscalling_group/simple/run.sh b/aws/aws_autoscaling_group/simple/run.sh similarity index 100% rename from aws/aws_autoscalling_group/simple/run.sh rename to aws/aws_autoscaling_group/simple/run.sh From 808d94a931fc703f77d284c11141e7c088e19be3 Mon Sep 17 00:00:00 2001 From: b4ld Date: Wed, 20 Apr 2022 15:27:48 +0100 Subject: [PATCH 5/7] change ami resource name --- aws/aws_autoscaling_group/launch_configuration/main.tf | 2 +- aws/aws_autoscaling_group/simple/main.tf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/aws/aws_autoscaling_group/launch_configuration/main.tf b/aws/aws_autoscaling_group/launch_configuration/main.tf index dcf87bd9..de7fcf91 100644 --- a/aws/aws_autoscaling_group/launch_configuration/main.tf +++ b/aws/aws_autoscaling_group/launch_configuration/main.tf @@ -36,7 +36,7 @@ data "aws_subnet_ids" "changeme_aws_subnet_ids" { # Documentation: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami # Explanation: Get most_recent version of Ubuntu Image -data "aws_ami" "changeme_aws_ami" { +data "aws_ami" "changeme_aws_ami_lc" { most_recent = true # Explanation: Canonical now publishes official Ubuntu images on the Amazon cloud. diff --git a/aws/aws_autoscaling_group/simple/main.tf b/aws/aws_autoscaling_group/simple/main.tf index 0071c721..e1513938 100644 --- a/aws/aws_autoscaling_group/simple/main.tf +++ b/aws/aws_autoscaling_group/simple/main.tf @@ -26,7 +26,7 @@ provider "aws" { # Documentation: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami # Explanation: Get most_recent version of Ubuntu Image -data "aws_ami" "changeme_aws_ami" { +data "aws_ami" "changeme_aws_ami_simple" { most_recent = true # Explanation: Canonical now publishes official Ubuntu images on the Amazon cloud. From ef8fb2ef36d82b7760cfeeeebc4aa3b7d13bc6ba Mon Sep 17 00:00:00 2001 From: b4ld Date: Wed, 20 Apr 2022 15:34:27 +0100 Subject: [PATCH 6/7] change asg resource name --- aws/aws_autoscaling_group/launch_configuration/main.tf | 2 +- aws/aws_autoscaling_group/simple/main.tf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/aws/aws_autoscaling_group/launch_configuration/main.tf b/aws/aws_autoscaling_group/launch_configuration/main.tf index de7fcf91..fec3b420 100644 --- a/aws/aws_autoscaling_group/launch_configuration/main.tf +++ b/aws/aws_autoscaling_group/launch_configuration/main.tf @@ -68,7 +68,7 @@ resource "aws_launch_configuration" "changeme_aws_launch_configuration" { } # Documentation: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_group -resource "aws_autoscaling_group" "changeme_aws_autoscaling_group" { +resource "aws_autoscaling_group" "changeme_aws_autoscaling_group_lc" { name = "changeme-aws-autoscaling-group" launch_configuration = aws_launch_configuration.changeme_aws_launch_configuration.name min_size = 1 diff --git a/aws/aws_autoscaling_group/simple/main.tf b/aws/aws_autoscaling_group/simple/main.tf index e1513938..98669c33 100644 --- a/aws/aws_autoscaling_group/simple/main.tf +++ b/aws/aws_autoscaling_group/simple/main.tf @@ -54,7 +54,7 @@ resource "aws_launch_template" "changeme_aws_launch_template" { } # Documentation: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_group -resource "aws_autoscaling_group" "changeme_aws_autoscaling_group" { +resource "aws_autoscaling_group" "changeme_aws_autoscaling_group_simple" { availability_zones = ["us-east-1a"] desired_capacity = 1 max_size = 2 From 6ee0810aa8f4d614d845448fcb8113bb753bf53a Mon Sep 17 00:00:00 2001 From: b4ld Date: Wed, 20 Apr 2022 15:36:09 +0100 Subject: [PATCH 7/7] change ami reference name --- aws/aws_autoscaling_group/launch_configuration/main.tf | 2 +- aws/aws_autoscaling_group/simple/main.tf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/aws/aws_autoscaling_group/launch_configuration/main.tf b/aws/aws_autoscaling_group/launch_configuration/main.tf index fec3b420..3e35e486 100644 --- a/aws/aws_autoscaling_group/launch_configuration/main.tf +++ b/aws/aws_autoscaling_group/launch_configuration/main.tf @@ -59,7 +59,7 @@ data "aws_ami" "changeme_aws_ami_lc" { # Documentation: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/launch_configuration resource "aws_launch_configuration" "changeme_aws_launch_configuration" { name_prefix = "changeme-" - image_id = data.aws_ami.changeme_aws_ami.id + image_id = data.aws_ami.changeme_aws_ami_lc.id instance_type = "t2.micro" lifecycle { diff --git a/aws/aws_autoscaling_group/simple/main.tf b/aws/aws_autoscaling_group/simple/main.tf index 98669c33..38092608 100644 --- a/aws/aws_autoscaling_group/simple/main.tf +++ b/aws/aws_autoscaling_group/simple/main.tf @@ -49,7 +49,7 @@ data "aws_ami" "changeme_aws_ami_simple" { # Documentation: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template resource "aws_launch_template" "changeme_aws_launch_template" { name_prefix = "changeme-aws-launch-template" - image_id = data.aws_ami.changeme_aws_ami.id + image_id = data.aws_ami.changeme_aws_ami_simple.id instance_type = "t2.micro" }