Skip to content

Commit ec431b5

Browse files
committed
roles: add role to be able to push to ECR from GitHub
This adds a new role that can be used to push the orchestrator docker image from the orchestrator repo to the ECR repo. I think I have added all the necessary policies for that action (I copied the list from the AWS doc)
1 parent 55ea446 commit ec431b5

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

aws/roles.tf

+60
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ data "aws_iam_policy_document" "codebuild_policy_document" {
2323
actions = [
2424
"iam:*",
2525
"sso:TagResource",
26+
"sso:ListInstances",
2627
"ecs:*",
2728
"ecr:*",
2829
"apigateway:*",
@@ -85,3 +86,62 @@ resource "aws_iam_role_policy_attachment" "github_tf_opentofu_policy_attachment"
8586
role = aws_iam_role.github_tf.name
8687
policy_arn = aws_iam_policy.opentofu_policy.arn
8788
}
89+
90+
data "aws_iam_policy_document" "ecr_policy_document" {
91+
statement {
92+
effect = "Allow"
93+
actions = [
94+
"ecr:GetAuthorizationToken",
95+
"ecr:BatchCheckLayerAvailability",
96+
"ecr:GetDownloadUrlForLayer",
97+
"ecr:GetRepositoryPolicy",
98+
"ecr:DescribeRepositories",
99+
"ecr:ListImages",
100+
"ecr:DescribeImages",
101+
"ecr:BatchGetImage",
102+
"ecr:InitiateLayerUpload",
103+
"ecr:UploadLayerPart",
104+
"ecr:CompleteLayerUpload",
105+
"ecr:PutImage"
106+
]
107+
resources = ["*"]
108+
}
109+
}
110+
111+
resource "aws_iam_policy" "ecr_policy" {
112+
name = "ECRPushPolicy"
113+
path = "/"
114+
description = "Policy to allow push to ECR"
115+
116+
policy = data.aws_iam_policy_document.ecr_policy_document.json
117+
}
118+
119+
resource "aws_iam_role" "github_ecr_push_role" {
120+
name = "GithubActionsRoleECRPush"
121+
description = "Allow GitHub actions to push to ECR"
122+
assume_role_policy = jsonencode({
123+
Statement = [
124+
{
125+
Action = "sts:AssumeRoleWithWebIdentity"
126+
Effect = "Allow"
127+
Condition = {
128+
StringEquals = {
129+
"token.actions.githubusercontent.com:aud" = "sts.amazonaws.com"
130+
}
131+
StringLike = {
132+
"token.actions.githubusercontent.com:sub" = "repo:Homebrew/ci-orchestrator:*"
133+
}
134+
}
135+
Principal = {
136+
Federated = aws_iam_openid_connect_provider.github_actions.arn
137+
}
138+
}
139+
]
140+
Version = "2012-10-17"
141+
})
142+
}
143+
144+
resource "aws_iam_role_policy_attachment" "github_ecr_policy_attachment" {
145+
role = aws_iam_role.github_ecr_push_role.name
146+
policy_arn = aws_iam_policy.ecr_policy.arn
147+
}

0 commit comments

Comments
 (0)