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.
- Sign up for a Google Cloud Platform (GCP) account and project. Check out the tutorial at https://console.cloud.google.com/getting-started/checklist.
- Install the gcloud command line tool. This allows you to use the terminal to authenticate and issue commands to GCP.
- Install Python 3.9.6.
- I use pyenv and pyenv-virtualenv to manage my Python versions.
- Install Terraform 1.0 or higher.
Install the Python requirements.
$ pip install -r requirements.txt
The code listings use Python to write a Terraform JSON configuration file.
The default code listings in the book use GCP because of its free tier.
-
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]
-
Set the
CLOUDSDK_CORE_PROJECT
environment variable to the GCP project ID.$ export CLOUDSDK_CORE_PROJECT=[PROJECT_ID]
-
Authenticate to GCP.
gcloud auth login
-
Change to the working directory of the code listing you want to run.
$ cd ch02/s04
-
Run Python.
$ python main.py
-
You should have a set of files with
*.tf.json
. Then, you can execute Terraform to initialize the plugin.$ terraform init
-
Apply Terraform and make sure to enter "yes" to create the resources.
$ terraform apply
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.
-
Save your AWS access and secret key.
-
Set your environment variables for the access key.
$ export AWS_ACCESS_KEY_ID=[ACCESS_KEY_ID]
-
Set your environment variables for the secret key.
$ export AWS_SECRET_ACCESS_KEY=[SECRET_ACCESS_KEY]
-
Set your environment variables for the region.
$ export AWS_DEFAULT_REGION=[REGION]
-
Change to the working directory of the code listing you want to run.
$ cd ch02/s04/aws
-
Run Python.
$ python main.py
-
You should have a set of files with
*.tf.json
. Then, you can execute Terraform to initialize the plugin.$ terraform init
-
Apply Terraform and make sure to enter "yes" to create the resources.
$ terraform apply
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.
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.
-
Change to the working directory of the code listing you want to run.
$ cd ch05/s01
-
Run
pytest
.$ pytest .