Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TMET-8915-Update-GHA-local-runner-to-use-EC2 #211

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions github-actions-runner-terraform/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions github-actions-runner-terraform/cloudwatch.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "aws_cloudwatch_log_group" "main" {
name = "/ecs/${var.environment}/gh-runner-${local.gh_name_hash}"
name = "/ecs/${var.environment}/gh-runner-${local.cluster_name}"
retention_in_days = var.cloudwatch_log_retention

kms_key_id = aws_kms_key.log_enc_key.arn
Expand All @@ -11,7 +11,6 @@ resource "aws_cloudwatch_log_group" "main" {
Environment = var.environment
Automation = "Terraform"
}

}

resource "aws_kms_key" "log_enc_key" {
Expand Down
2 changes: 1 addition & 1 deletion github-actions-runner-terraform/container-definitions.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
"./entrypoint.sh"
]
}
]
]
29 changes: 14 additions & 15 deletions github-actions-runner-terraform/ecs.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
locals {
gh_name_hash = uuidv5("3505f3f5-f7e4-46df-a7b0-42f7472ebea5", "${var.environment}-${var.github_repo_owner}-${var.github_repo_name}")
cluster_name = "${var.cluster_name_prefix}-${local.gh_name_hash}"
awslogs_group = split(":", aws_cloudwatch_log_group.main.arn)[6]
}

Expand Down Expand Up @@ -40,12 +41,12 @@ data "aws_iam_policy_document" "events_assume_role_policy" {
# SG - ECS

resource "aws_security_group" "ecs_sg" {
name = "ecs-gh-runner-${local.gh_name_hash}"
description = "gh-runner-${local.gh_name_hash} container security group"
name = "ecs-gh-runner-${local.cluster_name}"
description = "gh-runner-${local.cluster_name} container security group"
vpc_id = var.ecs_vpc_id

tags = {
Name = "ecs-gh-runner-${local.gh_name_hash}"
Name = "ecs-gh-runner-${local.cluster_name}"
GHOwner = var.github_repo_owner
GHRepo = var.github_repo_name
Environment = var.environment
Expand Down Expand Up @@ -75,7 +76,7 @@ resource "aws_security_group_rule" "allow_self" {
self = true
}

## ECS schedule task
# ECS schedule task

# Allows CloudWatch Rule to run ECS Task

Expand All @@ -92,7 +93,7 @@ data "aws_iam_policy_document" "cloudwatch_target_role_policy_doc" {
}

resource "aws_iam_role" "cloudwatch_target_role" {
name = "cw-target-role-${local.gh_name_hash}"
name = "cw-target-role-${local.cluster_name}"
description = "Role allowing CloudWatch Events to run the task"
assume_role_policy = data.aws_iam_policy_document.events_assume_role_policy.json
path = var.role_path
Expand All @@ -106,7 +107,7 @@ resource "aws_iam_role_policy" "cloudwatch_target_role_policy" {
}

resource "aws_iam_role" "task_role" {
name = "ecs-task-role-${local.gh_name_hash}"
name = "ecs-task-role-${local.cluster_name}"
description = "Role allowing container definition to execute"
assume_role_policy = data.aws_iam_policy_document.ecs_assume_role_policy.json
path = var.role_path
Expand Down Expand Up @@ -172,8 +173,7 @@ data "aws_iam_policy_document" "task_role_policy_doc" {
# ECS task details

resource "aws_ecs_cluster" "github-runner" {

name = "gh-runner-${local.gh_name_hash}"
name = local.cluster_name

tags = {
Name = "github-runner"
Expand All @@ -189,11 +189,11 @@ resource "aws_ecs_cluster" "github-runner" {
}

resource "aws_ecs_task_definition" "runner_def" {
family = "gh-runner-${local.gh_name_hash}"
family = "gh-runner-${local.cluster_name}"
network_mode = "awsvpc"
task_role_arn = aws_iam_role.task_role.arn

requires_compatibilities = ["FARGATE"]
requires_compatibilities = ["EC2"]
cpu = var.task_cpu
memory = var.task_memory
execution_role_arn = aws_iam_role.task_role.arn
Expand Down Expand Up @@ -225,15 +225,14 @@ resource "aws_ecs_task_definition" "runner_def" {
}

resource "aws_ecs_service" "actions-runner" {
name = "gh-runner-${local.gh_name_hash}"
name = "gh-runner-${local.cluster_name}"
cluster = aws_ecs_cluster.github-runner.arn
task_definition = aws_ecs_task_definition.runner_def.arn
desired_count = var.ecs_desired_count
launch_type = "FARGATE"
launch_type = "EC2"
network_configuration {
subnets = [for s in var.ecs_subnet_ids : s]
security_groups = [aws_security_group.ecs_sg.id]
assign_public_ip = var.assign_public_ip
subnets = var.ecs_subnet_ids
security_groups = [aws_security_group.ecs_sg.id]
}

tags = {
Expand Down
2 changes: 1 addition & 1 deletion github-actions-runner-terraform/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ output "ecs_service_arn" {
output "ecs_security_group_id" {
description = "The security group ID of the self-hosted runner ECS service"
value = aws_security_group.ecs_sg.id
}
}
26 changes: 12 additions & 14 deletions github-actions-runner-terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# ECR variables

variable "ecr_repo_tag" {
variable "ecr_repo_url" {
description = "The URL of the ECR registry that hosts the GitHub Actions image"
type = string
description = "The tag to identify and pull the image in ECR repository"
default = "latest"
default = "037370603820.dkr.ecr.us-east-1.amazonaws.com/github-actions-runner"
}

variable "ecr_repo_url" {
description = "The URL of the ECR registry that hosts the github actions image"
variable "ecr_repo_tag" {
description = "The tag to identify and pull the image in ECR repository"
type = string
default = "037370603820.dkr.ecr.us-east-1.amazonaws.com/github-actions-runner"
default = "latest"
}

variable "ecr_repository_arns" {
Expand All @@ -18,8 +16,6 @@ variable "ecr_repository_arns" {
default = ["arn:aws:ecr:us-east-1:037370603820:repository/github-actions-runner"]
}

# ECS variables

variable "environment" {
type = string
description = "Environment name"
Expand All @@ -41,22 +37,24 @@ variable "ecs_desired_count" {
default = 0
}

variable "cluster_name_prefix" {
description = "Prefix for the ECS cluster name to make it human readable"
type = string
default = "gh-runner"
}

variable "tags" {
type = map(any)
description = "Additional tags to apply."
default = {}
}

# Cloudwatch Variables

variable "cloudwatch_log_retention" {
description = "Number of days to retain logs"
type = number
default = 731
}

# GitHub Runner Variables

variable "personal_access_token_arn" {
description = "AWS SecretsManager ARN for GitHub personal access token"
type = string
Expand Down
Loading