Learn how to automate Google Cloud with Ansible and Qwiklabs

Qwiklabs has some pretty cool Google Cloud Platform (GCP) training that I’ve been playing with this weekend.

I thought it might be fun to try and automate the lessons with Ansible’s GCE modules.

The playbooks, and steps to recreate their execution environment, are on my GitHub.

I wasn’t able to automate 100% of the workshops. The Ansible GCE module is still in beta, and I’m new at GCP in general, and may have missed some things. For example, I couldn’t find any way at all to programmatically interact with Google Cloud Launcher with the SDK or API. But like I said, maybe I just missed it.

If you’re like me, and you know AWS better than GCP, here’s a nice Rosetta stone comparing services.

There are a number of open Ansible issues related to GCE, many of which look like great places to jump in and be an Ansible contributor.

Still, lots of fun!

Save your AWS budget with Python and boto

My team and I lean heavily on AWS services for prototypes, demos, and training. The challenge that we’ve encountered is that it’s easy to forget about the resources you’ve spun up. So I wrote a quickly little utility that shuts down unnecessary EC2 instances at night.

The Python library, boto, provides an AWS SDK. It’s very easy to use, and many good tutorials exist. Instructions can be found in the README, but here’s a quick overview.

First we import the boto and yaml libraries. (We’re using YAML for our config file markup. ) Then we read in that config file.

import boto.ec2
import yaml

config_file = '/etc/nightly_shutdown.yml'
with open(config_file) as f:
    config = yaml.safe_load(f)

In that config file, we’ve got our region, access and secret keys, and a white list of instance IDs we’d like to opt-out of the nightly shutdown. This last bit is important if you have instances doing long-running jobs like repo syncing, for example.

region: us-east-1
access_key: eggseggseggseggs
secret_key: spamspamspamspam
  - i-abcdefgh
  - i-ijklmnop

Now we connect to the AWS API and get a list of reservations. This itself is interesting, as it gives us a little insight into the guts of EC2. As I understand it, a reservation must exist before an instance can be launched.

conn = boto.ec2.connect_to_region(config['region'],

reservations = conn.get_all_reservations()

Now it’s simply a matter of iterating over those reservations, getting the instance IDs, and filtering out the white-listed IDs.

running_instances = []
for r in reservations:
    for i in r.instances:
        if i.state == "running":
            if i.id not in config['whitelist']:

Finally, we make the API call to stop the instances. Before doing so, we check to be sure there are any running, as this call will throw an exception if the instance ID list is empty.

if len(running_instances) > 0:

Now you just have to add this to your daily cronjobs and you’ll save a little budget.