Book Image

Infrastructure as Code (IAC) Cookbook

By : Stephane Jourdan, Pierre Pomès
Book Image

Infrastructure as Code (IAC) Cookbook

By: Stephane Jourdan, Pierre Pomès

Overview of this book

Para 1: Infrastructure as code is transforming the way we solve infrastructural challenges. This book will show you how to make managing servers in the cloud faster, easier and more effective than ever before. With over 90 practical recipes for success, make the very most out of IAC.
Table of Contents (18 chapters)
Infrastructure as Code (IAC) Cookbook
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Index

Using contextual defaults with Terraform


We've seen how to declare and use default values in our Terraform code, such as the Ubuntu AMI for our region or our VM size. An interesting feature in Terraform is the ability to declare and use maps of values, so, depending on a key, the variable can have a different value. We'll see how it applies to the correct AMI of the corresponding AWS.

Getting ready

To step through this recipe, you will need the following:

  • A working Terraform installation

  • An AWS provider and an EC2 instance (using a SSH key pair and a security group), all configured in Terraform (refer to the previous recipes)

  • An Internet connection

How to do it…

Here's how we simply declared the AMI we wanted for the eu-west-1 region in the variables.tf file:

variable "ami" {
  default = "ami-ee6b189d"
}

We accessed it easily like this in the instances.tf file:

ami = "${var.ami}"

A similar, but more explicit way would be to use a map, so we know which region the value refers to:

variable "ami" {
  default...