username hidden
username hidden

Reputation: 77

How to pass environment variable & values to codebuild from Cloudwatch Event rules?

I have a CodeBuild buildspec which has command to run based on environments (reads Env. variable) eg: Dev, Test etc. I stead of creating two different codebuilds to run this command, I want to pass Env variable value from CloudWatch rules i.e. with Constant (Json).

I tried using the following but, nothing worked:

  1. {"name":"Env-var", "value":"valueFromCWatch"}.
  2. {"name":"Env-var", "value":"valueFromCWatch", "type":"PLAINTEXT"}
  3. {"environmentVariables":[{"name":"Env-var", "value":"valueFromCWatch"}]}
  4. {"environmentVariables":[{"name":"Env-var","value":"valueFromCWatch","type":"PLAINTEXT"}]}

Eg usage in BuildSpec:

- echo "Environment variable is: " `Env-var`

Is there a different way ?

Upvotes: 1

Views: 2303

Answers (2)

shreya sood
shreya sood

Reputation: 1

I needed to add event in the code. It's more or less the same. But took me some time to figure out.

rule.addTarget(new
         cdk.aws_events_targets.CodeBuildProject(codeBuildProject, {
              event: cdk.aws_events.RuleTargetInput.fromObject({
                environmentVariablesOverride: [
                  {
                    name: 'TAG',
                    value: tag,
                    type: 'PLAINTEXT',
                  },
                 ],
              }),
             }));

Upvotes: 0

Yong Tao
Yong Tao

Reputation: 346

CodeBuild targets support all the parameters allowed by StartBuild API. You need to use environmentVariablesOverride in your JSON string.

{"environmentVariablesOverride": [ 
     { 
         "name": "Envvar",
         "value": "valueFromCWatch"
      }
 ]}

Also, avoid using '-' in the environment name.

Upvotes: 3

Related Questions