Skip to content

Commit 3ffa50e

Browse files
authored
make ecs cluster creation options as default in #21 (#24)
* make ecs cluster creation options as default in #21 * updating region to match the other defaults * make default option for cloudwatch log group * fixing defaults * updating docs --------- Co-authored-by: [email protected]
1 parent 7a86698 commit 3ffa50e

File tree

5 files changed

+75
-51
lines changed

5 files changed

+75
-51
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ This file will contain any instructional information about this module.
2424

2525
## Modules
2626

27-
No modules.
27+
| Name | Source | Version |
28+
|------|--------|---------|
29+
| <a name="module_ecs_cluster"></a> [ecs\_cluster](#module\_ecs\_cluster) | terraform-aws-modules/ecs/aws | ~> 5.0 |
2830

2931
## Resources
3032

3133
| Name | Type |
3234
|------|------|
35+
| [aws_cloudwatch_log_group.cloudwatch](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
3336
| [aws_ecs_service.hcp_terraform_agent](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service) | resource |
3437
| [aws_ecs_task_definition.hcp_terraform_agent](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task_definition) | resource |
3538
| [aws_iam_role.ecs_task_execution_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
@@ -50,8 +53,6 @@ No modules.
5053

5154
| Name | Description | Type | Default | Required |
5255
|------|-------------|------|---------|:--------:|
53-
| <a name="input_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#input\_cloudwatch\_log\_group\_name) | The name of the CloudWatch log group where agent logs will be sent. | `string` | n/a | yes |
54-
| <a name="input_ecs_cluster_arn"></a> [ecs\_cluster\_arn](#input\_ecs\_cluster\_arn) | ARN of the ECS cluster where the agent will be deployed. | `string` | n/a | yes |
5556
| <a name="input_hcp_terraform_org_name"></a> [hcp\_terraform\_org\_name](#input\_hcp\_terraform\_org\_name) | The name of the HCP Terraform or HCP Terraform enterprise organization where the agent pool will be configured. The combination of `hcp_terraform_org_name` and `name` must be unique within an AWS account. | `string` | n/a | yes |
5657
| <a name="input_name"></a> [name](#input\_name) | A name to apply to resources. The combination of `name` and `hcp_terraform_org_name` must be unique within an AWS account. | `string` | n/a | yes |
5758
| <a name="input_subnet_ids"></a> [subnet\_ids](#input\_subnet\_ids) | IDs of the subnet(s) where agents can be deployed (public subnets required) | `list(string)` | n/a | yes |
@@ -64,6 +65,11 @@ No modules.
6465
| <a name="input_agent_log_level"></a> [agent\_log\_level](#input\_agent\_log\_level) | The logging verbosity for the agent. Valid values are trace, debug, info (default), warn, and error. | `string` | `"info"` | no |
6566
| <a name="input_agent_memory"></a> [agent\_memory](#input\_agent\_memory) | The amount of memory, in MB, allocated to the agent container(s). | `number` | `512` | no |
6667
| <a name="input_agent_single_execution"></a> [agent\_single\_execution](#input\_agent\_single\_execution) | Whether to use single-execution mode. | `bool` | `true` | no |
68+
| <a name="input_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#input\_cloudwatch\_log\_group\_name) | The name of the CloudWatch log group where agent logs will be sent. | `string` | `"/ecs/hcp-terraform-agent"` | no |
69+
| <a name="input_cloudwatch_log_group_retention"></a> [cloudwatch\_log\_group\_retention](#input\_cloudwatch\_log\_group\_retention) | The number of days to retain logs in the CloudWatch log group. | `number` | `7` | no |
70+
| <a name="input_create_cloudwatch_log_group"></a> [create\_cloudwatch\_log\_group](#input\_create\_cloudwatch\_log\_group) | The name of the CloudWatch log group where agent logs will be sent. | `bool` | `true` | no |
71+
| <a name="input_create_ecs_cluster"></a> [create\_ecs\_cluster](#input\_create\_ecs\_cluster) | Whether to create a new ECS cluster for the agent. | `bool` | `true` | no |
72+
| <a name="input_ecs_cluster_arn"></a> [ecs\_cluster\_arn](#input\_ecs\_cluster\_arn) | ARN of the ECS cluster where the agent will be deployed. | `string` | `"arn:aws:ecs:us-west-2:000000000000:cluster/ecs-basic"` | no |
6773
| <a name="input_extra_env_vars"></a> [extra\_env\_vars](#input\_extra\_env\_vars) | Extra environment variables to pass to the agent container. | <pre>list(object({<br> name = string<br> value = string<br> }))</pre> | `[]` | no |
6874
| <a name="input_hcp_terraform_address"></a> [hcp\_terraform\_address](#input\_hcp\_terraform\_address) | The HTTPS address of the HCP Terraform or HCP Terraform enterprise instance. | `string` | `"https://app.terraform.io"` | no |
6975
| <a name="input_num_agents"></a> [num\_agents](#input\_num\_agents) | The number of agent containers to run. | `number` | `1` | no |

examples/basic/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717
| Name | Source | Version |
1818
|------|--------|---------|
1919
| <a name="module_agent_pool"></a> [agent\_pool](#module\_agent\_pool) | ../../ | n/a |
20-
| <a name="module_ecs_cluster"></a> [ecs\_cluster](#module\_ecs\_cluster) | terraform-aws-modules/ecs/aws | ~> 5.0 |
2120
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 5.0 |
2221

2322
## Resources
2423

2524
| Name | Type |
2625
|------|------|
27-
| [aws_cloudwatch_log_group.cloudwatch](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
2826
| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
2927

3028
## Inputs

examples/basic/main.tf

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,15 @@ locals {
2222
#####################################################################################
2323

2424
module "agent_pool" {
25-
source = "../../"
26-
name = local.name
27-
hcp_terraform_org_name = var.hcp_terraform_org_name
28-
agent_image = "hashicorp/tfc-agent:latest"
29-
use_spot_instances = true
30-
agent_cpu = 512
31-
agent_memory = 1024
32-
ecs_cluster_arn = module.ecs_cluster.cluster_arn
33-
vpc_id = module.vpc.vpc_id
34-
subnet_ids = module.vpc.private_subnets
35-
cloudwatch_log_group_name = aws_cloudwatch_log_group.cloudwatch.name
25+
source = "../../"
26+
name = local.name
27+
hcp_terraform_org_name = var.hcp_terraform_org_name
28+
agent_image = "hashicorp/tfc-agent:latest"
29+
use_spot_instances = true
30+
agent_cpu = 512
31+
agent_memory = 1024
32+
vpc_id = module.vpc.vpc_id
33+
subnet_ids = module.vpc.private_subnets
3634
}
3735

3836
#####################################################################################
@@ -55,36 +53,3 @@ module "vpc" {
5553

5654
tags = local.tags
5755
}
58-
59-
#####################################################################################
60-
# ECS CLUSTER DEFINITION
61-
#####################################################################################
62-
63-
resource "aws_cloudwatch_log_group" "cloudwatch" {
64-
name = "/ecs/hcp-terraform-agents/${local.name}"
65-
retention_in_days = 7
66-
}
67-
68-
module "ecs_cluster" {
69-
source = "terraform-aws-modules/ecs/aws"
70-
version = "~> 5.0"
71-
72-
cluster_name = local.name
73-
74-
fargate_capacity_providers = {
75-
FARGATE = {
76-
default_capacity_provider_strategy = {
77-
weight = 50
78-
base = 20
79-
}
80-
}
81-
FARGATE_SPOT = {
82-
default_capacity_provider_strategy = {
83-
weight = 50
84-
}
85-
}
86-
}
87-
88-
tags = local.tags
89-
}
90-

main.tf

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ resource "aws_ssm_parameter" "agent_token" {
1717
value = tfe_agent_token.ecs_agent_token.token
1818
}
1919

20+
resource "aws_cloudwatch_log_group" "cloudwatch" {
21+
name = "/ecs/hcp-terraform-agents/${var.name}"
22+
retention_in_days = var.cloudwatch_log_group_retention
23+
}
24+
2025
resource "aws_ecs_task_definition" "hcp_terraform_agent" {
2126
family = "hcp-tf-agent-${var.hcp_terraform_org_name}-${var.name}"
2227
cpu = var.agent_cpu
@@ -41,7 +46,7 @@ resource "aws_ecs_task_definition" "hcp_terraform_agent" {
4146
logDriver : "awslogs",
4247
options : {
4348
awslogs-create-group : "true",
44-
awslogs-group : var.cloudwatch_log_group_name
49+
awslogs-group : var.create_cloudwatch_log_group ? aws_cloudwatch_log_group.cloudwatch.name : var.cloudwatch_log_group_name
4550
awslogs-region : data.aws_region.current.name
4651
awslogs-stream-prefix : "hcp-tf-${var.hcp_terraform_org_name}-${var.name}"
4752
}
@@ -81,7 +86,7 @@ resource "aws_ecs_task_definition" "hcp_terraform_agent" {
8186

8287
resource "aws_ecs_service" "hcp_terraform_agent" {
8388
name = "hcp-tf-agent-${var.name}"
84-
cluster = var.ecs_cluster_arn
89+
cluster = var.create_ecs_cluster ? module.ecs_cluster[0].cluster_arn : var.ecs_cluster_arn
8590
task_definition = aws_ecs_task_definition.hcp_terraform_agent.arn
8691
desired_count = var.num_agents
8792
propagate_tags = "SERVICE"
@@ -139,6 +144,36 @@ resource "aws_security_group_rule" "allow_egress" {
139144
description = "Egress rule for HCP Terraform agent"
140145
}
141146

147+
#####################################################################################
148+
# ECS Cluster - Optional creation of an ECS cluster to run the HCP Terraform agent
149+
#####################################################################################
150+
151+
module "ecs_cluster" {
152+
count = var.create_ecs_cluster ? 1 : 0
153+
source = "terraform-aws-modules/ecs/aws"
154+
version = "~> 5.0"
155+
156+
cluster_name = var.name
157+
158+
fargate_capacity_providers = {
159+
FARGATE = {
160+
default_capacity_provider_strategy = {
161+
weight = 50
162+
base = 20
163+
}
164+
}
165+
FARGATE_SPOT = {
166+
default_capacity_provider_strategy = {
167+
weight = 50
168+
}
169+
}
170+
}
171+
172+
tags = {
173+
Name = var.name
174+
}
175+
}
176+
142177
#####################################################################################
143178
# IAM
144179
# Two roles are defined: the task execution role used during initialization,

variables.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,28 @@ variable "num_agents" {
9797
default = 1
9898
}
9999

100+
variable "create_cloudwatch_log_group" {
101+
type = bool
102+
description = "The name of the CloudWatch log group where agent logs will be sent."
103+
default = true
104+
}
105+
106+
variable "cloudwatch_log_group_retention" {
107+
type = number
108+
description = "The number of days to retain logs in the CloudWatch log group."
109+
default = 7
110+
}
111+
100112
variable "cloudwatch_log_group_name" {
101113
type = string
102114
description = "The name of the CloudWatch log group where agent logs will be sent."
115+
default = "/ecs/hcp-terraform-agent"
116+
}
117+
118+
variable "create_ecs_cluster" {
119+
type = bool
120+
description = "Whether to create a new ECS cluster for the agent."
121+
default = true
103122
}
104123

105124
variable "ecs_cluster_arn" {
@@ -109,6 +128,7 @@ variable "ecs_cluster_arn" {
109128
condition = can(regex("^arn:aws[a-z-]*:ecs:", var.ecs_cluster_arn))
110129
error_message = "Must be a valid ECS cluster ARN."
111130
}
131+
default = "arn:aws:ecs:us-west-2:000000000000:cluster/ecs-basic"
112132
}
113133

114134
variable "use_spot_instances" {

0 commit comments

Comments
 (0)