Issue with running AWS CLI commands

0

I am trying to run a shell script (myscript.sh) via Jenkins pipeline and Ansible. The script has 2 AWS cli commands. I was able to run the file directly on the machine like sh myscript.sh It throws an error when I run via pipeline (aws command not found). I also tried adding /usr/local/bin/aws in the script. It now throws import awscli.clidriver: no module name awscli.clidriver. I am not sure how to fix this issue. Thanks.

asked 9 months ago1530 views
1 Answer
0
Accepted Answer

It seems like there are two issues you're facing: The environment from which Jenkins is running the shell script can't find the AWS CLI (hence "aws: command not found"). There's a Python issue regarding awscli.clidriver.

Here are the steps to resolve these issues:

Solution 1: Correct Path

The AWS CLI might be installed in a location that's not in your PATH. Jenkins might not be using the same environment variables as your user, so it doesn't know where to find the AWS CLI.

To resolve this, you can specify the full path to the AWS CLI executable in your script. But before you can do this, you must find the correct path where AWS CLI is installed.

To find the path, you can use the command: which aws

This command will return the full path of AWS CLI. Once you have this, replace "aws" in your script with the full path.

Solution 2: Jenkins User Permissions

If AWS CLI is installed for a specific user, ensure the Jenkins job is running as that user, or install AWS CLI as a root user or Jenkins user, depending on your security considerations.

You can use the following commands to install AWS CLI for all users:

curl "https://d1vvhvl2y92vvt.cloudfront.net/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

Solution 3: Python Environment

Your second error indicates a Python environment problem. The AWS CLI is written in Python, so if there's an issue with your Python installation or environment, it could cause issues with the AWS CLI.

If the AWS CLI was installed with pip, try reinstalling it using pip: pip install awscli --upgrade --user

If you have multiple versions of Python installed, make sure you're using the correct version of pip that matches the Python version the AWS CLI is using. You can check the Python version with: python --version and the pip version with: pip --version

Also, make sure that your Python's scripts path is in your PATH environment variable.

Solution 4: Ansible AWS Modules

Instead of using AWS CLI commands in a shell script, consider using Ansible's built-in AWS modules, which are designed for managing AWS resources. This will handle AWS API calls for you, and you don't have to worry about managing AWS credentials or the AWS CLI. You can refer to the (Ansible AWS Guide)[https://docs.ansible.com/ansible/latest/scenario_guides/guide_aws.html] for more information.

Remember that these fixes address the issues separately. If your issue is caused by a combination of them, you may need to apply multiple fixes.

profile picture
answered 9 months ago
profile pictureAWS
EXPERT
reviewed 9 months ago
  • I was not able to reinstall using pip install awscli --upgrade --user (ConnectTimeoutError)

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions