AWS CLI Setup
I explain here how to interact with AWS either with the CLI (Command Line Interface) and with an IT automation tool: Ansible. Ansible is not the first tool that comes in mind for AWS (Serverless, Terraform or the built-in CloudFormation make more sense) however Ansible could be useful if you just want to configure some EC2 and specially if you have already an Ansible script somewhere around.
Prerequisites
I’m using an Anaconda as the python distribution, it’s not required but I find this distribution practical to use. I’m assuming either Anaconda or Miniconda is already installed. Please refer to [Anaconda Installation page] (https://conda.io/docs/installation.html) if it’s not the case. You will also need AWS Access and Secret Key pair. If you do not know how to get them, check this blog post.
Conda environment
Setting up a fresh conda
environment with the latest python version and giving it a meaningful name aws
.
$ conda create -n aws python=3.6.5
Activating the newly created environment
$ source activate aws
AWS CLI
Installing the AWS CLI.
$ conda install -c conda-forge awscli
It’s up!
|
Note: It’s possible to define all the environment configuration in a YAML
definition file. But here we are doing it step by step.
Configuring AWS CLI
Simple as answering to the questions.
|
Testing it with a simple command to list EC2 instances — It’s possible to use additional options like filter.
|
Ansible
Installing Ansible
|
Testing AWS connection with a simple playbook called List EC2 instances retrieving the list of t2.micro
EC2 instances already created.
|
To launch the script we have to use a little hack telling Ansible to use localhost
instead of an inventory file.
|
Here, for convenience reasons, I’ve put the keys in the playbook. However this shall not be done like that for security reason. There are several ways to manage these keys from environment variables to ansible-vault
. Check this documentation for further information on this topic.