Skip to content

Commit 33d2707

Browse files
committed
Merge branch 'develop' of https://github.com/shashidhar087/terraform-aws-github-runner into fix/S3-tf-resource
2 parents 8f8eae6 + defc198 commit 33d2707

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1378
-1214
lines changed

.github/workflows/lambda-runner-binaries-syncer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
working-directory: modules/runner-binaries-syncer/lambdas/runner-binaries-syncer
1818

1919
steps:
20-
- uses: actions/checkout@v2
20+
- uses: actions/checkout@v3
2121
- name: Install dependencies
2222
run: yarn install
2323
- name: Run prettier

.github/workflows/lambda-runners.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
working-directory: modules/runners/lambdas/runners
1818

1919
steps:
20-
- uses: actions/checkout@v2
20+
- uses: actions/checkout@v3
2121
- name: Install dependencies
2222
run: yarn install
2323
- name: Run prettier

.github/workflows/lambda-webhook.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
working-directory: modules/webhook/lambdas/webhook
1818

1919
steps:
20-
- uses: actions/checkout@v2
20+
- uses: actions/checkout@v3
2121
- name: Install dependencies
2222
run: yarn install
2323
- name: Run prettier

.github/workflows/packer-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
working-directory: images/${{ matrix.image }}
2828
steps:
2929
- name: "Checkout"
30-
uses: actions/checkout@v2
30+
uses: actions/checkout@v3
3131

3232
- name: packer init
3333
run: packer init .

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
env:
2222
LAMBDA: ${{ matrix.lambda }}
2323
run: echo ::set-output name=name::${LAMBDA##*/}
24-
- uses: actions/checkout@v2
24+
- uses: actions/checkout@v3
2525
- name: Add zip
2626
run: apt update && apt install zip
2727
- name: Build dist
@@ -39,7 +39,7 @@ jobs:
3939
needs:
4040
prepare
4141
steps:
42-
- uses: actions/checkout@v2
42+
- uses: actions/checkout@v3
4343
with:
4444
fetch-depth: 0
4545
persist-credentials: false

.github/workflows/terraform.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
image: hashicorp/terraform:${{ matrix.terraform }}
2222
steps:
2323
- name: "Checkout"
24-
uses: actions/checkout@v2
24+
uses: actions/checkout@v3
2525
- name: "Fake zip files" # Validate will fail if it cannot find the zip files
2626
run: |
2727
touch modules/webhook/lambdas/webhook/webhook.zip
@@ -54,7 +54,7 @@ jobs:
5454
container:
5555
image: hashicorp/terraform:${{ matrix.terraform }}
5656
steps:
57-
- uses: actions/checkout@v2
57+
- uses: actions/checkout@v3
5858
- name: terraform init
5959
run: terraform init -get -backend=false -input=false
6060
- if: contains(matrix.terraform, '1.1.')

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,11 @@ For time zones please check [TZ database name column](https://en.wikipedia.org/w
304304
Currently a beta feature! You can configure runners to be ephemeral, runners will be used only for one job. The feature should be used in conjunction with listening for the workflow job event. Please consider the following:
305305

306306
- The scale down lambda is still active, and should only remove orphan instances. But there is no strict check in place. So ensure you configure the `minimum_running_time_in_minutes` to a value that is high enough to got your runner booted and connected to avoid it got terminated before executing a job.
307-
- The messages sent from the webhook lambda to scale-up lambda are by default delayed delayed by SQS, to give available runners to option to start the job before the decision is made to scale more runners. For ephemeral runners there is no need to wait. Set `delay_webhook_event` to `0`.
307+
- The messages sent from the webhook lambda to scale-up lambda are by default delayed delayed by SQS, to give available runners to option to start the job before the decision is made to scale more runners. For ephemeral runners there is no need to wait. Set `delay_webhook_event` to `0`.
308+
- All events on the queue will lead to a new runner crated by the lambda. By setting `enable_job_queued_check` to `true` you can enforce only create a runner if the event has a correlated queued job. Setting this can avoid creating useless runners, for example whn jobs got cancelled before a runner is created. We suggest to use this in combination with a pool.
308309
- To ensure runners are created in the same order GitHub sends the events we use by default a FIFO queue, this is mainly relevant for repo level runners. For ephemeral runners you can set `fifo_build_queue` to `false`.
309310
- Error related to scaling should be retried via SQS. You can configure `job_queue_retention_in_seconds` `redrive_build_queue` to tune the behavior. We have no mechanism to avoid events will never processed, which means potential no runner could be created and the job in GitHub can time out in 6 hours.
310-
311+
311312
The example for [ephemeral runners](./examples/ephemeral) is based on the [default example](./examples/default). Have look on the diff to see the major configuration differences.
312313

313314
### Prebuilt Images
@@ -387,22 +388,27 @@ In case the setup does not work as intended follow the trace of events:
387388
| [aws_resourcegroups_group.resourcegroups_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/resourcegroups_group) | resource |
388389
| [aws_sqs_queue.queued_builds](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue) | resource |
389390
| [aws_sqs_queue.queued_builds_dlq](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue) | resource |
391+
| [aws_sqs_queue_policy.build_queue_dlq_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue_policy) | resource |
392+
| [aws_sqs_queue_policy.build_queue_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue_policy) | resource |
390393
| [random_string.random](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) | resource |
394+
| [aws_iam_policy_document.deny_unsecure_transport](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
391395

392396
## Inputs
393397

394398
| Name | Description | Type | Default | Required |
395399
|------|-------------|------|---------|:--------:|
396400
| <a name="input_ami_filter"></a> [ami\_filter](#input\_ami\_filter) | List of maps used to create the AMI filter for the action runner AMI. By default amazon linux 2 is used. | `map(list(string))` | `null` | no |
397401
| <a name="input_ami_owners"></a> [ami\_owners](#input\_ami\_owners) | The list of owners used to select the AMI of action runner instances. | `list(string)` | <pre>[<br> "amazon"<br>]</pre> | no |
402+
| <a name="input_aws_partition"></a> [aws\_partition](#input\_aws\_partition) | (optiona) partition in the arn namespace to use if not 'aws' | `string` | `"aws"` | no |
398403
| <a name="input_aws_region"></a> [aws\_region](#input\_aws\_region) | AWS region. | `string` | n/a | yes |
399-
| <a name="input_block_device_mappings"></a> [block\_device\_mappings](#input\_block\_device\_mappings) | The EC2 instance block device configuration. Takes the following keys: `device_name`, `delete_on_termination`, `volume_type`, `volume_size`, `encrypted`, `iops` | `map(string)` | `{}` | no |
404+
| <a name="input_block_device_mappings"></a> [block\_device\_mappings](#input\_block\_device\_mappings) | The EC2 instance block device configuration. Takes the following keys: `device_name`, `delete_on_termination`, `volume_type`, `volume_size`, `encrypted`, `iops` | <pre>list(object({<br> device_name = string<br> delete_on_termination = bool<br> volume_type = string<br> volume_size = number<br> encrypted = bool<br> iops = number<br> }))</pre> | `[]` | no |
400405
| <a name="input_cloudwatch_config"></a> [cloudwatch\_config](#input\_cloudwatch\_config) | (optional) Replaces the module default cloudwatch log config. See https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html for details. | `string` | `null` | no |
401406
| <a name="input_create_service_linked_role_spot"></a> [create\_service\_linked\_role\_spot](#input\_create\_service\_linked\_role\_spot) | (optional) create the serviced linked role for spot instances that is required by the scale-up lambda. | `bool` | `false` | no |
402407
| <a name="input_delay_webhook_event"></a> [delay\_webhook\_event](#input\_delay\_webhook\_event) | The number of seconds the event accepted by the webhook is invisible on the queue before the scale up lambda will receive the event. | `number` | `30` | no |
403408
| <a name="input_disable_runner_autoupdate"></a> [disable\_runner\_autoupdate](#input\_disable\_runner\_autoupdate) | Disable the auto update of the github runner agent. Be-aware there is a grace period of 30 days, see also the [GitHub article](https://github.blog/changelog/2022-02-01-github-actions-self-hosted-runners-can-now-disable-automatic-updates/) | `bool` | `false` | no |
404409
| <a name="input_enable_cloudwatch_agent"></a> [enable\_cloudwatch\_agent](#input\_enable\_cloudwatch\_agent) | Enabling the cloudwatch agent on the ec2 runner instances, the runner contains default config. Configuration can be overridden via `cloudwatch_config`. | `bool` | `true` | no |
405410
| <a name="input_enable_ephemeral_runners"></a> [enable\_ephemeral\_runners](#input\_enable\_ephemeral\_runners) | Enable ephemeral runners, runners will only be used once. | `bool` | `false` | no |
411+
| <a name="input_enable_job_queued_check"></a> [enable\_job\_queued\_check](#input\_enable\_job\_queued\_check) | Only scale if the job event received by the scale up lambda is is in the state queued. By default enabled for non ephemeral runners and disabled for ephemeral. Set this variable to overwrite the default behavior. | `bool` | `null` | no |
406412
| <a name="input_enable_managed_runner_security_group"></a> [enable\_managed\_runner\_security\_group](#input\_enable\_managed\_runner\_security\_group) | Enabling the default managed security group creation. Unmanaged security groups can be specified via `runner_additional_security_group_ids`. | `bool` | `true` | no |
407413
| <a name="input_enable_organization_runners"></a> [enable\_organization\_runners](#input\_enable\_organization\_runners) | Register runners to organization, instead of repo level | `bool` | `false` | no |
408414
| <a name="input_enable_ssm_on_runners"></a> [enable\_ssm\_on\_runners](#input\_enable\_ssm\_on\_runners) | Enable to allow access the runner instances for debugging purposes via SSM. Note that this adds additional permissions to the runner instances. | `bool` | `false` | no |
@@ -428,6 +434,7 @@ In case the setup does not work as intended follow the trace of events:
428434
| <a name="input_lambda_subnet_ids"></a> [lambda\_subnet\_ids](#input\_lambda\_subnet\_ids) | List of subnets in which the action runners will be launched, the subnets needs to be subnets in the `vpc_id`. | `list(string)` | `[]` | no |
429435
| <a name="input_log_level"></a> [log\_level](#input\_log\_level) | Logging level for lambda logging. Valid values are 'silly', 'trace', 'debug', 'info', 'warn', 'error', 'fatal'. | `string` | `"info"` | no |
430436
| <a name="input_log_type"></a> [log\_type](#input\_log\_type) | Logging format for lambda logging. Valid values are 'json', 'pretty', 'hidden'. | `string` | `"pretty"` | no |
437+
| <a name="input_logging_kms_key_id"></a> [logging\_kms\_key\_id](#input\_logging\_kms\_key\_id) | Specifies the kms key id to encrypt the logs with | `string` | `null` | no |
431438
| <a name="input_logging_retention_in_days"></a> [logging\_retention\_in\_days](#input\_logging\_retention\_in\_days) | Specifies the number of days you want to retain log events for the lambda log group. Possible values are: 0, 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, and 3653. | `number` | `180` | no |
432439
| <a name="input_market_options"></a> [market\_options](#input\_market\_options) | DEPCRECATED: Replaced by `instance_target_capacity_type`. | `string` | `null` | no |
433440
| <a name="input_minimum_running_time_in_minutes"></a> [minimum\_running\_time\_in\_minutes](#input\_minimum\_running\_time\_in\_minutes) | The time an ec2 action runner should be running at minimum before terminated if not busy. | `number` | `null` | no |

examples/ephemeral/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ module "runners" {
6363
# size = 20
6464
# schedule_expression = "cron(* * * * ? *)"
6565
# }]
66+
#
67+
#
68+
enable_job_queued_check = true
6669

6770
# configure your pre-built AMI
6871
# enabled_userdata = false

examples/ubuntu/main.tf

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,15 @@ module "runners" {
5656
# ami_owners = [data.aws_caller_identity.current.account_id]
5757

5858

59-
block_device_mappings = {
59+
block_device_mappings = [{
6060
# Set the block device name for Ubuntu root device
61-
device_name = "/dev/sda1"
62-
}
61+
device_name = "/dev/sda1"
62+
delete_on_termination = true
63+
volume_type = "gp3"
64+
volume_size = 30
65+
encrypted = true
66+
iops = null
67+
}]
6368

6469
runner_log_files = [
6570
{

images/linux-amzn2/github_agent.linux.pkr.hcl

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ variable "subnet_id" {
3131
default = null
3232
}
3333

34+
variable "associate_public_ip_address" {
35+
description = "If using a non-default VPC, there is no public IP address assigned to the EC2 instance. If you specified a public subnet, you probably want to set this to true. Otherwise the EC2 instance won't have access to the internet"
36+
type = string
37+
default = null
38+
}
39+
3440
variable "instance_type" {
3541
description = "The instance type Packer will use for the builder"
3642
type = string
@@ -66,15 +72,22 @@ variable "snapshot_tags" {
6672
default = {}
6773
}
6874

75+
variable "custom_shell_commands" {
76+
description = "Additional commands to run on the EC2 instance, to customize the instance, like installing packages"
77+
type = list(string)
78+
default = []
79+
}
80+
6981
source "amazon-ebs" "githubrunner" {
70-
ami_name = "github-runner-amzn2-x86_64-${formatdate("YYYYMMDDhhmm", timestamp())}"
71-
instance_type = var.instance_type
72-
region = var.region
73-
security_group_id = var.security_group_id
74-
subnet_id = var.subnet_id
82+
ami_name = "github-runner-amzn2-x86_64-${formatdate("YYYYMMDDhhmm", timestamp())}"
83+
instance_type = var.instance_type
84+
region = var.region
85+
security_group_id = var.security_group_id
86+
subnet_id = var.subnet_id
87+
associate_public_ip_address = var.associate_public_ip_address
7588
source_ami_filter {
7689
filters = {
77-
name = "amzn2-ami-hvm-2.*-x86_64-ebs"
90+
name = "amzn2-ami-kernel-5.*-hvm-*-x86_64-gp2"
7891
root-device-type = "ebs"
7992
virtualization-type = "hvm"
8093
}
@@ -111,15 +124,15 @@ build {
111124
]
112125
provisioner "shell" {
113126
environment_vars = []
114-
inline = [
127+
inline = concat([
115128
"sudo yum update -y",
116129
"sudo yum install -y amazon-cloudwatch-agent curl jq git",
117130
"sudo amazon-linux-extras install docker",
118131
"sudo systemctl enable docker.service",
119132
"sudo systemctl enable containerd.service",
120133
"sudo service docker start",
121134
"sudo usermod -a -G docker ec2-user",
122-
]
135+
], var.custom_shell_commands)
123136
}
124137

125138
provisioner "file" {

images/ubuntu-focal/github_agent.ubuntu.pkr.hcl

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ variable "subnet_id" {
3131
default = null
3232
}
3333

34+
variable "associate_public_ip_address" {
35+
description = "If using a non-default VPC, there is no public IP address assigned to the EC2 instance. If you specified a public subnet, you probably want to set this to true. Otherwise the EC2 instance won't have access to the internet"
36+
type = string
37+
default = null
38+
}
39+
3440
variable "instance_type" {
3541
description = "The instance type Packer will use for the builder"
3642
type = string
@@ -66,12 +72,20 @@ variable "snapshot_tags" {
6672
default = {}
6773
}
6874

75+
variable "custom_shell_commands" {
76+
description = "Additional commands to run on the EC2 instance, to customize the instance, like installing packages"
77+
type = list(string)
78+
default = []
79+
}
80+
6981
source "amazon-ebs" "githubrunner" {
70-
ami_name = "github-runner-ubuntu-focal-amd64-${formatdate("YYYYMMDDhhmm", timestamp())}"
71-
instance_type = var.instance_type
72-
region = var.region
73-
security_group_id = var.security_group_id
74-
subnet_id = var.subnet_id
82+
ami_name = "github-runner-ubuntu-focal-amd64-${formatdate("YYYYMMDDhhmm", timestamp())}"
83+
instance_type = var.instance_type
84+
region = var.region
85+
security_group_id = var.security_group_id
86+
subnet_id = var.subnet_id
87+
associate_public_ip_address = var.associate_public_ip_address
88+
7589
source_ami_filter {
7690
filters = {
7791
name = "*/ubuntu-focal-20.04-amd64-server-*"
@@ -112,7 +126,7 @@ build {
112126
environment_vars = [
113127
"DEBIAN_FRONTEND=noninteractive"
114128
]
115-
inline = [
129+
inline = concat([
116130
"sudo apt-get -y update",
117131
"sudo apt-get -y install ca-certificates curl gnupg lsb-release",
118132
"sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg",
@@ -128,7 +142,7 @@ build {
128142
"sudo curl -f https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip -o awscliv2.zip",
129143
"unzip awscliv2.zip",
130144
"sudo ./aws/install",
131-
]
145+
], var.custom_shell_commands)
132146
}
133147

134148
provisioner "file" {

images/windows-core-2019/github_agent.windows.pkr.hcl

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,25 @@ variable "ebs_delete_on_termination" {
2525
default = true
2626
}
2727

28+
variable "associate_public_ip_address" {
29+
description = "If using a non-default VPC, there is no public IP address assigned to the EC2 instance. If you specified a public subnet, you probably want to set this to true. Otherwise the EC2 instance won't have access to the internet"
30+
type = string
31+
default = null
32+
}
33+
34+
variable "custom_shell_commands" {
35+
description = "Additional commands to run on the EC2 instance, to customize the instance, like installing packages"
36+
type = list(string)
37+
default = []
38+
}
39+
2840
source "amazon-ebs" "githubrunner" {
29-
ami_name = "github-runner-windows-core-2019-${formatdate("YYYYMMDDhhmm", timestamp())}"
30-
communicator = "winrm"
31-
instance_type = "t3a.medium"
32-
region = var.region
41+
ami_name = "github-runner-windows-core-2019-${formatdate("YYYYMMDDhhmm", timestamp())}"
42+
communicator = "winrm"
43+
instance_type = "t3a.medium"
44+
region = var.region
45+
associate_public_ip_address = var.associate_public_ip_address
46+
3347
source_ami_filter {
3448
filters = {
3549
name = "Windows_Server-2019-English-Core-ContainersLatest-*"
@@ -70,8 +84,10 @@ build {
7084
}
7185

7286
provisioner "powershell" {
73-
inline = [templatefile("./windows-provisioner.ps1", {
74-
action_runner_url = var.action_runner_url
75-
})]
87+
inline = concat([
88+
templatefile("./windows-provisioner.ps1", {
89+
action_runner_url = var.action_runner_url
90+
})
91+
], var.custom_shell_commands)
7692
}
7793
}

0 commit comments

Comments
 (0)