Skip to content

Commit b38abba

Browse files
New example with all modules called
1 parent 635771b commit b38abba

File tree

19 files changed

+394
-246
lines changed

19 files changed

+394
-246
lines changed

example/complete/main.tf

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
################################################################################
2+
## defaults
3+
################################################################################
4+
terraform {
5+
required_version = "~> 1.5"
6+
7+
required_providers {
8+
aws = {
9+
source = "hashicorp/aws"
10+
version = "~> 5.0"
11+
}
12+
}
13+
}
14+
15+
################################################################################
16+
## ecs cluster
17+
################################################################################
18+
19+
module "ecs" {
20+
source = "../modules/ecs"
21+
22+
create = true
23+
24+
ecs_cluster = {
25+
name = "arc-ecs-module-poc"
26+
configuration = {
27+
execute_command_configuration = {
28+
logging = "OVERRIDE"
29+
log_configuration = {
30+
log_group_name = "arc-poc-cluster-log-group"
31+
}
32+
}
33+
}
34+
create_cloudwatch_log_group = true
35+
service_connect_defaults = {}
36+
settings = []
37+
}
38+
39+
capacity_provider = {
40+
autoscaling_capacity_providers = {}
41+
default_capacity_provider_use_fargate = true
42+
fargate_capacity_providers = {
43+
fargate_cp = {
44+
name = "FARGATE"
45+
tags = {
46+
Environment = "develop"
47+
}
48+
}
49+
}
50+
}
51+
tags = {
52+
Project = "arc-poc-ecs"
53+
Environment = "develop"
54+
}
55+
}
56+
57+
58+
################################################################################
59+
## ALB
60+
################################################################################
61+
62+
module "alb" {
63+
source = "../../modules/alb"
64+
65+
vpc_id = "vpc-12345"
66+
67+
alb = {
68+
name = "arc-poc-alb"
69+
internal = false
70+
port = 80
71+
}
72+
73+
alb_target_group = [{
74+
name = "arc-poc-alb-tg"
75+
port = 80
76+
protocol = "HTTP"
77+
vpc_id = "vpc-12345"
78+
health_check = {
79+
enabled = true
80+
path = "/"
81+
}
82+
}]
83+
84+
listener_rules = []
85+
}
86+
87+
88+
################################################################################
89+
## health check service
90+
################################################################################
91+
92+
module "health-check" {
93+
source = "../../modules/health-check"
94+
95+
vpc_id = "vpc-12345"
96+
environment = "develop"
97+
98+
ecs = {
99+
cluster_name = module.ecs.ecs_cluster.name
100+
service_name = "arc-ecs-module-service-poc"
101+
repository_name = "12345.dkr.ecr.us-east-1.amazonaws.com/arc/arc-poc-ecs"
102+
enable_load_balancer = false
103+
}
104+
105+
task = {
106+
container_port = 8100
107+
}
108+
109+
alb = {
110+
name = module.alb.name
111+
listener_port = 8100
112+
security_group_id = ""
113+
}
114+
depends_on = [ module.alb ]
115+
}

example/health-check-service/.terraform.lock.hcl

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/health-check-service/main.tf

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
################################################################################
2+
## defaults
3+
################################################################################
4+
terraform {
5+
required_version = "~> 1.5"
6+
7+
required_providers {
8+
aws = {
9+
source = "hashicorp/aws"
10+
version = "~> 5.0"
11+
}
12+
}
13+
}
14+
15+
module "health-check" {
16+
source = "../../modules/health-check"
17+
18+
vpc_id = "vpc-0e6c09980580ecbf6"
19+
environment = "develop"
20+
aws_region = "us-east-1"
21+
22+
ecs = {
23+
cluster_name = "arc-ecs-module-poc"
24+
service_name = "arc-ecs-module-service-poc"
25+
repository_name = "884360309640.dkr.ecr.us-east-1.amazonaws.com/arc/arc-poc-ecs"
26+
enable_load_balancer = false
27+
}
28+
29+
task = {
30+
container_port = 8100
31+
}
32+
33+
alb = {
34+
name = "arc-poc-alb"
35+
listener_port = 8100
36+
security_group_id = ""
37+
}
38+
}

modules/alb/data.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
data "aws_region" "current" {}
2+
13
# Fetch all subnets in the VPC
24
data "aws_subnets" "all" {
35
filter {

modules/alb/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ terraform {
1212
}
1313
}
1414
provider "aws" {
15-
region = var.region
15+
region = data.aws_region.current
1616
}
1717

1818
###################################################################

modules/alb/outputs.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ output "alb_zone_id" {
2121
value = aws_lb.this.alb_zone_id
2222
} */
2323

24-
24+
output "alb" {
25+
value = {
26+
name = aws_lb.this.name
27+
}
28+
}
2529

2630
# Use the filtered subnets
2731
output "public_subnets" {

modules/alb/variables.tf

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
variable "create_alb" {
2-
type = bool
3-
default = false
4-
description = "A flag that decides whether to create alb"
5-
}
61

72
variable "create_listener_rule" {
83
type = bool
94
default = false
105
}
116

12-
variable "region" {
13-
type = string
14-
default = "us-east-1"
15-
}
16-
177
variable "vpc_id" {
188
type = string
199
description = "VPC in which security group for ALB has to be created"

modules/ecs-cluster/main.tf

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,30 @@ terraform {
1212
}
1313
}
1414

15+
########################################################################
16+
# CloudWatch Log Group
17+
########################################################################
18+
resource "aws_cloudwatch_log_group" "this" {
19+
count = var.ecs_cluster.create_cloudwatch_log_group ? 1 : 0
20+
21+
name = var.ecs_cluster.configuration.log_configuration.log_group_name != null ? var.ecs_cluster.configuration.log_configuration.log_group_name : "/aws/ecs/${var.ecs_cluster.name}"
22+
23+
retention_in_days = var.ecs_cluster.configuration.log_configuration.log_group_retention_in_days
24+
kms_key_id = var.ecs_cluster.configuration.log_configuration.log_group_kms_key_id
25+
26+
tags = merge(var.tags, var.ecs_cluster.configuration.log_configuration.log_group_tags)
27+
}
28+
29+
30+
########################################################################
31+
# ECS Cluster
32+
########################################################################
1533
resource "aws_ecs_cluster" "this" {
16-
count = var.create ? 1 : 0
1734

18-
name = var.ecs_cluster.cluster_name
35+
name = var.ecs_cluster.name
1936

2037
dynamic "configuration" {
21-
for_each = var.ecs_cluster.create_cloudwatch_log_group ? [var.ecs_cluster.cluster_configuration] : []
38+
for_each = var.ecs_cluster.configuration != null ? { "default" = var.ecs_cluster.configuration } : {}
2239

2340
content {
2441
dynamic "execute_command_configuration" {
@@ -29,7 +46,7 @@ resource "aws_ecs_cluster" "this" {
2946
logging = length(execute_command_configuration.value.logging) > 0 ? execute_command_configuration.value.logging : "DEFAULT"
3047

3148
dynamic "log_configuration" {
32-
for_each = length(execute_command_configuration.value.log_configuration) > 0 ? [execute_command_configuration.value.log_configuration] : []
49+
for_each = var.ecs_cluster.create_cloudwatch_log_group && length(execute_command_configuration.value.log_configuration) > 0 ? [execute_command_configuration.value.log_configuration] : []
3350

3451
content {
3552
cloud_watch_encryption_enabled = log_configuration.value.cloud_watch_encryption_enabled
@@ -44,50 +61,34 @@ resource "aws_ecs_cluster" "this" {
4461
}
4562
}
4663

47-
4864
dynamic "service_connect_defaults" {
49-
for_each = length(var.ecs_cluster.cluster_service_connect_defaults) > 0 ? [var.ecs_cluster.cluster.cluster_service_connect_defaults] : []
65+
for_each = length(var.ecs_cluster.service_connect_defaults) > 0 ? [var.ecs_cluster.cluster.service_connect_defaults] : []
5066

5167
content {
5268
namespace = service_connect_defaults.value.namespace
5369
}
5470
}
5571

56-
5772
dynamic "setting" {
58-
for_each = flatten([var.ecs_cluster.cluster_settings])
73+
for_each = flatten([var.ecs_cluster.settings])
5974

6075
content {
6176
name = setting.value.name
6277
value = setting.value.value
6378
}
6479
}
6580

66-
tags = var.tags
67-
}
68-
69-
70-
########################################################################
71-
# CloudWatch Log Group
72-
########################################################################
73-
resource "aws_cloudwatch_log_group" "this" {
74-
count = var.create && var.ecs_cluster.create_cloudwatch_log_group ? 1 : 0
75-
76-
name = var.cloudwatch.log_group_name != null ? var.cloudwatch.log_group_name : "/aws/ecs/${var.ecs_cluster.cluster_name}"
81+
tags = merge(var.tags, var.ecs_cluster.tags)
7782

78-
retention_in_days = var.cloudwatch.log_group_retention_in_days
79-
kms_key_id = var.cloudwatch.log_group_kms_key_id
80-
81-
tags = merge(var.tags, var.cloudwatch.log_group_tags)
83+
depends_on = [ aws_cloudwatch_log_group.this ]
8284
}
8385

84-
8586
################################################################################
8687
# ECS Capacity Provider - EC2
8788
################################################################################
8889

8990
resource "aws_ecs_capacity_provider" "this" {
90-
for_each = var.create ? var.capacity_provider.autoscaling_capacity_providers : {}
91+
for_each = var.capacity_provider.autoscaling_capacity_providers != null ? var.capacity_provider.autoscaling_capacity_providers : {}
9192

9293
name = each.value.name != "" ? each.value.name : each.key
9394

@@ -127,9 +128,9 @@ locals {
127128
}
128129

129130
resource "aws_ecs_cluster_capacity_providers" "this" {
130-
count = var.create && length(merge(var.capacity_provider.fargate_capacity_providers, var.capacity_provider.autoscaling_capacity_providers)) > 0 ? 1 : 0
131+
count = length(merge(var.capacity_provider.fargate_capacity_providers, var.capacity_provider.autoscaling_capacity_providers)) > 0 ? 1 : 0
131132

132-
cluster_name = var.ecs_cluster.cluster_name
133+
cluster_name = var.ecs_cluster.name
133134

134135
capacity_providers = distinct(concat(
135136
[for k, v in var.capacity_provider.fargate_capacity_providers : try(v.name, k)],
@@ -142,10 +143,9 @@ resource "aws_ecs_cluster_capacity_providers" "this" {
142143

143144
content {
144145
capacity_provider = strategy.value.name
145-
base = lookup(strategy.value, "base", null) # Adjusted lookup
146+
base = lookup(strategy.value, "base", null)
146147
weight = lookup(strategy.value, "weight", null)
147148
}
148149
}
149-
150150
depends_on = [aws_ecs_capacity_provider.this]
151151
}

modules/ecs-cluster/outputs.tf

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
output "ecs_cluster_id" {
2-
value = aws_ecs_cluster.this.id
3-
}
4-
5-
output "ecs_cluster_name" {
6-
value = aws_ecs_cluster.this.name
7-
}
1+
output "ecs_cluster" {
2+
value = {
3+
name = aws_ecs_cluster.this.name
4+
id = aws_ecs_cluster.this.id
5+
}
6+
}

0 commit comments

Comments
 (0)