Running AWS Command Line Interface using Jenkins installed through Docker: command not found?

Viewed 4862

When running aws from Jenkins pipeline I have the following error message: command not found - which aws returns command not found.

By other hand, when running aws from a single job it works - which aws returns /usr/local/bin/aws.

Do you have any idea why this is happening?

Thank you.

3 Answers

You still need to install the AWS CLI inside the docker container.

 # Swap to root user to install pip and aws cli then go back to jenkins user
USER root
RUN apt-get update
RUN apt install python3-pip -y
RUN pip3 install awscli --upgrade
USER jenkins

In my case the problem simply was that I was running the Jenkins in a docker container while my AWS-CLI was installed on the host. The temporal workaround was I logged in the Jenkins container through "docker exec" and installed the AWS-CLI in that container too. Probably if my container restarts I have to install the AWS-CLI again. Better way is to create a docker image which contains the AWS-CLI beside Jenkins or using an AWS-CLI jenkins agent for running those steps/jobs.

Solution 1: Install aws-cli in Jenkins container using a Dockerfile and use the credentials already stored on your host.

Working example:

Dockerfile:

FROM jenkins/jenkins:lts
USER root
# Set timezone
ENV TZ="UTC"

RUN apt-get -y update --fix-missing

# Install useful tools
RUN apt-get -y install nano wget ntp  git curl  libcurl4 libcurl4-openssl-dev zip unzip jq

# Install AWS Cli:
#   https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
RUN unzip awscliv2.zip
RUN ./aws/install

docker-compose.yml:

Version: '3.3'
services:
  jenkins:
    container_name: jenkins
    build:
      context: ./
        dockerfile: Dockerfile
    hostname: jenkinslocal
    user: root
    ports:
      - 8500:8080
    volumes: 
      # local persistent storage for docker to keep plugins, jobs, workspaces, etc  
       - ./jenkins-data:/var/jenkins_home

       # mount the source file for project for easy access in pipeline
       # and in project pipeline -> configure SCM -> GIT url can be: 
       #      file:///projects/project1
       - /Users/bogdan/docker/project1:/projects/project1

       # make docker available in container
       - /var/run/docker.sock:/var/run/docker.sock
       - /usr/bin/docker:/usr/bin/docker

       #provide AWS CLI credentials from host
       - /Users/bogdan/.aws:/root/.aws

Solution 2: Use docker image with aws-cli provided by aws with credentials from host. In this case, if you don't have other tools to install, you don't need a Dockerfile, just the docker-compose.yml file

Working example:

Version: '3.3'
services:
  jenkins:
    container_name: jenkins
    # image from docker hub
    image: jenkins/jenkins:lts 
    hostname: jenkinslocal
    user: root
    ports:
      - 8500:8080
    volumes: 
      # local persistent storage for docker to keep plugins, jobs, workspaces, etc  
       - ./jenkins-data:/var/jenkins_home

       # mount the source file for project for easy access in pipeline
       # and in project pipeline -> configure SCM -> GIT url can be: 
       #      file:///projects/project1
       - /Users/bogdan/docker/project1:/projects/project1

       # make docker available in container
       - /var/run/docker.sock:/var/run/docker.sock
       - /usr/bin/docker:/usr/bin/docker

Documentation can be found here: https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-docker.html

After Jenkins is up and running you can login to Jenkins container:

docker exec -it jenkins bash

and run AWS-CLI like this:

docker run --rm -it -v ~/.aws:/root/.aws amazon/aws-cli --version

Using this command in Jenkins pipeline will be difficult and the script will not run on a real production server, so you need to create an alias to use the command "aws" :

alias aws='docker run --rm -it -v ~/.aws:/root/.aws -v amazon/aws-cli'

now you can test:

aws --version

For avoid login to Jenkins container you can add the command to docker-compose.yml file:

command: sh -c "alias aws='docker run --rm -it -v ~/.aws:/root/.aws -v amazon/aws-cli'" 

Important!

Solution 2 can be difficult to use in some situations because aliases are not available, by default, in bash scripts.

Related