Skip to content

How to define a "Runner project" for CodeBuild in Terraform?

0

I'm trying to configure a CodeBuild project using Terraform, and in the AWS Console, there's an option to select a "Runner project" (instead of a default project), which integrates with GitHub Actions using CodeBuild-managed runners.

How can I define this Runner project type in Terraform? I can't find clear documentation on how to set this using the AWS provider. Any examples or resources would be helpful. Thanks!

Enter imag

asked a year ago865 views

2 Answers
1
Accepted Answer

Hello.

The following documentation provides an example of a GitHub Actions self-hosted runner:
I think it's possible to configure it using "aws_codebuild_webhook".
https://docs.aws.amazon.com/codebuild/latest/userguide/action-runner.html#sample-github-action-runners-webhooks-terraform
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/codebuild_webhook

resource "aws_codebuild_webhook" "example" {
  project_name = aws_codebuild_project.example.name
  build_type   = "BUILD"
  filter_group {
    filter {
      type    = "EVENT"
      pattern = "WORKFLOW_JOB_QUEUED"
    }
  }
}

The AI ​​response from re:Post Agent suggests setting it up with "aws_codebuild_project", but I've checked the Terraform documentation and I don't think it's possible to write it this way.
https://registry.terraform.io/providers/hashicorp/aws/4.39.0/docs/resources/codebuild_project.html

EXPERT

answered a year ago

-1

To define a "Runner project" for CodeBuild in Terraform, you need to configure specific settings in your CodeBuild project resource. While there isn't a direct "Runner project" option in Terraform, you can achieve this functionality by setting up the project with the appropriate configurations.

Here's how you can set up a CodeBuild project as a runner for GitHub Actions:

  1. In your Terraform configuration for the CodeBuild project, you need to set up a webhook with specific filter patterns.

  2. The webhook should have a filter group with the following settings:

    • Set the build_type to "RUNNER_BUILDKITE_BUILD" or "RUNNER_GITHUB_ACTIONS_BUILD" depending on your use case.
    • Add an event filter with the type "EVENT" and pattern "WORKFLOW_JOB_QUEUED".
  3. Configure the project's environment variables to include necessary settings for the runner. For GitHub Actions, you might need to set CODEBUILD_CONFIG_GITHUB_ACTIONS_ENTERPRISE_REGISTRATION_NAME if you're registering runners at the enterprise level.

  4. Set the source type to "NO_SOURCE" as runner projects don't typically have a source code repository.

  5. Ensure the service role associated with the CodeBuild project has the necessary permissions for runner operations.

Here's a basic example of how your Terraform configuration might look:

resource "aws_codebuild_project" "github_actions_runner" {
  name         = "github-actions-runner"
  service_role = aws_iam_role.codebuild_service_role.arn

  artifacts {
    type = "NO_ARTIFACTS"
  }

  environment {
    compute_type                = "BUILD_GENERAL1_SMALL"
    image                       = "aws/codebuild/amazonlinux2-x86_64-standard:3.0"
    type                        = "LINUX_CONTAINER"
    image_pull_credentials_type = "CODEBUILD"

    environment_variable {
      name  = "CODEBUILD_CONFIG_GITHUB_ACTIONS_ENTERPRISE_REGISTRATION_NAME"
      value = "your-enterprise-name"
    }
  }

  source {
    type = "NO_SOURCE"
  }

  webhook {
    build_type = "RUNNER_GITHUB_ACTIONS_BUILD"
    filter_group {
      filter {
        type    = "EVENT"
        pattern = "WORKFLOW_JOB_QUEUED"
      }
    }
  }
}

Remember to adjust the configuration based on your specific requirements, such as the compute type, image, and any additional environment variables or settings needed for your runner project.

After applying this Terraform configuration, you should have a CodeBuild project set up as a runner for GitHub Actions, similar to what you would achieve through the AWS Console.

Sources
Tutorial: Configure a CodeBuild-hosted GitHub Actions runner - AWS CodeBuild
Setting up a Buildkite runner programmatically - AWS CodeBuild

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.