- Newest
- Most votes
- Most comments
For most part, answer given above stands valid. I am adding few things here for more clarity.
For Ansible, consider utilizing Python Package Manager (pip):
-
Use pip
pip install --upgrade ansible -
Use official repositories
For Ubuntu/Debian
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible
For RHEL/CentOS
sudo yum install ansible
For Python, you can either compile from source or use Python Environment Manager (pyenv)
-
Use pyenv
curl https://pyenv.run | bash pyenv install 3.x.x pyenv global 3.x.x -
Compile from source:
wget https://www.python.org/ftp/python/3.x.x/Python-3.x.x.tgz tar xzf Python-3.x.x.tgz cd Python-3.x.x ./configure --enable-optimizations make altinstall -
For Amazon Linux 2
sudo amazon-linux-extras install python3.8
To get newer versions of Ansible and Python on your EC2 instances for Lambda compatibility, you'll need to take a different approach than using the default package managers. Here's how you can achieve this:
-
For Python: Instead of relying on yum or pip3, you should consider using a tool like pyenv to manage multiple Python versions on your EC2 instances. This allows you to install and use newer Python versions without affecting the system-wide installation.
To install pyenv:
- Install the necessary dependencies
- Download and install pyenv
- Add pyenv to your shell configuration
- Install the desired Python version (e.g., 3.12) using pyenv
-
For Ansible: Since the repositories don't have the version you need, you can install Ansible using pip after setting up the newer Python version. This ensures Ansible is compatible with the Python version you're using.
- Activate the Python environment you set up with pyenv
- Use pip to install the desired Ansible version:
pip install ansible==2.10.0(or whichever specific version you need)
For your Lambda function, you should create a layer with the appropriate Python runtime and Ansible version. Here's how:
- Create a directory for your layer (e.g.,
my-ansible-layer) - Inside this directory, create a
pythonfolder - Install Ansible and its dependencies into this folder:
pip install ansible -t ./python - Zip the contents of the
pythonfolder - Upload this zip file as a new layer in the AWS Lambda console
- Attach the layer to your Lambda function
When creating your Lambda function, make sure to select the appropriate Python runtime (e.g., Python 3.9 or 3.12) that matches the version you used to create the layer.
By following these steps, you can ensure that your Lambda function has access to the correct versions of Python and Ansible, independent of what's installed on your EC2 instances. This approach allows you to use the newer versions required for Lambda compatibility while maintaining the existing setup on your EC2 instances.
Sources
Community | Step-by-Step Guide to Creating an AWS Lambda Function Layer
Working with layers for Python Lambda functions - AWS Lambda
Relevant content
- AWS OFFICIALUpdated 3 years ago

Did it help in unblocking the issue, comment here if you have additional questions, happy to help.