@@ -30,3 +30,64 @@ resource "aws_iam_role" "github_actions" {
3030 }
3131 )
3232}
33+
34+
35+ # GitHub Actions IAM Bootstrap Role
36+ # It can update the main deployment role's policies but cannot modify itself.
37+ resource "aws_iam_role" "github_actions_iam_bootstrap" {
38+ name = " github-actions-iam-bootstrap-role"
39+ description = " Role for GitHub Actions to deploy IAM infrastructure (iams-developer-roles stack only)"
40+ permissions_boundary = aws_iam_policy. iam_bootstrap_permissions_boundary . arn
41+ path = " /service-roles/"
42+
43+ assume_role_policy = data. aws_iam_policy_document . github_actions_iam_bootstrap_assume_role . json
44+
45+ tags = merge (
46+ local. tags ,
47+ {
48+ Name = " github-actions-iam-bootstrap-role"
49+ }
50+ )
51+ }
52+
53+ data "aws_iam_policy_document" "github_actions_iam_bootstrap_assume_role" {
54+ statement {
55+ sid = " OidcAssumeRoleForIamBootstrap"
56+ effect = " Allow"
57+ actions = [" sts:AssumeRoleWithWebIdentity" ]
58+
59+ principals {
60+ type = " Federated"
61+ identifiers = [
62+ aws_iam_openid_connect_provider . github . arn
63+ ]
64+ }
65+
66+ condition {
67+ test = " StringEquals"
68+ variable = " token.actions.githubusercontent.com:aud"
69+ values = [" sts.amazonaws.com" ]
70+ }
71+
72+ # Only allow from main branch (and events triggered from main)
73+ condition {
74+ test = " StringLike"
75+ variable = " token.actions.githubusercontent.com:sub"
76+ values = [
77+ " repo:${ var . github_org } /${ var . github_repo } :ref:refs/heads/main" ,
78+ " repo:${ var . github_org } /${ var . github_repo } :environment:*" ,
79+ ]
80+ }
81+
82+ # Only allow from the IAM bootstrap and base deployment workflows
83+ condition {
84+ test = " StringLike"
85+ variable = " token.actions.githubusercontent.com:job_workflow_ref"
86+ values = [
87+ " ${ var . github_org } /${ var . github_repo } /.github/workflows/iam-bootstrap-deploy.yaml@*" ,
88+ " ${ var . github_org } /${ var . github_repo } /.github/workflows/base-deploy.yml@*" ,
89+ " ${ var . github_org } /${ var . github_repo } /.github/workflows/cicd-2-publish.yaml@*" ,
90+ ]
91+ }
92+ }
93+ }
0 commit comments