Skip to content

Commit ede5e96

Browse files
author
Valentin Khramtsov
committed
Code for create IAM policy and role for github-runner
1 parent df797d6 commit ede5e96

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

terraform/modules/aws-ecr/outputs.tf

+5
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ output "ecr_repository_url" {
22
value = var.create_ecr_repository ? aws_ecr_repository.this[0].repository_url : ""
33
description = "The URL of the ECR repository, or empty if not created."
44
}
5+
6+
output "ecr_repository_arn" {
7+
value = var.create_ecr_repository ? aws_ecr_repository.this[0].arn : ""
8+
description = "The ARN of the ECR repository, or empty if not created."
9+
}

terraform/modules/k8s-addons/eks-gha-runner-scale-set.tf

+52
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ locals {
77
namespace = local.helm_releases[index(local.helm_releases.*.id, "gha-runner-scale-set")].namespace
88
}
99
kube_github_runner_github_token = lookup(jsondecode(data.aws_secretsmanager_secret_version.infra.secret_string), "github_pat_token", "")
10+
service_account_name = "gha-runner-scale-set-gha-rs-no-permission"
1011

1112
gha_runner_scale_set_values = <<VALUES
1213
githubConfigUrl: "https://github.com/madopsio/madactions"
@@ -57,3 +58,54 @@ resource "helm_release" "gha_runner_scale_set" {
5758
]
5859
depends_on = [module.gha_runner_scale_set_controller_namespace]
5960
}
61+
62+
resource "aws_iam_role" "github_actions_runner_role" {
63+
count = local.gha_runner_scale_set.enabled ? 1 : 0
64+
name = "${local.gha_runner_scale_set.name}-role"
65+
66+
assume_role_policy = data.aws_iam_policy_document.github_actions_runner_assume_role_policy.json
67+
}
68+
69+
data "aws_iam_policy_document" "github_actions_runner_assume_role_policy" {
70+
statement {
71+
actions = ["sts:AssumeRoleWithWebIdentity"]
72+
effect = "Allow"
73+
74+
principals {
75+
type = "Federated"
76+
identifiers = [local.eks_oidc_provider_arn]
77+
}
78+
79+
condition {
80+
test = "StringEquals"
81+
variable = "${local.eks_oidc_provider_arn}:sub"
82+
values = ["system:serviceaccount:${module.gha_runner_scale_set_controller_namespace[0].name}:${local.service_account_name}"]
83+
}
84+
}
85+
}
86+
87+
resource "aws_iam_role_policy" "github_actions_runner_policy" {
88+
count = local.gha_runner_scale_set.enabled ? 1 : 0
89+
name = "${local.gha_runner_scale_set.name}-policy"
90+
role = aws_iam_role.github_actions_runner_role[0].id
91+
92+
policy = data.aws_iam_policy_document.github_actions_runner_policy.json
93+
}
94+
95+
data "aws_iam_policy_document" "github_actions_runner_policy" {
96+
statement {
97+
actions = [
98+
"ecr:GetDownloadUrlForLayer",
99+
"ecr:BatchGetImage",
100+
"ecr:BatchCheckLayerAvailability",
101+
"ecr:PutImage",
102+
"ecr:InitiateLayerUpload",
103+
"ecr:UploadLayerPart",
104+
"ecr:CompleteLayerUpload",
105+
"ecr:GetAuthorizationToken",
106+
"ecr:ListImages"
107+
]
108+
resources = ["*"]
109+
}
110+
}
111+

terragrunt/ACCOUNT_ID/us-east-1/demo/aws-ecr/terragrunt.hcl

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ include "env" {
99
}
1010

1111
dependencies {
12-
paths = ["../k8s-addons"]
12+
paths = ["../karpenter"]
1313
}
1414

1515
generate "providers_versions" {

terragrunt/ACCOUNT_ID/us-east-1/demo/k8s-addons/terragrunt.hcl

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ dependency "aws-r53" {
5252
}
5353

5454
dependencies {
55-
paths = ["../karpenter"]
55+
paths = ["../karpenter", "../aws-ecr"]
5656
}
5757

5858
generate "providers_versions" {

0 commit comments

Comments
 (0)