How to Automate Anaconda Installation on AWS EC2: Complete Guide
By Braincuber Team
Published on April 27, 2026
Amazon uses Python 2 on their standard Amazon Linux 2 EC2 instances by default. For projects requiring Python3, Anaconda is the solution, but automating its installation with AWS CloudFormation takes some know-how. This complete tutorial shows you how to automate Anaconda installation and configure AWS CLI on new EC2 instances using CloudFormation UserData.
What You Will Learn:
- How CloudFormation UserData works for automated installations
- The bash script to install Anaconda silently
- How to install AWS CLI and Boto3 automatically
- Deploy a complete stack with one click
- Verify the installation after launch
The Problem With Amazon Linux and Python
Amazon insists on using Python 2 on their standard Amazon Linux 2 EC2 instances. On projects requiring Python3, you need a solution. Virtual environments help, but you also need Anaconda and the AWS CLI installed automatically when the instance launches.
AWS provides a UserData key when provisioning EC2 instances that can run user-defined bash commands. This enables full automation of your development environment.
Time Investment
Stack creation can take 10 minutes or more - Anaconda is a hefty install. Plan accordingly.
The Bash Script for Automated Installation
Before creating your CloudFormation stack, you need a bash script that handles the complete installation. Here is the script that automates everything:
#!/bin/bash
yum update -y
yum install wget -y
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh
bash ~/miniconda.sh -b -p ~/miniconda
rm ~/miniconda.sh
export PATH=~/miniconda/bin:$PATH
conda install -y python=3
pip install --upgrade pip
pip install boto3
pip install awscli
echo 'export PATH=~/miniconda/bin:$PATH' >> ~/.bash_profile
Step by Step: Deploy with CloudFormation
Get the CloudFormation Template
Obtain the CloudFormation template from the GitHub repository. Open the template in the AWS CloudFormation Designer to visualize and modify it as needed.
Create the Stack
Click create stack in the upper left corner. Input your AWS API key, secret key, SSH key name, and other required parameters.
Wait for Stack Creation
You will receive a CREATE_IN_PROGRESS message. Wait for CREATE_COMPLETE. This can take 10 minutes or more as Anaconda is a large installation.
SSH and Verify Installation
Using your SSH client, log into the new instance. Verify Python3 with conda activate python3. Verify AWS CLI with cd ~/.awssl.
CloudFormation Template Overview
The CloudFormation template handles multiple tasks through the UserData property. Here is what gets configured automatically:
| Component | Description |
|---|---|
| OS Update | yum update -y |
| Wget Install | Download utility for Miniconda |
| Miniconda Download | Latest Miniconda3 for Linux x86_64 |
| Silent Install | bash miniconda.sh -b -p |
| Python3 | conda install python=3 |
| AWS CLI | pip install awscli |
| Boto3 | pip install boto3 |
Verification Commands
After your instance is ready, use these commands to verify the installation was successful:
# Verify Anaconda/Python3 activation
conda activate python3
# Verify AWS CLI installed
aws --version
# Check AWS credentials file exists
cd ~/.aws
ls -la
# Should see credentials file
Common Issues and Solutions
Stack Creation Timeout
If the stack fails, check the CloudFormation events. Anaconda installation takes 10+ minutes. Increase timeout or check network connectivity.
Python2 Still Active
Use conda activate python3 explicitly. The bash script sets PATH but you may need to source .bash_profile.
AWS CLI Not Found
Verify pip installed correctly. Check ~/miniconda/bin is in your PATH environment variable.
Credentials Missing
When passing AWS keys via CloudFormation parameters, ensure they are properly configured in the UserData section of the template.
Frequently Asked Questions
Can I use full Anaconda instead of Miniconda?
Yes, replace the Miniconda download URL with the full Anaconda installer URL. Be aware this significantly increases deployment time.
How long does stack creation take?
Stack creation typically takes 10 minutes or more due to the Anaconda download and installation size. Plan your deployment accordingly.
Do I need to pass AWS credentials?
Yes, pass AWS_ACCESS_KEY and AWS_SECRET_KEY as parameters to the CloudFormation template. These are used to configure the AWS CLI automatically.
Can I install specific Python packages?
Yes, add additional pip install commands to the bash script before the final line. You can install any packages available via pip or conda.
What if I want to use conda environments?
Once Anaconda is installed, use conda create -n myenv python=3 to create new environments. Activate with conda activate myenv.
Need Help with AWS EC2 Automation?
Our experts can help you set up automated deployments, configure CloudFormation templates, and optimize your AWS infrastructure.
