How to run AWS CLI commands based on AWS CLI Version in Bash

If you find yourself having the requirements to support both AWS CLI v1 and v2 simultaneously, then you can use the following conditional in Bash to only run the specific version required:

AWS_VERSION=$(aws --version | gawk '{print $1}' | gawk -F/ '{print $2}' | gawk -F. '{print $1}') if [ AWS_VERSION == "1" ] then aws lambda invoke --function-name YourFunction --payload $(echo '{"k":"v"}') response.json else aws lambda invoke --function-name YourFunction --cli-binary-format raw-in-base64-out --payload '{"k":"v"}' response.json fi
Code language: Bash (bash)

This can be useful for deploying to environments where you cannot guarantee the AWS CLI version installed. Such as distributed pipelines, or customer environments.

Notify of
Inline Feedbacks
View all comments