Skip to content

Commit 5dba104

Browse files
authored
Merge pull request #1 from danbarr/v0.3.0
v0.3.0 - Moves the agent token to an SSM SecureString - Add outputs to module
2 parents dad76de + bab864b commit 5dba104

File tree

5 files changed

+89
-9
lines changed

5 files changed

+89
-9
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.DS_Store
22

3+
test/
4+
35
# Local .terraform directories
46
**/.terraform/*
57

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
# Terraform module aws-ecs-tfc-agent
22

3-
This module deploys a Terraform Cloud Agent task definition and service into an existing ECS Fargate cluster. It includes the required security group and IAM roles for a basic deployment. For all options, see variables.tf
3+
This module creates a Terraform Cloud Agent pool in a TFC org, and deploys a task definition and service into an existing ECS Fargate cluster. It includes the required security group and IAM roles for a basic deployment. For all options, see variables.tf
44

55
Prerequisites:
66

77
- An existing VPC with at least one public subnet
88
- An existing ECS Fargate cluster
99
- A Terraform Cloud organization with self-hosted agent support (Business tier), or a Terraform Enterprise instance
1010

11+
Hat tip to Andy Assareh for his [excellent examples](https://github.com/assareh/tfc-agent).
12+
1113
Minimal example using the standard agent image (hashicorp/tfc-agent):
1214

1315
```terraform
1416
module "agent_standard" {
15-
source = "github.com/danbarr/terraform-aws-ecs-tfc-agent?ref=v0.2.4"
17+
source = "github.com/danbarr/terraform-aws-ecs-tfc-agent?ref=v0.3.0"
1618
1719
name = "ecs"
1820
tfc_org_name = "My-TFC-Org"
@@ -32,7 +34,7 @@ module "agent_cluster" {
3234
}
3335
3436
module "agent_standard" {
35-
source = "github.com/danbarr/terraform-aws-ecs-tfc-agent?ref=v0.2.4"
37+
source = "github.com/danbarr/terraform-aws-ecs-tfc-agent?ref=v0.3.0"
3638
3739
name = "ecs-custom"
3840
tfc_org_name = "My-TFC-Org"

example/basic-example.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ provider "aws" {
2121
provider "tfe" {}
2222

2323
module "agent_pool" {
24-
source = "../"
24+
source = "github.com/danbarr/terraform-aws-ecs-tfc-agent?ref=v0.3.0"
2525
name = "ecs"
2626
tfc_org_name = "My-TFC-Org"
2727
agent_image = "hashicorp/tfc-agent:latest"

main.tf

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ resource "tfe_agent_token" "ecs_agent_token" {
2626
description = "${var.name}-agent-token"
2727
}
2828

29+
resource "aws_ssm_parameter" "agent_token" {
30+
name = "${var.name}-tfc-agent-token"
31+
description = "Terraform Cloud agent token"
32+
type = "SecureString"
33+
value = tfe_agent_token.ecs_agent_token.token
34+
}
35+
2936
resource "aws_ecs_task_definition" "tfc_agent" {
3037
family = "tfc-agent-${var.tfc_org_name}-${var.name}"
3138
cpu = var.agent_cpu
@@ -68,15 +75,17 @@ resource "aws_ecs_task_definition" "tfc_agent" {
6875
name = "TFC_ADDRESS",
6976
value = var.tfc_address
7077
},
71-
{
72-
name = "TFC_AGENT_TOKEN",
73-
value = tfe_agent_token.ecs_agent_token.token
74-
},
7578
{
7679
name = "TFC_AGENT_LOG_LEVEL",
7780
value = var.agent_log_level
7881
}
79-
], var.extra_env_vars)
82+
], var.extra_env_vars),
83+
secrets = [
84+
{
85+
name = "TFC_AGENT_TOKEN",
86+
valueFrom = aws_ssm_parameter.agent_token.arn
87+
}
88+
]
8089
}
8190
]
8291
)
@@ -132,6 +141,10 @@ resource "aws_security_group_rule" "allow_egress" {
132141
cidr_blocks = ["0.0.0.0/0"]
133142
}
134143

144+
## IAM
145+
# Two roles are defined: the task execution role used during initialization,
146+
# and the task role which is assumed by the container(s).
147+
135148
data "aws_iam_policy_document" "agent_assume_role_policy" {
136149
statement {
137150
effect = "Allow"
@@ -153,6 +166,20 @@ resource "aws_iam_role_policy_attachment" "ecs_task_execution_role_policy_attach
153166
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
154167
}
155168

169+
data "aws_iam_policy_document" "agent_init_policy" {
170+
statement {
171+
effect = "Allow"
172+
actions = ["ssm:GetParameters"]
173+
resources = [aws_ssm_parameter.agent_token.arn]
174+
}
175+
}
176+
177+
resource "aws_iam_role_policy" "agent_init_policy" {
178+
role = aws_iam_role.ecs_task_execution_role.name
179+
name = "AccessSSMforAgentToken"
180+
policy = data.aws_iam_policy_document.agent_init_policy.json
181+
}
182+
156183
resource "aws_iam_role" "ecs_task_role" {
157184
name = "tfc-agent-${var.tfc_org_name}-${var.name}-ecsTaskRole"
158185
assume_role_policy = data.aws_iam_policy_document.agent_assume_role_policy.json

outputs.tf

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
output "agent_pool_name" {
2+
description = "Name of the TFC agent pool."
3+
value = tfe_agent_pool.ecs_agent_pool.name
4+
}
5+
6+
output "agent_pool_id" {
7+
description = "ID of the TFC agent pool."
8+
value = tfe_agent_pool.ecs_agent_pool.id
9+
}
10+
11+
output "ecs_service_arn" {
12+
description = "ARN of the ECS service."
13+
value = aws_ecs_service.tfc_agent.id
14+
}
15+
16+
output "ecs_task_arn" {
17+
description = "ARN of the ECS task definition."
18+
value = aws_ecs_task_definition.tfc_agent.arn
19+
}
20+
21+
output "ecs_task_revision" {
22+
description = "Revision number of the ECS task definition."
23+
value = aws_ecs_task_definition.tfc_agent.revision
24+
}
25+
26+
output "log_stream_prefix" {
27+
description = "Prefix for the CloudWatch log stream."
28+
value = jsondecode(aws_ecs_task_definition.tfc_agent.container_definitions)[0].logConfiguration.options.awslogs-stream-prefix
29+
}
30+
31+
output "security_group_name" {
32+
description = "Name of the VPC security group attached to the service."
33+
value = aws_security_group.tfc_agent.name
34+
}
35+
36+
output "security_group_id" {
37+
description = "ID of the VPC security group attached to the service."
38+
value = aws_security_group.tfc_agent.id
39+
}
40+
41+
output "task_role_name" {
42+
description = "Name of the IAM role attached to the task containers."
43+
value = aws_iam_role.ecs_task_role.name
44+
}
45+
46+
output "task_role_arn" {
47+
description = "ARN of the IAM role attached to the task containers."
48+
value = aws_iam_role.ecs_task_role.arn
49+
}

0 commit comments

Comments
 (0)