Skip to content

joatmon08/manning-book

Repository files navigation

Code Examples for Infrastructure as Code, Patterns & Practices

This repository includes code examples for Infrastructure as Code, Patterns and Practices.

Note: For clarity and cost, some of the examples have been abbreviated. As a result, not all infrastructure resources will register as healthy or error free. Certain sections have READMEs to provide additional information if the code deviates from the installation and run instructions here.

Prerequisites

Installation

Install the Python requirements.

$ pip install -r requirements.txt

Run

The code listings use Python to write a Terraform JSON configuration file.

Google Cloud Platform (GCP)

The default code listings in the book use GCP because of its free tier.

  1. Create a new project in GCP. Change [PROJECT_ID] to a unique project identifier of your choice. This isolates resources from this book from other environments or projects.

    $ gcloud projects create [PROJECT_ID]
  2. Set the CLOUDSDK_CORE_PROJECT environment variable to the GCP project ID.

    $ export CLOUDSDK_CORE_PROJECT=[PROJECT_ID]
  3. Authenticate to GCP.

    gcloud auth login
  4. Change to the working directory of the code listing you want to run.

    $ cd ch02/s04
  5. Run Python.

    $ python main.py
  6. You should have a set of files with *.tf.json. Then, you can execute Terraform to initialize the plugin.

    $ terraform init
  7. Apply Terraform and make sure to enter "yes" to create the resources.

    $ terraform apply

Amazon Web Services (AWS)

To show some of the AWS equivalents, I also include a few AWS examples. These will always be located in the aws/ directory within the chapter section.

  1. Save your AWS access and secret key.

  2. Set your environment variables for the access key.

    $ export AWS_ACCESS_KEY_ID=[ACCESS_KEY_ID]
  3. Set your environment variables for the secret key.

    $ export AWS_SECRET_ACCESS_KEY=[SECRET_ACCESS_KEY]
  4. Set your environment variables for the region.

    $ export AWS_DEFAULT_REGION=[REGION]
  5. Change to the working directory of the code listing you want to run.

    $ cd ch02/s04/aws
  6. Run Python.

    $ python main.py
  7. You should have a set of files with *.tf.json. Then, you can execute Terraform to initialize the plugin.

    $ terraform init
  8. Apply Terraform and make sure to enter "yes" to create the resources.

    $ terraform apply

Removing Resources

You can delete resources by changing the working directory of the code listing and destroying resources with Terraform.

$ terraform destroy

You can always identify the resources created by this book by examining the labels (or tags for AWS). Most of the resources created by this book should have a label named purpose set to manning-infrastructure-as-code, when applicable.

Tests

The chapter on testing uses a Python framework called pytest to run the tests. Some of the will create resources in GCP, as they are integration or end-to-end tests.

  1. Change to the working directory of the code listing you want to run.

    $ cd ch05/s01
  2. Run pytest.

    $ pytest .

About

Code examples for "Infrastructure as Code, Patterns & Practices" by Rosemary Wang

Resources

License

Stars

Watchers

Forks

Languages