2
votes

I am in process of using setting up pipeline using codebuild and use cloudformation package and cloudformation deploy to spin up the stack which spuns lambda function. Now i know that with cloudformation deploy we cant use parameters file with --parameters-overrides and this feature request is still in open state wit AWS https://github.com/aws/aws-cli/issues/2828 . So i am trying to use a workaround using JQ which is decsribed in this link https://github.com/aws/aws-cli/issues/3274#issuecomment-529155262 like below.

PARAMETERS_FILE="parameters.json" &&  PARAMS=($(jq -r '.Parameters[] | [.ParameterKey, .ParameterValue] | "\(.[0])=\(.[1])"' ${PARAMETERS_FILE})) - aws cloudformation deploy --template-file /codebuild/output/packaged.yaml --region us-east-2 --stack-name InitialSetup --capabilities CAPABILITY_IAM  --parameter-overrides ${PARAMS[@]}

This workaround works well if tested via cli . I also tried this workaround inside a container as buildspec.yaml file creates a container in background which runs these commands , but codebuild doesnt excute the aws cloudformation deploy step and fails . I get error "aws: error: argument --parameter-overrides: expected at least one argument" . I even tried copying the two steps of workaround in shell script and then executing it but i run into error "[Container] 2020/01/21 09:19:14 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: ./test.sh. Reason: exit status 255" Can someone please guide me here .My buildspec.yaml file is as below :

''' version: 0.2

phases: install: runtime-versions: java: corretto8 commands:

- wget -O jq https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64
- chmod +x ./jq
- cp jq /usr/bin
- jq --version 

pre_build: commands: # - echo "[Pre-Build phase]

build: commands:

  - aws cloudformation package --template-file master.yaml --s3-bucket rtestbucket --output-template-file packaged.yaml
  - aws s3 cp ./packaged.yaml s3://rtestbucket/packaged.yaml
  - aws s3 cp s3://rtestbucket/packaged.yaml /codebuild/output

post_build: commands:

- PARAMETERS_FILE="parameters.json" &&  PARAMS=($(jq -r '.Parameters[] | [.ParameterKey, .ParameterValue] | "\(.[0])=\(.[1])"' ${PARAMETERS_FILE}))
- ls 
- aws cloudformation deploy --template-file /codebuild/output/packaged.yaml --region us-east-2 --stack-name InitialSetup --capabilities CAPABILITY_IAM  --parameter-overrides ${PARAMS[@]}

artifacts: type: zip files: - packaged.yaml


3
What do you expect to see from the ${PARAMS[@]} expression.Rodrigo M
Well the output would be getting the parameters in single line from parameters.json so that these can be passed to --parameter-overrides. Well this workaround is for passing a file instead of indivual key value pairs for parameters.rashmi

3 Answers

0
votes

CodeBuild buildspec commands are not running in bash shell and I think the syntax:

${PARAMS[@]}

... is bash specific.

As per the answer here: https://stackoverflow.com/a/44811491/12072431

Try to wrap your commands in a script file with a shebang specifying the shell you'd like the commands to execute with.

0
votes

The expression ${PARAMS[@]} is not returning any value, which causes the error aws: error: argument --parameter-overrides: expected at least one argument. Review the code and resolve, or remove that parameter.

0
votes

I was able to resolve this issue with executing the all the required steps in shell script and providing access to script.