Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
Infrastructure as Code, Patterns and Practices: With examples in Python and Terraform (Final Release)
Infrastructure as Code, Patterns and Practices: With examples in Python and Terraform (Final Release)
Infrastructure as Code, Patterns and Practices teaches patterns for scaling systems and supporting infrastructure for mission-critical applications.
Infrastructure as Code, Patterns and Practices teaches patterns for scaling systems and supporting infrastructure for mission-critical applications. The book is full of flexible automation techniques and universal principles that are easy to apply to almost any use case, from data centers, to public cloud, to Software-as-a-Service.
Infrastructure as Code, Patterns and Practices teaches you how to automate software infrastructure by capturing your desired configurations as a set of scripts. You’ll learn how to create, test, and deploy infrastructure components in a way that’s easy to scale and share across an entire organization. While the patterns and techniques are tool agnostic, you’ll appreciate the easy-to-follow examples in Python and Terraform.
Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.
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
- 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.
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.
-
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
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.
-
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
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.
-
Change to the working directory of the code listing you want to run.
$ cd ch05/s01
-
Run
pytest
.$ pytest .