@@ -88,55 +88,26 @@ Where the current equivalent now looks like:
8888### Before v3.x Example
8989
9090``` hcl
91- provider "aws" {
92- region = local.region
93- }
94-
95- locals {
96- region = "eu-west-1"
97- name = "ecs-ex-${replace(basename(path.cwd), "_", "-")}"
98-
99- user_data = <<-EOT
100- #!/bin/bash
101- cat <<'EOF' >> /etc/ecs/ecs.config
102- ECS_CLUSTER=${local.name}
103- ECS_LOGLEVEL=debug
104- EOF
105- EOT
106-
107- tags = {
108- Name = local.name
109- Example = local.name
110- Repository = "https://github.com/terraform-aws-modules/terraform-aws-ecs"
111- }
112- }
113-
114- ################################################################################
115- # ECS Module
116- ################################################################################
117-
11891module "ecs" {
119- source = "../../"
92+ source = "terraform-aws-modules/ecs/aws"
93+ version = "3.5.0"
12094
121- name = local.name
95+ name = "example"
12296 container_insights = true
12397
12498 capacity_providers = ["FARGATE", "FARGATE_SPOT", aws_ecs_capacity_provider.prov1.name]
12599
126100 default_capacity_provider_strategy = [{
127- capacity_provider = aws_ecs_capacity_provider.prov1.name # "FARGATE_SPOT"
101+ capacity_provider = aws_ecs_capacity_provider.prov1.name
128102 weight = "1"
129103 }]
130-
131- tags = local.tags
132104}
133105
134106module "ec2_profile" {
135- source = "../../modules/ecs-instance-profile"
107+ source = "terraform-aws-modules/ecs/aws//modules/ecs-instance-profile"
108+ version = "3.5.0"
136109
137110 name = local.name
138-
139- tags = local.tags
140111}
141112
142113resource "aws_ecs_capacity_provider" "prov1" {
@@ -146,118 +117,16 @@ resource "aws_ecs_capacity_provider" "prov1" {
146117 auto_scaling_group_arn = module.autoscaling.autoscaling_group_arn
147118 }
148119}
149-
150- ################################################################################
151- # Supporting Resources
152- ################################################################################
153-
154- # https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html#ecs-optimized-ami-linux
155- data "aws_ssm_parameter" "ecs_optimised_ami" {
156- name = "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended"
157- }
158-
159- module "autoscaling" {
160- source = "terraform-aws-modules/autoscaling/aws"
161- version = "~> 6.5"
162-
163- name = local.name
164-
165- image_id = jsondecode(data.aws_ssm_parameter.ecs_optimised_ami.value)["image_id"]
166- instance_type = "t3.micro"
167- ebs_optimized = true
168- enable_monitoring = true
169-
170- security_groups = [module.autoscaling_sg.security_group_id]
171- user_data = base64encode(local.user_data)
172- ignore_desired_capacity_changes = true
173-
174- iam_instance_profile_arn = module.ec2_profile.iam_instance_profile_arn
175-
176- vpc_zone_identifier = module.vpc.private_subnets
177- health_check_type = "EC2"
178- min_size = 0
179- max_size = 2
180- desired_capacity = 1
181-
182- # https://github.com/hashicorp/terraform-provider-aws/issues/12582
183- autoscaling_group_tags = {
184- AmazonECSManaged = true
185- }
186-
187- tags = local.tags
188- }
189-
190- module "autoscaling_sg" {
191- source = "terraform-aws-modules/security-group/aws"
192- version = "~> 4.0"
193-
194- name = local.name
195- description = "Autoscaling group security group"
196- vpc_id = module.vpc.vpc_id
197-
198- ingress_cidr_blocks = ["0.0.0.0/0"]
199- ingress_rules = ["https-443-tcp"]
200-
201- egress_rules = ["all-all"]
202-
203- tags = local.tags
204- }
205-
206- module "vpc" {
207- source = "terraform-aws-modules/vpc/aws"
208- version = "~> 3.0"
209-
210- name = local.name
211- cidr = "10.99.0.0/18"
212-
213- azs = ["${local.region}a", "${local.region}b", "${local.region}c"]
214- public_subnets = ["10.99.0.0/24", "10.99.1.0/24", "10.99.2.0/24"]
215- private_subnets = ["10.99.3.0/24", "10.99.4.0/24", "10.99.5.0/24"]
216-
217- enable_nat_gateway = true
218- single_nat_gateway = true
219- enable_dns_hostnames = true
220- map_public_ip_on_launch = false
221-
222- tags = local.tags
223- }
224120```
225121
226122### After v4.x Example
227123
228124``` hcl
229- provider "aws" {
230- region = local.region
231- }
232-
233- locals {
234- region = "eu-west-1"
235- name = "ecs-ex-${replace(basename(path.cwd), "_", "-")}"
236-
237- user_data = <<-EOT
238- #!/bin/bash
239- cat <<'EOF' >> /etc/ecs/ecs.config
240- ECS_CLUSTER=${local.name}
241- ECS_LOGLEVEL=debug
242- EOF
243- EOT
244-
245- tags = {
246- Name = local.name
247- Example = local.name
248- Repository = "https://github.com/terraform-aws-modules/terraform-aws-ecs"
249- }
250- }
251-
252- ################################################################################
253- # ECS Module
254- ################################################################################
255-
256125module "ecs" {
257- # source = "../../ "
258- source = "../../../terraform-aws-ecs "
126+ source = "terraform-aws-modules/ecs/aws "
127+ version = "4.0.0 "
259128
260- cluster_name = local.name
129+ cluster_name = "example"
261130
262131 fargate_capacity_providers = {
263132 "FARGATE" = {}
@@ -272,100 +141,19 @@ module "ecs" {
272141 }
273142 }
274143 }
275-
276- tags = local.tags
277- }
278-
279- ################################################################################
280- # Supporting Resources
281- ################################################################################
282-
283- # https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html#ecs-optimized-ami-linux
284- data "aws_ssm_parameter" "ecs_optimised_ami" {
285- name = "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended"
286- }
287-
288- module "autoscaling" {
289- source = "terraform-aws-modules/autoscaling/aws"
290- version = "~> 6.5"
291-
292- name = local.name
293-
294- image_id = jsondecode(data.aws_ssm_parameter.ecs_optimised_ami.value)["image_id"]
295- instance_type = "t3.micro"
296- ebs_optimized = true
297- enable_monitoring = true
298-
299- security_groups = [module.autoscaling_sg.security_group_id]
300- user_data = base64encode(local.user_data)
301- ignore_desired_capacity_changes = true
302-
303- create_iam_instance_profile = true
304- iam_role_name = local.name
305- iam_role_policies = {
306- AmazonEC2ContainerServiceforEC2Role = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
307- CloudWatchLogsFullAccess = "arn:aws:iam::aws:policy/CloudWatchLogsFullAccess"
308- }
309-
310- vpc_zone_identifier = module.vpc.private_subnets
311- health_check_type = "EC2"
312- min_size = 0
313- max_size = 2
314- desired_capacity = 1
315-
316- # https://github.com/hashicorp/terraform-provider-aws/issues/12582
317- autoscaling_group_tags = {
318- AmazonECSManaged = true
319- }
320-
321- tags = local.tags
322- }
323-
324- module "autoscaling_sg" {
325- source = "terraform-aws-modules/security-group/aws"
326- version = "~> 4.0"
327-
328- name = local.name
329- description = "Autoscaling group security group"
330- vpc_id = module.vpc.vpc_id
331-
332- ingress_cidr_blocks = ["0.0.0.0/0"]
333- ingress_rules = ["https-443-tcp"]
334-
335- egress_rules = ["all-all"]
336-
337- tags = local.tags
338144}
339145
340- module "vpc" {
341- source = "terraform-aws-modules/vpc/aws"
342- version = "~> 3.0"
343-
344- name = local.name
345- cidr = "10.99.0.0/18"
346-
347- azs = ["${local.region}a", "${local.region}b", "${local.region}c"]
348- public_subnets = ["10.99.0.0/24", "10.99.1.0/24", "10.99.2.0/24"]
349- private_subnets = ["10.99.3.0/24", "10.99.4.0/24", "10.99.5.0/24"]
350-
351- enable_nat_gateway = true
352- single_nat_gateway = true
353- enable_dns_hostnames = true
354- map_public_ip_on_launch = false
146+ module "ec2_profile" {
147+ source = "terraform-aws-modules/ecs/aws//modules/ecs-instance-profile"
148+ version = "3.5.0" # We are using v3.5 to maintain compatibility
355149
356- tags = local.tags
150+ name = "example
357151}
358152```
359153
360154### Diff of Before vs After
361155
362156``` diff
363- - module "ec2_profile" {
364- - source = "terraform-aws-modules/ecs/aws/modules/ecs-instance-profile"
365- -
366- - name = local.name
367- - }
368-
369157- resource "aws_ecs_capacity_provider" "prov1" {
370158- name = "prov1"
371159-
@@ -405,20 +193,6 @@ module "vpc" {
405193+ }
406194+ }
407195}
408-
409- module "autoscaling" {
410- source = "terraform-aws-modules/autoscaling/aws"
411- version = "~> 6.5"
412-
413- - iam_instance_profile_arn = module.ec2_profile.iam_instance_profile_arn
414-
415- + create_iam_instance_profile = true
416- + iam_role_name = local.name
417- + iam_role_policies = {
418- + AmazonEC2ContainerServiceforEC2Role = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
419- + CloudWatchLogsFullAccess = "arn:aws:iam::aws:policy/CloudWatchLogsFullAccess"
420- + }
421- }
422196```
423197
424198### State Move Commands
@@ -428,10 +202,4 @@ The `terraform state mv ...` commands assocaited with the before and after chang
428202``` sh
429203# Cluster
430204terraform state mv ' aws_ecs_capacity_provider.prov1' ' module.ecs.aws_ecs_capacity_provider.this["prov1"]'
431-
432- # IAM instance profile
433- terraform state mv ' module.ec2_profile.aws_iam_role.this' ' module.autoscaling.aws_iam_role.this[0]'
434- terraform state mv ' module.ec2_profile.aws_iam_instance_profile.this' ' module.autoscaling.aws_iam_instance_profile.this[0]'
435- terraform state mv ' module.ec2_profile.aws_iam_role_policy_attachment.ecs_ec2_cloudwatch_role' ' module.autoscaling.aws_iam_role_policy_attachment.this["CloudWatchLogsFullAccess"]'
436- terraform state mv ' module.ec2_profile.aws_iam_role_policy_attachment.ecs_ec2_role' ' module.autoscaling.aws_iam_role_policy_attachment.this["AmazonEC2ContainerServiceforEC2Role"]'
437205```
0 commit comments