Skip to content

Commit 0046b1a

Browse files
authored
Merge pull request #8 from flexion/feature/docker-opts
Add Docker Variables
2 parents 5fe5f97 + 742514d commit 0046b1a

File tree

8 files changed

+183
-13
lines changed

8 files changed

+183
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ override.tf.json
3535
# Ignore CLI configuration files
3636
.terraformrc
3737
terraform.rc
38+
**/.terraform.lock.hcl

.terraform-docs.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
formatter: markdown
2+
3+
header-from: README.header.md
4+
footer-from: README.footer.md
5+
6+
sections:
7+
#hide: [footer, header]
8+
show: [header, data-sources, inputs, modules, outputs, providers, requirements, resources, footer]
9+
10+
output:
11+
file: ""
12+
mode: inject
13+
template: |-
14+
<!-- BEGIN_TF_DOCS -->
15+
{{ .Content }}
16+
<!-- END_TF_DOCS -->
17+
18+
output-values:
19+
enabled: false
20+
from: ""
21+
22+
sort:
23+
enabled: true
24+
by: name
25+
26+
settings:
27+
anchor: true
28+
color: true
29+
default: true
30+
description: false
31+
escape: true
32+
indent: 2
33+
required: true
34+
sensitive: true
35+
type: true

README.footer.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## ℹ️ Notes:
2+
> - A default filter group is always added to transform the project into a runner project.
3+
> - `additional_filter_groups` are appended after the default filter group.
4+
> - Each additional group must contain a filter with `type = "EVENT"`.
5+
> - `exclude_matched_pattern` is optional and defaults to `false`.
6+
> - `scope_configuration` is applied only when `source_location` is default or unset.
7+
8+
---
9+
10+
## ✅ Tested With
11+
12+
- Terraform v1.5+
13+
- AWS Provider v5.x
14+
- GitHub + CodeConnections integration
15+
- Default and additional filter group handling

README.header.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Terraform AWS CodeBuild Runner Project
2+
3+
This Terraform module provisions an AWS CodeBuild Runner project with an attached webhook. Currently, AWS does not support creating Runner Projects via API or CLI. This module is a workaround. Terraform creates a default project and, by applying certain webhooks, it converts the project into a Runner project. This module is useful for teams that want to run GitHub Actions on AWS-managed on-demand compute.
4+
5+
---
6+
7+
## 🛠️ Prerequisites
8+
9+
- A GitHub App **"AWS Connector for GitHub"** successfully installed and configured in your AWS account. [More info on that](https://qnetconfluence.cms.gov/display/ISFCS/Configuring+Github+Runners+using+AWS+CodeBuild)
10+
- CodeConnection itself can be provisioned via Terraform but requires manual authorization from AWS. The module will fail if the connection is not authorized.
11+
12+
13+
14+
## ✨ Features
15+
16+
- 🚀 Provisions a CodeBuild Runner project using GitHub as the source (via CodeConnections)
17+
- 📦 Adds default filter groups to capture key events (indirectly creates a runner project without official support)
18+
- ➕ Supports additional user-supplied filter groups
19+
- 🧠 Automatically configures `scope_configuration` when applicable (i.e., webhook access at repo level vs org level)
20+
- 🔁 Defaults to Lambda compute with 2 GB memory, Linux OS, and Node.js runtime
21+
22+
---
23+
24+
## 📦 Module Usage Examples
25+
26+
See the [examples](examples) dir for examples of usage:
27+
28+
1. [Access level - Repo | Compute - EC2 | Mode - Container](examples/repo-ec2)
29+
1. [Access level - Org | Compute - Lambda (4 GB) | Mode - Container](examples/org-lambda)
30+
1. [Repo-level access with additional webhook filter](examples/repo-webhook)
31+
32+
---
33+
34+
## 🧩 Default Filter Groups
35+
36+
This default filter group is the core logic that triggers creation of a runner project. Since AWS does not officially support provisioning CodeBuild runner projects via API, this webhook indirectly transforms a default CodeBuild project into a runner-enabled project.
37+
38+
```tf
39+
[ # group 1
40+
{
41+
type = "EVENT"
42+
pattern = "WORKFLOW_JOB_QUEUED"
43+
}
44+
]
45+
```
46+
47+
---
48+
49+
50+
## 🔧 Input Variables
51+

README.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ This Terraform module provisions an AWS CodeBuild Runner project with an attache
99
- A GitHub App **"AWS Connector for GitHub"** successfully installed and configured in your AWS account. [More info on that](https://qnetconfluence.cms.gov/display/ISFCS/Configuring+Github+Runners+using+AWS+CodeBuild)
1010
- CodeConnection itself can be provisioned via Terraform but requires manual authorization from AWS. The module will fail if the connection is not authorized.
1111

12-
13-
1412
## ✨ Features
1513

1614
- 🚀 Provisions a CodeBuild Runner project using GitHub as the source (via CodeConnections)
@@ -23,7 +21,7 @@ This Terraform module provisions an AWS CodeBuild Runner project with an attache
2321

2422
## 📦 Module Usage Examples
2523

26-
See the [examples](examples) dir for examples of usage:
24+
See the [examples](examples) dir for examples of usage:
2725

2826
1. [Access level - Repo | Compute - EC2 | Mode - Container](examples/repo-ec2)
2927
1. [Access level - Org | Compute - Lambda (4 GB) | Mode - Container](examples/org-lambda)
@@ -46,10 +44,8 @@ This default filter group is the core logic that triggers creation of a runner p
4644

4745
---
4846

49-
5047
## 🔧 Input Variables
5148

52-
<!-- BEGIN_TF_DOCS -->
5349
## Requirements
5450

5551
| Name | Version |
@@ -61,7 +57,7 @@ This default filter group is the core logic that triggers creation of a runner p
6157

6258
| Name | Version |
6359
|------|---------|
64-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.0.0 |
60+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.95.0 |
6561

6662
## Modules
6763

@@ -72,40 +68,60 @@ No modules.
7268
| Name | Type |
7369
|------|------|
7470
| [aws_codebuild_project.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/codebuild_project) | resource |
71+
| [aws_codebuild_source_credential.ssm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/codebuild_source_credential) | resource |
7572
| [aws_codebuild_webhook.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/codebuild_webhook) | resource |
73+
| [aws_secretsmanager_secret.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/secretsmanager_secret) | resource |
74+
| [aws_secretsmanager_secret_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/secretsmanager_secret_policy) | resource |
75+
| [aws_secretsmanager_secret_version.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/secretsmanager_secret_version) | resource |
76+
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
77+
| [aws_iam_policy_document.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
78+
| [aws_iam_role.role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_role) | data source |
79+
| [aws_ssm_parameter.github_personal_access_token](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssm_parameter) | data source |
7680

7781
## Inputs
7882

7983
| Name | Description | Type | Default | Required |
8084
|------|-------------|------|---------|:--------:|
8185
| <a name="input_additional_filter_groups"></a> [additional\_filter\_groups](#input\_additional\_filter\_groups) | Additional filter groups to be appended to the default | <pre>list(list(object({<br/> type = string<br/> pattern = string<br/> exclude_matched_pattern = optional(bool)<br/> })))</pre> | `[]` | no |
8286
| <a name="input_build_timeout"></a> [build\_timeout](#input\_build\_timeout) | Build timeout in minutes | `number` | `5` | no |
83-
| <a name="input_codeconnections_arn"></a> [codeconnections\_arn](#input\_codeconnections\_arn) | preauthorized ARN of the CodeConnection | `string` | n/a | yes |
87+
| <a name="input_cloudwatch_logs_group_name"></a> [cloudwatch\_logs\_group\_name](#input\_cloudwatch\_logs\_group\_name) | Name of the CloudWatch log group | `string` | `""` | no |
88+
| <a name="input_cloudwatch_logs_stream_name"></a> [cloudwatch\_logs\_stream\_name](#input\_cloudwatch\_logs\_stream\_name) | Name of the CloudWatch log stream | `string` | `""` | no |
89+
| <a name="input_codeconnections_arn"></a> [codeconnections\_arn](#input\_codeconnections\_arn) | preauthorized ARN of the CodeConnection | `string` | `null` | no |
8490
| <a name="input_description"></a> [description](#input\_description) | Description of the CodeBuild project | `string` | `""` | no |
91+
| <a name="input_docker_server_compute_type"></a> [docker\_server\_compute\_type](#input\_docker\_server\_compute\_type) | Compute type for the Docker server. Default: null. Valid values: BUILD\_GENERAL1\_SMALL, BUILD\_GENERAL1\_MEDIUM, BUILD\_GENERAL1\_LARGE, BUILD\_GENERAL1\_XLARGE, and BUILD\_GENERAL1\_2XLARGE. | `string` | `null` | no |
92+
| <a name="input_docker_server_security_group_ids"></a> [docker\_server\_security\_group\_ids](#input\_docker\_server\_security\_group\_ids) | The list of Security Group IDs for the Docker server. | `list(string)` | `null` | no |
8593
| <a name="input_environment_compute_type"></a> [environment\_compute\_type](#input\_environment\_compute\_type) | BUILD\_GENERAL1\_SMALL, BUILD\_GENERAL1\_MEDIUM, BUILD\_LAMBDA\_2GB, BUILD\_LAMBDA\_4GB, etc | `string` | `"BUILD_LAMBDA_2GB"` | no |
8694
| <a name="input_environment_image"></a> [environment\_image](#input\_environment\_image) | applicable image of ec2 or lambda | `string` | `"aws/codebuild/amazonlinux-x86_64-lambda-standard:nodejs20"` | no |
95+
| <a name="input_environment_image_pull_creds"></a> [environment\_image\_pull\_creds](#input\_environment\_image\_pull\_creds) | Type of credentials AWS CodeBuild uses to pull images in your build. Valid values: CODEBUILD, SERVICE\_ROLE. | `string` | `"CODEBUILD"` | no |
8796
| <a name="input_environment_type"></a> [environment\_type](#input\_environment\_type) | LINUX\_CONTAINER for EC2 and LINUX\_LAMBDA\_CONTAINER for Lambda | `string` | `"LINUX_LAMBDA_CONTAINER"` | no |
8897
| <a name="input_github_org_name"></a> [github\_org\_name](#input\_github\_org\_name) | Name of your github org if webhook is of org level | `string` | n/a | yes |
98+
| <a name="input_github_personal_access_token_ssm_parameter"></a> [github\_personal\_access\_token\_ssm\_parameter](#input\_github\_personal\_access\_token\_ssm\_parameter) | The GitHub personal access token to use for accessing the repository. If not specified then GitHub auth must be configured separately. | `string` | `null` | no |
8999
| <a name="input_name"></a> [name](#input\_name) | Name of the CodeBuild project | `string` | n/a | yes |
90-
| <a name="input_service_role_arn"></a> [service\_role\_arn](#input\_service\_role\_arn) | IAM role ARN for CodeBuild to assume | `string` | n/a | yes |
100+
| <a name="input_pat_override"></a> [pat\_override](#input\_pat\_override) | Is the PAT provided an override of the default account token. Default: true | `bool` | `true` | no |
101+
| <a name="input_privileged_mode"></a> [privileged\_mode](#input\_privileged\_mode) | Is privileged mode enabled for AWS CodeBuild. Required for Docker builds. Default: false | `bool` | `false` | no |
102+
| <a name="input_service_role_name"></a> [service\_role\_name](#input\_service\_role\_name) | IAM role name for CodeBuild to assume | `string` | n/a | yes |
103+
| <a name="input_source_buildspec"></a> [source\_buildspec](#input\_source\_buildspec) | The build spec declaration to use for this build project's related builds. Enter a path from your repository's root dir. | `string` | `null` | no |
104+
| <a name="input_source_git_submodules_config_fetch"></a> [source\_git\_submodules\_config\_fetch](#input\_source\_git\_submodules\_config\_fetch) | Whether to fetch Git submodules for the AWS CodeBuild build project. | `bool` | `false` | no |
91105
| <a name="input_source_location"></a> [source\_location](#input\_source\_location) | The git remote address for the repository | `string` | `"CODEBUILD_DEFAULT_WEBHOOK_SOURCE_LOCATION"` | no |
106+
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | The VPC ID for AWS CodeBuild to launch ephemeral instances in. | `string` | `null` | no |
107+
| <a name="input_vpc_security_group_ids"></a> [vpc\_security\_group\_ids](#input\_vpc\_security\_group\_ids) | The list of Security Group IDs for AWS CodeBuild to launch ephemeral EC2 instances in. | `list(string)` | `[]` | no |
108+
| <a name="input_vpc_subnet_ids"></a> [vpc\_subnet\_ids](#input\_vpc\_subnet\_ids) | The list of Subnet IDs for AWS CodeBuild to launch ephemeral EC2 instances in. | `list(string)` | `[]` | no |
92109

93110
## Outputs
94111

95112
| Name | Description |
96113
|------|-------------|
97114
| <a name="output_project_arn"></a> [project\_arn](#output\_project\_arn) | n/a |
98115
| <a name="output_project_name"></a> [project\_name](#output\_project\_name) | n/a |
99-
<!-- END_TF_DOCS -->
100116

101-
> ℹ️ Notes:
117+
## ℹ️ Notes:
102118
> - A default filter group is always added to transform the project into a runner project.
103119
> - `additional_filter_groups` are appended after the default filter group.
104120
> - Each additional group must contain a filter with `type = "EVENT"`.
105121
> - `exclude_matched_pattern` is optional and defaults to `false`.
106122
> - `scope_configuration` is applied only when `source_location` is default or unset.
107123
108-
---
124+
---
109125

110126
## ✅ Tested With
111127

main.tf

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ resource "aws_codebuild_project" "this" {
1414
compute_type = var.environment_compute_type
1515
image = var.environment_image
1616
image_pull_credentials_type = var.environment_image_pull_creds
17+
privileged_mode = var.privileged_mode
18+
19+
dynamic "docker_server" {
20+
for_each = var.docker_server_compute_type != null ? [1] : []
21+
content {
22+
compute_type = var.docker_server_compute_type
23+
security_group_ids = var.docker_server_security_group_ids
24+
}
25+
}
1726
}
1827

1928
logs_config {
@@ -27,8 +36,9 @@ resource "aws_codebuild_project" "this" {
2736
}
2837

2938
source {
30-
type = "GITHUB"
31-
location = var.source_location
39+
type = "GITHUB"
40+
location = var.source_location
41+
buildspec = var.source_buildspec
3242

3343
dynamic "auth" {
3444
for_each = var.codeconnections_arn != null ? [1] : []
@@ -45,6 +55,13 @@ resource "aws_codebuild_project" "this" {
4555
resource = aws_secretsmanager_secret.this[0].arn
4656
}
4757
}
58+
59+
dynamic "git_submodules_config" {
60+
for_each = var.source_git_submodules_config_fetch != null ? [1] : []
61+
content {
62+
fetch_submodules = var.source_git_submodules_config_fetch
63+
}
64+
}
4865
}
4966

5067
dynamic "vpc_config" {

terraform.tfvars.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ cloudwatch_logs_group_name = ""
44
cloudwatch_logs_stream_name = ""
55
codeconnections_arn = ""
66
description = ""
7+
docker_server_compute_type = null
8+
docker_server_security_group_ids = null
79
environment_compute_type = "BUILD_LAMBDA_2GB"
810
environment_image = "aws/codebuild/amazonlinux-x86_64-lambda-standard:nodejs20"
911
environment_image_pull_creds = "CODEBUILD"
@@ -12,7 +14,10 @@ github_org_name = ""
1214
github_personal_access_token_ssm_parameter = ""
1315
name = ""
1416
pat_override = true
17+
privileged_mode = false
1518
service_role_name = ""
19+
source_buildspec = null
20+
source_git_submodules_config_fetch = false
1621
source_location = "CODEBUILD_DEFAULT_WEBHOOK_SOURCE_LOCATION"
1722
vpc_id = ""
1823
vpc_security_group_ids = []

variables.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ variable "codeconnections_arn" {
4444
default = null
4545
}
4646

47+
variable "docker_server_compute_type" {
48+
description = "Compute type for the Docker server. Default: null. Valid values: BUILD_GENERAL1_SMALL, BUILD_GENERAL1_MEDIUM, BUILD_GENERAL1_LARGE, BUILD_GENERAL1_XLARGE, and BUILD_GENERAL1_2XLARGE."
49+
type = string
50+
default = null
51+
}
52+
53+
variable "docker_server_security_group_ids" {
54+
description = "The list of Security Group IDs for the Docker server."
55+
type = list(string)
56+
default = null
57+
}
58+
4759
variable "environment_type" {
4860
type = string
4961
description = "LINUX_CONTAINER for EC2 and LINUX_LAMBDA_CONTAINER for Lambda"
@@ -68,6 +80,18 @@ variable "environment_image_pull_creds" {
6880
default = "CODEBUILD"
6981
}
7082

83+
variable "source_buildspec" {
84+
type = string
85+
description = "The build spec declaration to use for this build project's related builds. Enter a path from your repository's root dir."
86+
default = null
87+
}
88+
89+
variable "source_git_submodules_config_fetch" {
90+
type = bool
91+
description = "Whether to fetch Git submodules for the AWS CodeBuild build project."
92+
default = false
93+
}
94+
7195
variable "source_location" {
7296
type = string
7397
description = "The git remote address for the repository"
@@ -95,6 +119,12 @@ variable "pat_override" {
95119
type = bool
96120
}
97121

122+
variable "privileged_mode" {
123+
description = "Is privileged mode enabled for AWS CodeBuild. Required for Docker builds. Default: false"
124+
type = bool
125+
default = false
126+
}
127+
98128
variable "vpc_id" {
99129
type = string
100130
description = "The VPC ID for AWS CodeBuild to launch ephemeral instances in."

0 commit comments

Comments
 (0)