简体   繁体   中英

Using CloudWatch Event : How to Pass JSON Object to CodeBuild as an Environment Variable

Summary: I can't specify a JSON object using CloudWatch target Input Transformer, in order to pass the object contents as an environment variable to a CodeBuild project.

Background: I trigger an AWS CodeBuild job when an S3 bucket receives any new object. I have enabled CloudTrail for S3 operations so that I can use a CloudWatch rule that has my S3 bucket as an Event Source, with the CodeBuild project as a Target.

If I setup the 'Configure input' part of the Target, using Input Transformer, I can get single 'primitive' values from the event using the format below:

Input path textbox:

{"zip_file":"$.detail.requestParameters.key"}

Input template textbox:

{"environmentVariablesOverride": [ {"name":"ZIP_FILE", "value":<zip_file>}]}

And this works fine if I use 'simple' single strings.

However, for example, if I wish to obtain the entire 'resources' key, which is a JSON object, I need to have knowledge of each of the keys within, and the object structure, and manually recreate the structure for each key/value pair.

For example, the resources element in the Event is:

"resources": [
            {
                "type": "AWS::S3::Object",
                "ARN": "arn:aws:s3:::mybucket/myfile.zip"
            },
            {
                "accountId": "1122334455667799",
                "type": "AWS::S3::Bucket",
                "ARN": "arn:aws:s3:::mybucket"
            }
        ],

I want the code in the buildspec in CodeBuild to do the heavy lifting and parse the JSON data.

If I specify in the input path textbox:

 {"zip_file":"$.detail.resources"}

Then CodeBuild project never gets triggered.

Is there a way to get the entire JSON object, identified by a specific key, as an environment variable?

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

{"environmentVariablesOverride": [ {"name":"ZIPFILE", "value":<zip_file>}]}

Please,avoid using '_' in the environment name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM