|
| 1 | +data "aws_caller_identity" "current" {} |
| 2 | + |
| 3 | +locals { |
| 4 | + account_id = data.aws_caller_identity.current.account_id |
| 5 | +} |
| 6 | + |
| 7 | +data "aws_security_groups" "cms_cloud_sg" { |
| 8 | + filter { |
| 9 | + name = "group-name" |
| 10 | + values = ["cmscloud*"] |
| 11 | + } |
| 12 | + |
| 13 | + filter { |
| 14 | + name = "vpc-id" |
| 15 | + values = [var.vpc_id] |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +resource "aws_iam_role" "github_runner_resource_creation_role" { |
| 20 | + description = "Role to be assumed for resource creation" |
| 21 | + name = "${var.account_name}-github-actions-runner-creation-role" |
| 22 | + assume_role_policy = jsonencode({ |
| 23 | + Version = "2012-10-17" |
| 24 | + Statement = [{ |
| 25 | + Effect = "Allow" |
| 26 | + Action = "sts:AssumeRoleWithWebIdentity" |
| 27 | + Condition = { |
| 28 | + StringEquals = { |
| 29 | + "token.actions.githubusercontent.com:aud" = "sts.amazonaws.com" |
| 30 | + } |
| 31 | + StringLike = { |
| 32 | + "token.actions.githubusercontent.com:sub" = "repo:CMS-Enterprise/NPD:*" |
| 33 | + } |
| 34 | + } |
| 35 | + Principal = { |
| 36 | + Federated = "arn:aws:iam::${local.account_id}:oidc-provider/token.actions.githubusercontent.com" |
| 37 | + } |
| 38 | + }] |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +resource "aws_iam_role_policy_attachment" "github_runner_has_admin" { |
| 43 | + role = aws_iam_role.github_runner_resource_creation_role.id |
| 44 | + policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess" |
| 45 | +} |
| 46 | + |
| 47 | +resource "aws_iam_role_policy_attachment" "github_runner_has_ado_restriction" { |
| 48 | + role = aws_iam_role.github_runner_resource_creation_role.id |
| 49 | + policy_arn = "arn:aws:iam::${local.account_id}:policy/ADO-Restriction-Policy" |
| 50 | +} |
| 51 | + |
| 52 | +resource "aws_iam_role_policy_attachment" "github_runner_has_region_restriction" { |
| 53 | + role = aws_iam_role.github_runner_resource_creation_role.id |
| 54 | + policy_arn = "arn:aws:iam::${local.account_id}:policy/CMSCloudApprovedRegions" |
| 55 | +} |
| 56 | + |
| 57 | +resource "aws_iam_role_policy_attachment" "github_runner_has_user_creation_restriction" { |
| 58 | + role = aws_iam_role.github_runner_resource_creation_role.id |
| 59 | + policy_arn = "arn:aws:iam::${local.account_id}:policy/ct-iamCreateUserRestrictionPolicy" |
| 60 | +} |
| 61 | + |
| 62 | +resource "aws_security_group" "github_runner_security_group" { |
| 63 | + description = "Defines traffic flows to/from the GitHub Action runner" |
| 64 | + name = "${var.account_name}-github-actions-runner-sg" |
| 65 | + vpc_id = var.vpc_id |
| 66 | +} |
| 67 | + |
| 68 | +resource "aws_vpc_security_group_egress_rule" "github_runner_can_make_outgoing_requests" { |
| 69 | + description = "Allows the GitHub Runner instance to make outgoing requests" |
| 70 | + ip_protocol = "-1" |
| 71 | + cidr_ipv4 = "0.0.0.0/0" |
| 72 | + security_group_id = aws_security_group.github_runner_security_group.id |
| 73 | +} |
| 74 | + |
| 75 | +resource "aws_instance" "github_actions_instance" { |
| 76 | + ami = "ami-04345af6ff8317b5e" |
| 77 | + instance_type = "m5.xlarge" |
| 78 | + vpc_security_group_ids = concat( |
| 79 | + data.aws_security_groups.cms_cloud_sg.ids, |
| 80 | + [aws_security_group.github_runner_security_group.id] |
| 81 | + ) |
| 82 | + subnet_id = var.subnet_id |
| 83 | + iam_instance_profile = "cms-cloud-base-ec2-profile-v4" |
| 84 | + tags = { |
| 85 | + Name = "github-actions-runner-instance" |
| 86 | + } |
| 87 | +} |
0 commit comments