Skip to content

Commit 105eb38

Browse files
committed
feat!: sub-modules integration into root module
1 parent 715d1f8 commit 105eb38

File tree

6 files changed

+537
-159
lines changed

6 files changed

+537
-159
lines changed

README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616

1717
## Modules
1818

19-
No modules.
19+
| Name | Source | Version |
20+
|------|--------|---------|
21+
| <a name="module_acm"></a> [acm](#module\_acm) | ./modules/acm | n/a |
22+
| <a name="module_alb"></a> [alb](#module\_alb) | ./modules/alb | n/a |
23+
| <a name="module_asg"></a> [asg](#module\_asg) | ./modules/asg | n/a |
24+
| <a name="module_capacity_provider"></a> [capacity\_provider](#module\_capacity\_provider) | ./modules/capacity-provider | n/a |
2025

2126
## Resources
2227

@@ -29,13 +34,50 @@ No modules.
2934

3035
| Name | Description | Type | Default | Required |
3136
|------|-------------|------|---------|:--------:|
37+
| <a name="input_acm_amazon_issued_certificates"></a> [acm\_amazon\_issued\_certificates](#input\_acm\_amazon\_issued\_certificates) | Amazon-issued ACM certificates to create | `any` | `{}` | no |
38+
| <a name="input_acm_imported_certificates"></a> [acm\_imported\_certificates](#input\_acm\_imported\_certificates) | Imported ACM certificates to create | `any` | `{}` | no |
39+
| <a name="input_acm_private_ca_issued_certificates"></a> [acm\_private\_ca\_issued\_certificates](#input\_acm\_private\_ca\_issued\_certificates) | Private CA Issued ACM certificates to create | `any` | `{}` | no |
40+
| <a name="input_autoscaling_group"></a> [autoscaling\_group](#input\_autoscaling\_group) | Configuration for Autoscaling Group | `any` | `{}` | no |
41+
| <a name="input_capacity_provider_default_auto_scaling_group_arn"></a> [capacity\_provider\_default\_auto\_scaling\_group\_arn](#input\_capacity\_provider\_default\_auto\_scaling\_group\_arn) | Default Autoscaling Group to use with the Capacity Providers | `string` | `null` | no |
42+
| <a name="input_capacity_providers"></a> [capacity\_providers](#input\_capacity\_providers) | Capacity Providers to create for use within the ECS Cluster | `any` | `{}` | no |
43+
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | Name of the ECS Cluster to use with the ECS Service | `string` | n/a | yes |
44+
| <a name="input_create_acm"></a> [create\_acm](#input\_create\_acm) | Creates the ACM certificates to use with the Load Balancer | `bool` | `false` | no |
45+
| <a name="input_create_alb"></a> [create\_alb](#input\_create\_alb) | Creates a new Application Load Balancer to use with the ECS Service | `bool` | `true` | no |
46+
| <a name="input_create_autoscaling_group"></a> [create\_autoscaling\_group](#input\_create\_autoscaling\_group) | Creates a new Autoscaling group to use with the ECS Service | `bool` | `true` | no |
47+
| <a name="input_create_capacity_provider"></a> [create\_capacity\_provider](#input\_create\_capacity\_provider) | Creates a new Capacity Provider to use with the Autoscaling Group | `bool` | `true` | no |
48+
| <a name="input_default_capacity_providers_strategies"></a> [default\_capacity\_providers\_strategies](#input\_default\_capacity\_providers\_strategies) | Default Capacity Provider Strategies to use | `any` | `[]` | no |
49+
| <a name="input_load_balancer"></a> [load\_balancer](#input\_load\_balancer) | Configuration for the Application Load Balancer | `any` | `{}` | no |
3250
| <a name="input_service"></a> [service](#input\_service) | Configuration for ECS Service | `any` | n/a | yes |
3351
| <a name="input_task_definition"></a> [task\_definition](#input\_task\_definition) | ECS Task Definition to use for running tasks | `any` | n/a | yes |
52+
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | Identifier of the VPC to use | `string` | n/a | yes |
3453

3554
## Outputs
3655

3756
| Name | Description |
3857
|------|-------------|
58+
| <a name="output_alb_arn"></a> [alb\_arn](#output\_alb\_arn) | ARN of the Load Balancer |
59+
| <a name="output_alb_dns_name"></a> [alb\_dns\_name](#output\_alb\_dns\_name) | DNS name of the Load Balancer |
60+
| <a name="output_alb_id"></a> [alb\_id](#output\_alb\_id) | Identifier of the Load Balancer |
61+
| <a name="output_alb_listeners_arns"></a> [alb\_listeners\_arns](#output\_alb\_listeners\_arns) | ARNs of the Listeners |
62+
| <a name="output_alb_listeners_ids"></a> [alb\_listeners\_ids](#output\_alb\_listeners\_ids) | Identifiers of the Listeners |
63+
| <a name="output_alb_target_groups_arns"></a> [alb\_target\_groups\_arns](#output\_alb\_target\_groups\_arns) | ARNs of the Target Groups |
64+
| <a name="output_alb_target_groups_ids"></a> [alb\_target\_groups\_ids](#output\_alb\_target\_groups\_ids) | Identifiers of the Target Groups |
65+
| <a name="output_alb_zone_id"></a> [alb\_zone\_id](#output\_alb\_zone\_id) | Canonical hosted zone ID of the Load Balancer (to be used in a Route 53 Alias record) |
66+
| <a name="output_amazon_issued_acm_certificates_arns"></a> [amazon\_issued\_acm\_certificates\_arns](#output\_amazon\_issued\_acm\_certificates\_arns) | ARNs of the Amazon issued ACM certificates |
67+
| <a name="output_amazon_issued_acm_certificates_validation_records"></a> [amazon\_issued\_acm\_certificates\_validation\_records](#output\_amazon\_issued\_acm\_certificates\_validation\_records) | Validation Records of the Amazon issued ACM certificates |
68+
| <a name="output_asg_arn"></a> [asg\_arn](#output\_asg\_arn) | ARN of the Autoscaling group |
69+
| <a name="output_asg_iam_instance_profile_arn"></a> [asg\_iam\_instance\_profile\_arn](#output\_asg\_iam\_instance\_profile\_arn) | ARN of the IAM Instance Profile |
70+
| <a name="output_asg_iam_instance_profile_id"></a> [asg\_iam\_instance\_profile\_id](#output\_asg\_iam\_instance\_profile\_id) | Identifier of the IAM Instance Profile |
71+
| <a name="output_asg_iam_role_id"></a> [asg\_iam\_role\_id](#output\_asg\_iam\_role\_id) | Identifier of the IAM Role |
72+
| <a name="output_asg_id"></a> [asg\_id](#output\_asg\_id) | Identifier of the Autoscaling group |
73+
| <a name="output_asg_launch_template_arn"></a> [asg\_launch\_template\_arn](#output\_asg\_launch\_template\_arn) | ARN of the Launch Template |
74+
| <a name="output_asg_launch_template_id"></a> [asg\_launch\_template\_id](#output\_asg\_launch\_template\_id) | Identifier of the Launch Template |
75+
| <a name="output_capacity_provider_arns"></a> [capacity\_provider\_arns](#output\_capacity\_provider\_arns) | ARNs for the ECS Capacity Providers |
76+
| <a name="output_capacity_provider_ecs_cluster_capacity_providers_id"></a> [capacity\_provider\_ecs\_cluster\_capacity\_providers\_id](#output\_capacity\_provider\_ecs\_cluster\_capacity\_providers\_id) | Identifier for the ECS Cluster Capacity Providers |
77+
| <a name="output_capacity_provider_ids"></a> [capacity\_provider\_ids](#output\_capacity\_provider\_ids) | Identifiers for the ECS Capacity Providers |
3978
| <a name="output_ecs_service_arn"></a> [ecs\_service\_arn](#output\_ecs\_service\_arn) | ARN of the ECS Service |
4079
| <a name="output_ecs_task_definition_arn"></a> [ecs\_task\_definition\_arn](#output\_ecs\_task\_definition\_arn) | ARN of the ECS Task Definition |
80+
| <a name="output_imported_acm_certificates_arns"></a> [imported\_acm\_certificates\_arns](#output\_imported\_acm\_certificates\_arns) | ARNs of the Imported ACM certificates |
81+
| <a name="output_private_ca_issued_acm_certificates_arns"></a> [private\_ca\_issued\_acm\_certificates\_arns](#output\_private\_ca\_issued\_acm\_certificates\_arns) | ARNs of the Private CA issued ACM certificates |
82+
| <a name="output_private_ca_issued_acm_certificates_validation_records"></a> [private\_ca\_issued\_acm\_certificates\_validation\_records](#output\_private\_ca\_issued\_acm\_certificates\_validation\_records) | Validation Records of the Private CA issued ACM certificates |
4183
<!-- END_TF_DOCS -->

examples/complete/main.tf

Lines changed: 120 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,47 @@ locals {
3838
# ECS Deployment Module
3939
################################################################################
4040

41+
data "aws_ami" "ecs_optimized_amzn_linux" {
42+
most_recent = true
43+
44+
filter {
45+
name = "name"
46+
values = ["amzn2-ami-ecs-hvm-*-x86_64-ebs"]
47+
}
48+
49+
filter {
50+
name = "virtualization-type"
51+
values = ["hvm"]
52+
}
53+
54+
owners = ["amazon"]
55+
}
56+
4157
module "ecs_deployment" {
4258
source = "../../"
4359

60+
cluster_name = local.cluster_name
61+
vpc_id = module.vpc.vpc_id
62+
63+
# ECS
64+
service = {
65+
name = local.service_name
66+
desired_count = 1
67+
force_new_deployment = true
68+
69+
network_configuration = {
70+
security_groups = [aws_security_group.allow_nginx_http_from_alb.id]
71+
subnets = module.vpc.private_subnets
72+
}
73+
74+
load_balancer = [
75+
{
76+
target_group_arn = "my-target-group"
77+
container_name = "nginx"
78+
container_port = 80
79+
}
80+
]
81+
}
4482
task_definition = {
4583
family = "${local.name_prefix}nginx"
4684
network_mode = "awsvpc"
@@ -70,93 +108,31 @@ module "ecs_deployment" {
70108
]
71109
}
72110

73-
service = {
74-
name = local.service_name
75-
cluster = local.cluster_name
76-
desired_count = 1
77-
force_new_deployment = true
78-
79-
network_configuration = {
80-
security_groups = [aws_security_group.allow_nginx_http_from_alb.id]
81-
subnets = module.vpc.private_subnets
111+
# ASG
112+
autoscaling_group = {
113+
name = "${local.name_prefix}my-asg"
114+
vpc_zone_identifier = module.vpc.private_subnets
115+
protect_from_scale_in = true
116+
117+
desired_capacity = 1
118+
min_size = 1
119+
max_size = 1
120+
121+
launch_template = {
122+
name = "${local.name_prefix}my-launch-template"
123+
image_id = data.aws_ami.ecs_optimized_amzn_linux.id
124+
instance_type = "t2.micro"
125+
vpc_security_group_ids = [aws_security_group.allow_all_within_vpc.id]
126+
user_data = <<-EOT
127+
#!/bin/bash
128+
echo ECS_CLUSTER=${local.cluster_name} >> /etc/ecs/ecs.config
129+
EOT
82130
}
83131

84-
load_balancer = [
85-
{
86-
target_group_arn = module.alb.target_groups_arns["this"]
87-
container_name = "nginx"
88-
container_port = 80
89-
}
90-
]
132+
iam_role_ec2_container_service_role_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
91133
}
92-
}
93-
94-
################################################################################
95-
# Supporting Resources
96-
################################################################################
97-
98-
module "vpc" {
99-
source = "terraform-aws-modules/vpc/aws"
100-
version = "~> 5.9.0"
101-
102-
name = local.vpc_name
103-
cidr = local.vpc_cidr
104-
105-
azs = local.vpc_azs
106-
private_subnets = local.vpc_private_subnets
107-
public_subnets = local.vpc_public_subnets
108-
109-
enable_nat_gateway = false
110-
enable_vpn_gateway = false
111-
}
112-
113-
data "aws_ami" "ecs_optimized_amzn_linux" {
114-
most_recent = true
115-
116-
filter {
117-
name = "name"
118-
values = ["amzn2-ami-ecs-hvm-*-x86_64-ebs"]
119-
}
120-
121-
filter {
122-
name = "virtualization-type"
123-
values = ["hvm"]
124-
}
125-
126-
owners = ["amazon"]
127-
}
128-
129-
module "asg" {
130-
source = "../../modules/asg"
131-
132-
name = "${local.name_prefix}my-asg"
133-
vpc_zone_identifier = module.vpc.private_subnets
134-
protect_from_scale_in = true
135-
136-
desired_capacity = 1
137-
min_size = 1
138-
max_size = 1
139-
140-
launch_template = {
141-
name = "${local.name_prefix}my-launch-template"
142-
image_id = data.aws_ami.ecs_optimized_amzn_linux.id
143-
instance_type = "t2.micro"
144-
vpc_security_group_ids = [aws_security_group.allow_all_within_vpc.id]
145-
user_data = <<-EOT
146-
#!/bin/bash
147-
echo ECS_CLUSTER=${local.cluster_name} >> /etc/ecs/ecs.config
148-
EOT
149-
}
150-
151-
iam_role_ec2_container_service_role_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
152-
}
153-
154-
module "capacity_provider" {
155-
source = "../../modules/capacity-provider"
156-
157-
ecs_cluster_name = local.cluster_name
158-
default_auto_scaling_group_arn = module.asg.arn
159134

135+
# Capacity Provider
160136
capacity_providers = {
161137
my-capacity-provider = {
162138
name = "${local.name_prefix}my-capacity-provider"
@@ -168,57 +144,78 @@ module "capacity_provider" {
168144
}
169145
}
170146
}
171-
172-
default_capacity_provider_strategies = [
147+
default_capacity_providers_strategies = [
173148
{
174149
capacity_provider = "my-capacity-provider"
175150
weight = 1
176151
base = 0
177152
}
178153
]
179-
}
180154

181-
################################################################################
182-
# # Application Load Balancer
183-
################################################################################
184-
185-
module "alb" {
186-
source = "../../modules/alb"
155+
# Amazon Certificates Manager
156+
create_acm = true
157+
acm_amazon_issued_certificates = {
158+
base_domain = {
159+
domain_name = local.base_domain
160+
validation_method = "DNS"
161+
}
162+
}
187163

188-
name = "${local.name_prefix}my-alb"
189-
internal = false
190-
security_groups_ids = [aws_security_group.alb_allow_all.id]
191-
subnets_ids = module.vpc.public_subnets
164+
# Application Load Balancer
165+
load_balancer = {
166+
name = "${local.name_prefix}my-alb"
167+
internal = false
168+
security_groups_ids = [aws_security_group.alb_allow_all.id]
169+
subnets_ids = module.vpc.public_subnets
170+
171+
target_groups = {
172+
my-target-group = {
173+
name = "${local.name_prefix}my-alb-tg"
174+
port = 80
175+
protocol = "HTTP"
176+
target_type = "ip"
177+
178+
health_check = {
179+
path = "/"
180+
}
181+
}
182+
}
192183

193-
target_groups = {
194-
this = {
195-
name = "${local.name_prefix}my-alb-tg"
196-
vpc_id = module.vpc.vpc_id
197-
port = 80
198-
protocol = "HTTP"
199-
target_type = "ip"
184+
listeners = {
185+
my-listener = {
186+
port = 443
187+
protocol = "HTTPS"
188+
certificate = "base_domain"
189+
ssl_policy = local.ssl_policy
200190

201-
health_check = {
202-
path = "/"
191+
default_action = [
192+
{
193+
type = "forward"
194+
target_group = "my-target-group"
195+
}
196+
]
203197
}
204198
}
205199
}
200+
}
206201

207-
listeners = {
208-
this = {
209-
port = 443
210-
protocol = "HTTPS"
211-
certificate_arn = module.acm.amazon_issued_acm_certificates_arns["base_domain"]
212-
ssl_policy = local.ssl_policy
213-
214-
default_action = [
215-
{
216-
type = "forward"
217-
target_group = "this"
218-
}
219-
]
220-
}
221-
}
202+
################################################################################
203+
# Supporting Resources
204+
################################################################################
205+
206+
module "vpc" {
207+
source = "terraform-aws-modules/vpc/aws"
208+
version = "~> 5.9.0"
209+
210+
name = local.vpc_name
211+
cidr = local.vpc_cidr
212+
213+
azs = local.vpc_azs
214+
private_subnets = local.vpc_private_subnets
215+
public_subnets = local.vpc_public_subnets
216+
217+
enable_nat_gateway = false
218+
enable_vpn_gateway = false
222219
}
223220

224221
################################################################################
@@ -229,31 +226,20 @@ data "aws_route53_zone" "base_domain" {
229226
name = local.base_domain
230227
}
231228

232-
module "acm" {
233-
source = "../../modules/acm"
234-
235-
amazon_issued_certificates = {
236-
base_domain = {
237-
domain_name = local.base_domain
238-
validation_method = "DNS"
239-
}
240-
}
241-
}
242-
243229
resource "aws_route53_record" "endpoint" {
244230
zone_id = data.aws_route53_zone.base_domain.zone_id
245231
name = local.endpoint
246232
type = "A"
247233

248234
alias {
249-
name = module.alb.dns_name
250-
zone_id = module.alb.zone_id
235+
name = module.ecs_deployment.alb_dns_name
236+
zone_id = module.ecs_deployment.alb_zone_id
251237
evaluate_target_health = true
252238
}
253239
}
254240

255241
resource "aws_acm_certificate_validation" "base_domain_certificate" {
256-
certificate_arn = module.acm.amazon_issued_acm_certificates_arns["base_domain"]
242+
certificate_arn = module.ecs_deployment.amazon_issued_acm_certificates_arns["base_domain"]
257243
validation_record_fqdns = [aws_route53_record.endpoint.fqdn]
258244
}
259245

0 commit comments

Comments
 (0)