amazon-web-servicesaws-codebuildamazon-cloudwatch

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 ?


Solution

  • 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.