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:
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:
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:
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:
Install boto
#
Some Ansible
modules depend on the boto
package. You can install it by using the command:
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:
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.