Home > OS >  AWS Eventbridge: scheduling a CodeBuild job with environment variable overrides
AWS Eventbridge: scheduling a CodeBuild job with environment variable overrides

Time:04-08

When I launch an AWS CodeBuild project from the web interface, I can choose "Start Build" to start the build project with its normal configuration. Alternatively I can choose "Start build with overrides", which lets me specify, amongst others, custom environment variables for the build job.

From AWS EventBridge (events -> Rules -> Create rule), I can create a scheduled event to trigger the codebuild job, and this works. How though in EventBridge do I specify environment variable overrides for a scheduled CodeBuild job?

I presume it's possible somehow by using "additional settings" -> "Configure target input", which allows specification and templating of event JSON. I'm not sure though how how to work out, beyond blind trial and error, what this JSON should look like (to override environment variables in my case). In other words, where do I find the JSON spec for events sent to CodeBuild?

There are an number of similar questions here: e.g. AWS EventBridge scheduled events with custom details? and AWS Cloudwatch (EventBridge) Event Rule for AWS Batch with Environment Variables , but I can't find the specifics for CodeBuild jobs. I've tried the CDK docs at e.g. https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_events_targets.CodeBuildProjectProps.html , but am little wiser. I've also tried capturing the events output by EventBridge, to see what the event WITHOUT overrides looks like, but have not managed. Submitting the below (and a few variations: e.g. as "detail") as an "input constant" triggers the job, but the environment variables do not take effect:

{
  "ContainerOverrides": {
    "Environment": [{
      "Name": "SOME_VAR",
      "Value": "override value"
    }]
  }
}

There is also CodeBuild API reference at https://docs.aws.amazon.com/codebuild/latest/APIReference/API_StartBuild.html#API_StartBuild_RequestSyntax. EDIT: this seems to be the correct reference (as per my answer below).

CodePudding user response:

The rule target's event input template should match the structure of the CodeBuild API StartBuild action input. In the StartBuild action, environment variable overrides have a key of "environmentVariablesOverride" and value of an array of EnvironmentVariable objects.

Here is a sample target input transformer with one constant env var and another whose value is taken from the event payload's detail-type:

Input path:

{ "detail-type": "$.detail-type" }

Input template:

{"environmentVariablesOverride": [
  {"name":"MY_VAR","type":"PLAINTEXT","value":"foo"},
  {"name":"MY_DYNAMIC_VAR","type":"PLAINTEXT","value":<detail-type>}]
}

CodePudding user response:

I got this to work using an "input constant" like this:

{
  "environmentVariablesOverride": [{
    "name": "SOME_VAR",
    "type": "PLAINTEXT",
    "value": "override value"
  }]
}

In other words, you can ignore the fields in the sample events in EventBridge, and the overrides do not need to be specified in a "detail" field.

I used the Code Build "StartBuild" API docs at https://docs.aws.amazon.com/codebuild/latest/APIReference/API_StartBuild.html#API_StartBuild_RequestSyntax to find this format. I would presume (but have not tested) that other fields show here would work similarly (and that the API reference for other services would work similarly when using EventBridge: can anyone confirm?).

  • Related