Reader small image

You're reading from  Infrastructure as Code (IAC) Cookbook

Product typeBook
Published inFeb 2017
PublisherPackt
ISBN-139781786464910
Edition1st Edition
Right arrow
Authors (2):
Stephane Jourdan
Stephane Jourdan
author image
Stephane Jourdan

Stephane Jourdan is a passionate infrastructure engineer, enthusiastic entrepreneur, zealous trainer, and continuous learner, working on innovative infrastructures since the early 2000s. He focuses equally on tools and culture, in environments as different as startups, online audio/video media, e-commerce, and semi-conductors. The common point between all these experiences is that success comes with rigor, technical repeatability, communication, and a shared team culture. He co-founded an infrastructure automation consultancy (https://www.linkedin.com/company/green-alto), a web radio (http://phauneradio.com/), a container/serverless platform for developers (https://www.squarescale.com/), and a sound design studio (http://www.tarabust.com/). When Stephane isn't starting or contributing to new open source projects, he's usually found hiking in remote places with his camera.
Read more about Stephane Jourdan

Pierre Pomès
Pierre Pomès
author image
Pierre Pomès

Pierre Pomès is a senior enthusiastic engineer of open source technologies and a Linux adept since 1994. He has been working in the IT industry for the last twenty years mostly in C development, system administration, and security including PCI-DSS. He is currently an architect and a DevOps team leader for Reservit, an online hotel booking engine. He has also contributed to the pfSense project.
Read more about Pierre Pomès

View More author details
Right arrow

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...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Infrastructure as Code (IAC) Cookbook
Published in: Feb 2017Publisher: PacktISBN-13: 9781786464910

Authors (2)

author image
Stephane Jourdan

Stephane Jourdan is a passionate infrastructure engineer, enthusiastic entrepreneur, zealous trainer, and continuous learner, working on innovative infrastructures since the early 2000s. He focuses equally on tools and culture, in environments as different as startups, online audio/video media, e-commerce, and semi-conductors. The common point between all these experiences is that success comes with rigor, technical repeatability, communication, and a shared team culture. He co-founded an infrastructure automation consultancy (https://www.linkedin.com/company/green-alto), a web radio (http://phauneradio.com/), a container/serverless platform for developers (https://www.squarescale.com/), and a sound design studio (http://www.tarabust.com/). When Stephane isn't starting or contributing to new open source projects, he's usually found hiking in remote places with his camera.
Read more about Stephane Jourdan

author image
Pierre Pomès

Pierre Pomès is a senior enthusiastic engineer of open source technologies and a Linux adept since 1994. He has been working in the IT industry for the last twenty years mostly in C development, system administration, and security including PCI-DSS. He is currently an architect and a DevOps team leader for Reservit, an online hotel booking engine. He has also contributed to the pfSense project.
Read more about Pierre Pomès