Getting Started with Terraform
5/5
()
About this ebook
- An up-to-date and comprehensive resource on Terraform that lets you quickly and efficiently launch your infrastructure
- Learn how to implement your infrastructure as code and make secure, effective changes to your infrastructure
- Learn to build multi-cloud fault-tolerant systems and simplify the management and orchestration of even the largest scale and most complex cloud infrastructures
This book is for developers and operators who already have some exposure to working with infrastructure but want to improve their workflow and introduce infrastructure as a code practice.
Knowledge of essential Amazon Web Services components (EC2, VPC, IAM) would help contextualize the examples provided. Basic understanding of Jenkins and Shell scripts will be helpful for the chapters on the production usage of Terraform.
Read more from Kirill Shirinkin
Getting Started with Terraform Rating: 0 out of 5 stars0 ratingsGetting Started with Terraform - Second Edition Rating: 0 out of 5 stars0 ratings
Related to Getting Started with Terraform
Related ebooks
Implementing DevOps on AWS Rating: 0 out of 5 stars0 ratingsImplementing Cloud Design Patterns for AWS Rating: 0 out of 5 stars0 ratingsMastering Cloud Development using Microsoft Azure Rating: 0 out of 5 stars0 ratingsTerraform in Action Rating: 5 out of 5 stars5/5Mastering Kubernetes Rating: 5 out of 5 stars5/5Infrastructure as Code (IAC) Cookbook Rating: 0 out of 5 stars0 ratingsDevOps. How to build pipelines with Jenkins, Docker container, AWS ECS, JDK 11, git and maven 3? Rating: 0 out of 5 stars0 ratingsGetting Started with Kubernetes - Second Edition Rating: 0 out of 5 stars0 ratingsLearn Kubernetes & Docker - .NET Core, Java, Node.JS, PHP or Python Rating: 0 out of 5 stars0 ratingsMastering Ansible Rating: 5 out of 5 stars5/5Hands-On Microservices with Kubernetes: Build, deploy, and manage scalable microservices on Kubernetes Rating: 5 out of 5 stars5/5Pipeline as Code: Continuous Delivery with Jenkins, Kubernetes, and Terraform Rating: 3 out of 5 stars3/5DevOps Bootcamp Rating: 0 out of 5 stars0 ratingsKubernetes Handbook: Non-Programmer's Guide to Deploy Applications with Kubernetes Rating: 4 out of 5 stars4/5GitOps and Kubernetes: Continuous Deployment with Argo CD, Jenkins X, and Flux Rating: 0 out of 5 stars0 ratingsLearning Ansible 2 - Second Edition Rating: 5 out of 5 stars5/5Learn Kubernetes in a Month of Lunches Rating: 0 out of 5 stars0 ratingsAWS Administration – The Definitive Guide Rating: 5 out of 5 stars5/5Mastering Ansible - Second Edition Rating: 0 out of 5 stars0 ratingsKubernetes in Action Rating: 0 out of 5 stars0 ratingsAmazon Web Services in Action Rating: 0 out of 5 stars0 ratingsDevOps for Networking Rating: 0 out of 5 stars0 ratingsAnsible Playbook Essentials Rating: 0 out of 5 stars0 ratingsLogging in Action: With Fluentd, Kubernetes and more Rating: 0 out of 5 stars0 ratingsOpenShift Cookbook Rating: 0 out of 5 stars0 ratingsBootstrapping Microservices with Docker, Kubernetes, and Terraform: A project-based guide Rating: 3 out of 5 stars3/5Learning AWS Rating: 4 out of 5 stars4/5Implementing DevOps with Microsoft Azure Rating: 0 out of 5 stars0 ratings
Computers For You
The Innovators: How a Group of Hackers, Geniuses, and Geeks Created the Digital Revolution Rating: 4 out of 5 stars4/5Elon Musk Rating: 4 out of 5 stars4/5Standard Deviations: Flawed Assumptions, Tortured Data, and Other Ways to Lie with Statistics Rating: 4 out of 5 stars4/5Mastering ChatGPT: 21 Prompts Templates for Effortless Writing Rating: 5 out of 5 stars5/5101 Awesome Builds: Minecraft® Secrets from the World's Greatest Crafters Rating: 4 out of 5 stars4/5The Invisible Rainbow: A History of Electricity and Life Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Tor and the Dark Art of Anonymity Rating: 5 out of 5 stars5/5Procreate for Beginners: Introduction to Procreate for Drawing and Illustrating on the iPad Rating: 0 out of 5 stars0 ratingsSlenderman: Online Obsession, Mental Illness, and the Violent Crime of Two Midwestern Girls Rating: 4 out of 5 stars4/5The ChatGPT Millionaire Handbook: Make Money Online With the Power of AI Technology Rating: 4 out of 5 stars4/5Alan Turing: The Enigma: The Book That Inspired the Film The Imitation Game - Updated Edition Rating: 4 out of 5 stars4/5Everybody Lies: Big Data, New Data, and What the Internet Can Tell Us About Who We Really Are Rating: 4 out of 5 stars4/5How to Create Cpn Numbers the Right way: A Step by Step Guide to Creating cpn Numbers Legally Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Deep Search: How to Explore the Internet More Effectively Rating: 5 out of 5 stars5/5CompTIA Security+ Get Certified Get Ahead: SY0-701 Study Guide Rating: 5 out of 5 stars5/5Uncanny Valley: A Memoir Rating: 4 out of 5 stars4/5CompTIA IT Fundamentals (ITF+) Study Guide: Exam FC0-U61 Rating: 0 out of 5 stars0 ratingsThe Professional Voiceover Handbook: Voiceover training, #1 Rating: 5 out of 5 stars5/5Master Builder Roblox: The Essential Guide Rating: 4 out of 5 stars4/5Dawn of the New Everything: Encounters with Reality and Virtual Reality Rating: 4 out of 5 stars4/5The Best Hacking Tricks for Beginners Rating: 4 out of 5 stars4/5
Reviews for Getting Started with Terraform
1 rating0 reviews
Book preview
Getting Started with Terraform - Kirill Shirinkin
Table of Contents
Getting Started with Terraform
Credits
About the Author
About the Reviewer
www.PacktPub.com
Why subscribe?
Customer Feedback
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Infrastructure Automation
What is Infrastructure as Code and why is it needed?
Declarative vs Procedural tools for Infrastructure as Code
Infrastructure as Code in the Cloud
Requirements for infrastructure provisioner
Supports a wide variety of services
Idempotency
Dependency resolution
Robust integration with existing tools
Platform agnosticism
Smart update management
Ease of extension
Which tools exist for infrastructure provisioning?
Scripting
Configuration management
CloudFormation/Heat
Terraform
A short overview of Terraform
Journey ahead and how to read this book
Summary
2. Deploying First Server
History of Terraform
Preparing work environment
The many Terraform providers
Short introduction to AWS
Using Elastic Compute Cloud
Creating an instance through Management Console
Creating an instance with AWS CLI
Configuring AWS provider
Static credentials
Environment variables
Credentials file
Creating EC2 instance with Terraform
Working with state
Handling resource updates
Destroying everything we've built
Summary
3. Resource Dependencies and Modules
Creating AWS Virtual Private Cloud
Understanding dependency graph
Playing with Terraform graph
Controlling dependencies with depends_on and ignore_changes
Making sense of our template
Removing duplication with modules
Configuring modules
Retrieving module data with outputs
Using root module outputs
Summary
4. Storing and Supplying Configuration
Understanding variables
Using map variables
Using list variables
Supplying variables inline
Using Terraform environment variables
Using variable files
Configuring data sources
Providing configuration with template_file
Providing data from anywhere with external_data
Exploring Terraform configuration resources
Taking a quick look at Consul
Summary
5. Connecting with Other Tools
Returning data with outputs
Testing servers with Inspec
Provisioners
Provisioning with local-exec and Ansible
Provisioning with Chef
Provisioning with remote-exec and Puppet
Uploading files with a file provisioner
Reprovisioning machines with null_resource
Using third-party plugins
Summary
6. Scaling and Updating Infrastructure
Counting servers
Bringing in high availability
Load balancing and simulating conditionals
Immutable Infrastructure
Baking images with Packer
Rolling out AMI upgrades with Terraform
Performing blue-green deployments
Refreshing infrastructure
Importing resources
Summary
7. Collaborative Infrastructure
Version control with Git 101
Moving templates to Git
Protecting secrets in a Git repository
Storing state files remotely
Connecting remote states together
Storing modules remotely
Locking state files with Terragrunt
Moving infrastructure updates to the CI pipeline
Integration testing of Terraform modules
Summary
8. Future of Terraform
Infrastructure as Code and Terraform replacements
Learning AWS and compiling Terraform
Learning Consul
Provisioning and configuration management
Immutable Infrastructure
Collaboration and CI/CD
The many tools around Terraform
The rapid development of Terraform
Closing thoughts on the future of Terraform
Summary
Getting Started with Terraform
Getting Started with Terraform
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: January 2017
Production reference: 1240117
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78646-510-8
www.packtpub.com
Credits
About the Author
Kirill Shirinkin is an IT consultant who focuses on Cloud technologies and DevOps practices. He has worked in companies of different sizes and areas, from an online language learning leader to a major IT provider for the global travel industry and one of the largest management consultancies. He is also a cofounder of online mentorship platform mkdev.me, where he leads a team and teaches his students all about DevOps.
About the Reviewer
Anton Babenko is currently working as a senior automation engineer at Stelligent Systems AB, where he specializes in infrastructure management and deployment using Amazon Web Services. He is an AWS certified professional with all five available certifications. Also, he has been working as a web developer, team lead, and chief technology officer for the last 10 years. He has been constantly involved in automation (from testing to marketing) and exploring ways to do it properly and as risk-free as possible. He has strong interest and experience in the DevOps toolset.
www.PacktPub.com
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Customer Feedback
Thank you for purchasing this Packt book. We take our commitment to improving our content and products to meet your needs seriously—that's why your feedback is so valuable. Whatever your feelings about your purchase, please consider leaving a review on this book's Amazon page. Not only will this help us, more importantly it will also help others in the community to make an informed decision about the resources that they invest in to learn.
You can also review for us on a regular basis by joining our reviewers' club. If you're interested in joining, or would like to learn more about the benefits we offer, please contact us: customerreviews@packtpub.com.
Preface
With ever-rising adoption of Cloud technologies and infrastructure SaaS products, as well as always the constantly sizes of infrastructures the need, to manage it all in the form of code becomes more and more apparent. Cloud providers such as Amazon Web Services have dozens of services and all of them require secure, re-usable and predictable configuration. Terraform, the primary tool for this job, appeared in 2014 and quickly gained popularity among system administrators and software developers. Since the first release, Terraform has achieved a lot of traction. It became the new de facto tool for managing the cloud environments. Terraform is also a tool that is quite new, that is changing with every release and that requires a new mindset and new practices from teams that adopt it.
In this book you will learn how Terraform works and how to use it, with many examples of real-life applications of it. You will explore modern approaches to managing the infrastructure, such as Infrastructure as Code and Immutable Infrastructure. You will also learn many new small utilities that either improve the experience of working with Terraform or cover the layers that Terraform is not supposed to manage. By the end of this book not only will you now how to use Terraform, but you will be in an expert in treating your whole Infrastructure as Code, with Terraform being the core of this procedure.
What this book covers
Chapter 1, Infrastructure Automation, covers infrastructure automation in general, why is it needed at all (with a list of the main reasons to do it) and which tools exist to solve this problem. By the end of this chapter you will know which problem Terraform solves and why it is the best tool for particular infrastructure automation tasks.
Chapter 2, Deploying First Server, walks through all the necessary steps to install Terraform, gives a short overview of AWS and EC2, and explain in detail how to create your very first EC2 instance with Terraform.
Chapter 3, Resource Dependencies and Modules, explains one of most important features of Terraform: dependency graph. You will figure out how dependencies work and see it in practice by extending the template from previous chapter. At the moment we find out our template is too big, we will use Terraform modules to DRY our code and also use more advanced dependency features.
Chapter 4, Storing and Supplying Configuration, teaches how to make Terraform templates more configurable. You will see all the possible ways to supply data to Terraform templates, to basic variables to using any external data source.
Chapter 5, Connecting with Other Tools, talks about how you can connect Terraform templates to external tools. It shows how to combine Terraform and Ansible, Puppet, or Chef, how to provision servers, and how to run Inspec tests against them.
Chapter 6, Scaling and Updating Infrastructure, dives deep into managing existing infrastructures with Terraform. It gives an overview of the various ways to perform updates with Terraform and explains what Immutable Infrastructure is and how to use it with Terraform. It gives a full example of performing both rolling updates and blue-green deployments, as well as tricks on running smaller updates.
Chapter 7, Collaborative Infrastructure, provides best practices of using Terraform in a team. It shows how to refactor and split Terraform templates into remote modules, how to organize your code to be re-usable, and how to handle sensitive data inside Terraform templates. It also teaches how to do full Continuous Integration of a Terraform-based infrastructure.
Chapter 8, Future of Terraform, speculates on the future of Terraform. It also recaps everything learned so far and gives some extra thoughts and hints on topics that were too small too deserve a separate chapter.
What you need for this book
This book assumes a basic level of understanding the Linux operating system. The book will go through configuring numerous AWS resources. Being familiar with AWS is a plus, but is not required, as all required services will be explained. Usage of some cloud services in this book will require you to spend a dollar or two on them. Although the book assumes Linux as the primary workstation operating system, all of the content applies to MacOS and most of it will work the same way on Windows as well.
Internet connectivity is required to install the necessary tools, including Terraform. It is also required to perform any Terraform operations.
Who this book is for
This book is essentially intended to both software developers and system administrators, as well as specialists who have knowledge of both areas: system reliability engineers, DevOps engineers, cloud architects and so on.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: For some reason, instead of using DNS server, you want to hardcode the IP address of this box to the /etc/hosts file with a domain name repository.internal.
A block of code is set as follows:
host { 'repository.internal':
ip => '192.168.0.5',
}
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
resource null_resource
app_server_provisioner
{
triggers {
server_id = ${join(
,, aws_instance.app-server.*.id)}
}
connection { user = centos
host = ${element(aws_instance.app-server.*.public_ip, count.index)}
}
provisioner file
{
source = ${path.module}/setup.pp
destination = /tmp/setup.pp
}
Any command-line input or output is written as follows:
$> curl -O https://releases.hashicorp.com/terraform/0.8.2/terraform_0.8.2_linux_amd64.zip $> sudo unzip terraform_0.8.2_linux_amd64.zip -d /usr/local/bin/
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: Click on Launch Instance.
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail feedback@packtpub.com, and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the book in the Search box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on Code Download.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Getting-Started-with-Terraform. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Downloading the color images of this book
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/GettingStartedwithTerraform_ColorImages.pdf.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help