Connect to AWS
Connect your Ansible Container with Amazon Web Services.
You can connect to AWS by performing the following steps:
- Create Environment Variables.
- Get AWS Caller Information.
- Install boto and boto3.
Prerequisites
Before you connect to AWS
, you’ll need the following:
- Active
AWS
account - Access Key
- Secret Access Key for an existing
IAM user
in theAWS
account
If you do not have the account and keys setup, you can do so by following the links below:
Admin Policy
The user must have theAdministratorAccess
orSystemAdministrator
policy assigned to it.
Create environment variables
Ansible uses AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
to authenticate to AWS.
Once you have acquired the keys, you can create the environment variable with the export
command within the container.
Docker Setup
We have pre-configured the environment to save you the hassle of rebuilding and re-running your containers. All the commands will work on your local setup as well.
Run the following commands in the Ansible
environment:
# replace <Access Key> with your access key Idexport AWS_ACCESS_KEY_ID='<Access Key>'# replace <Secret Access Key> with your secret access keyexport AWS_SECRET_ACCESS_KEY='<Secret Access Key>'
Create AWS caller information
You can verify that connection to AWS is established by gathering the caller information using the Ansible module aws_caller_info
.
Run the following command in the Ansible
environment:
ansible localhost -m aws_caller_info
Running the aws_caller_info
again will return the JSON
object back.
Practice all the commands one by one in the Ansible
environment. We can skip the first two commands by setting the environment variables in the terminal. You can find the summarized view of all the commands below:
# replace <Access Key> with your access key Idexport AWS_ACCESS_KEY_ID='<Access Key>'# replace <Secret Access Key> with your secret access keyexport AWS_SECRET_ACCESS_KEY='<Secret Access Key>'# Verify that the connection to AWS is establishedansible localhost -m aws_caller_info# In case of error,# "_Failed to import the required Python library (botocore or boto3)"pip3 install boto3pip3 install boto# re-executeansible localhost -m aws_caller_info
Troubleshooting tips
Missing
boto3
package
You might come across the following message.
“Failed to import the required Python library (botocore or boto3) on 022cdb1cb53c Python /usr/bin/python2. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible uses the wrong Python interpreter, please consult the documentation on ansible_python_interpreter.”
Install boto3
The error
states that it “failed to import the library botocore or boto3”. You can resolve this issue by installing the package boto3
. Run the following command in the Ansible
environment:
pip3 install boto3
Install boto
Some Ansible
modules depend on the boto
package. You can install it by using the command:
pip3 install boto
Update Dockerfile
You made some changes inside your container. If that container is deleted, all the changes will be lost. You can save your changes by updating the Dockerfile and rebuilding the image.
Review the code’s highlighted lines for the newly introduced changes, i.e., line 9-10. You can review the updated Dockerfile
below:
FROM ubuntu:latestRUN apt-get update; \apt install -y openssh-client; \apt install -y python3-pipRUN pip3 install --upgrade pip; \pip3 install "ansible==2.9.12"; \pip3 install boto; \pip3 install boto3
In this lesson, we covered how to export your access keys for AWS
, use the aws_caller_info
to verify the connection, and install missing packages.
Get hands-on with 1300+ tech skills courses.