|
| 1 | +locals { |
| 2 | + name_sg = var.overrides["name_sg"] == "" ? local.tags["Name"] : var.overrides["name_sg"] |
| 3 | + |
| 4 | + tags = merge( |
| 5 | + { |
| 6 | + "Name" = format("%s", var.environment) |
| 7 | + }, |
| 8 | + { |
| 9 | + "Environment" = format("%s", var.environment) |
| 10 | + }, |
| 11 | + var.tags, |
| 12 | + ) |
| 13 | +} |
| 14 | + |
| 15 | +data "aws_ami" "runner" { |
| 16 | + most_recent = "true" |
| 17 | + |
| 18 | + dynamic "filter" { |
| 19 | + for_each = var.ami_filter |
| 20 | + content { |
| 21 | + name = filter.key |
| 22 | + values = filter.value |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + owners = var.ami_owners |
| 27 | +} |
| 28 | + |
| 29 | +resource "aws_launch_template" "runner" { |
| 30 | + name = "${var.environment}-action-runner" |
| 31 | + |
| 32 | + dynamic "block_device_mappings" { |
| 33 | + for_each = [var.block_device_mappings] |
| 34 | + content { |
| 35 | + device_name = "/dev/xvda" |
| 36 | + |
| 37 | + ebs { |
| 38 | + delete_on_termination = lookup(block_device_mappings.value, "delete_on_termination", true) |
| 39 | + volume_type = lookup(block_device_mappings.value, "volume_type", "gp2") |
| 40 | + volume_size = lookup(block_device_mappings.value, "volume_size", 30) |
| 41 | + encrypted = lookup(block_device_mappings.value, "encrypted", true) |
| 42 | + iops = lookup(block_device_mappings.value, "iops", null) |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + iam_instance_profile { |
| 48 | + name = aws_iam_instance_profile.runner.name |
| 49 | + } |
| 50 | + |
| 51 | + instance_initiated_shutdown_behavior = "terminate" |
| 52 | + |
| 53 | + instance_market_options { |
| 54 | + market_type = var.market_options |
| 55 | + } |
| 56 | + |
| 57 | + image_id = data.aws_ami.runner.id |
| 58 | + instance_type = var.instance_type |
| 59 | + |
| 60 | + vpc_security_group_ids = [aws_security_group.runner_sg.id] |
| 61 | + |
| 62 | + tag_specifications { |
| 63 | + resource_type = "instance" |
| 64 | + tags = local.tags |
| 65 | + } |
| 66 | + |
| 67 | + user_data = base64encode(templatefile("${path.module}/templates/user-data.sh", { |
| 68 | + environment = var.environment |
| 69 | + pre_install = var.userdata_pre_install |
| 70 | + post_install = var.userdata_post_install |
| 71 | + s3_location_runner_distribution = var.s3_location_runner_distribution |
| 72 | + })) |
| 73 | +} |
| 74 | + |
| 75 | +resource "aws_security_group" "runner_sg" { |
| 76 | + name_prefix = "${var.environment}-github-actions-runner-sg" |
| 77 | + description = "Github Actions Runner security group" |
| 78 | + |
| 79 | + vpc_id = var.vpc_id |
| 80 | + |
| 81 | + egress { |
| 82 | + from_port = 0 |
| 83 | + to_port = 0 |
| 84 | + protocol = "-1" |
| 85 | + cidr_blocks = ["0.0.0.0/0"] |
| 86 | + } |
| 87 | + tags = merge( |
| 88 | + local.tags, |
| 89 | + { |
| 90 | + "Name" = format("%s", local.name_sg) |
| 91 | + }, |
| 92 | + ) |
| 93 | +} |
0 commit comments