Skip to content

Commit ae9fa83

Browse files
committed
build: Add lifecycle prevention for agent space destruction and enhance IAM role validation
1 parent 7ecec2b commit ae9fa83

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ resource "awscc_devopsagent_agent_space" "this" {
2121
}
2222

2323
depends_on = [time_sleep.iam_propagation]
24+
25+
lifecycle {
26+
prevent_destroy = true
27+
}
2428
}
2529

2630
# Primary account association (hosting account as monitor)

modules/spoke/main.tf

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ data "aws_iam_policy" "devops_agent_access" {
22
name = "AIDevOpsAgentAccessPolicy"
33
}
44

5+
locals {
6+
# Extract the hosting account ID from the AgentSpace ARN for SourceAccount condition.
7+
# arn:aws:aidevops:<region>:<account-id>:agentspace/<id>
8+
hub_account_id = regex("arn:aws:aidevops:[^:]+:([0-9]+):agentspace/.*", var.agent_space_arn)[0]
9+
}
10+
511
resource "aws_iam_role" "devops_agent" {
612
name = var.role_name
713
permissions_boundary = var.permissions_boundary_arn
@@ -16,11 +22,14 @@ resource "aws_iam_role" "devops_agent" {
1622
}
1723
Action = "sts:AssumeRole"
1824
Condition = {
19-
# ArnEquals pins account + Agent Space — no wildcard,
20-
# unlike the hub roles which use agentspace/*.
25+
# ArnEquals pins to the exact AgentSpace — no wildcard unlike hub roles.
26+
# SourceAccount is defense-in-depth; ArnEquals already implicitly validates account.
2127
ArnEquals = {
2228
"aws:SourceArn" = var.agent_space_arn
2329
}
30+
StringEquals = {
31+
"aws:SourceAccount" = local.hub_account_id
32+
}
2433
}
2534
}
2635
]

variables.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ variable "name_prefix" {
77
description = "Short slug used in IAM role names — no spaces or special chars (defaults to agent_space_name if not set)"
88
type = string
99
default = ""
10+
validation {
11+
condition = length(var.name_prefix) <= 35
12+
error_message = "name_prefix must be 35 characters or fewer — IAM role names are limited to 64 chars and the longest prefix 'DevOpsAgentRole-WebappAdmin-' is 29 chars."
13+
}
1014
}
1115

1216
variable "agent_space_description" {

0 commit comments

Comments
 (0)