The following is a snippet from a Terraform configuration file: Which, when validated, results in the following error: Fill in the blank in the error message with the correct string from the list below.
Eric needs to make use of module within his terraform code. Should the module always be public and open-source to be able to be used?
Correct Answer: A
Terraform module need not be public and open-source. Module can be placed in - * Local paths * Terraform Registry * GitHub * Bitbucket * Generic Git, Mercurial repositories * HTTP URLs * S3 buckets * GCS buckets https://www.terraform.io/docs/modules/sources.html
Question 28
You have created an AWS EC2 instance of type t2.micro through your terraform configuration file ec2.tf . Now you want to change the instance type from t2.micro to t2.medium. Accordingly you have changed your configuration file and and ran terraform plan. After running terraform plan you check the output and saw one instance will be updated from t2.micro --> t2.medium. After this you went to grab a coffee without running terraform apply and meanwhile a member of your team changed the instance type of that EC2 instance to t2.medium from aws console. After coming to your desk you run terraform apply. What will happen?
Correct Answer: A
Question 29
You have declared a variable name my_var in terraform configuration without a value associated with it. variable my_var {} After running terraform plan it will show an error as variable is not defined.
Correct Answer: B
Input variables are usually defined by stating a name, type and a default value. However, the type and default values are not strictly necessary. Terraform can deduct the type of the variable from the default or input value. Variables can be predetermined in a file or included in the command-line options. As such, the simplest variable is just a name while the type and value are selected based on the input. variable "variable_name" {} terraform apply -var variable_name="value" The input variables, like the one above, use a couple of different types: strings, lists, maps, and boolean. Here are some examples of how each type are defined and used. String Strings mark a single value per structure and are commonly used to simplify and make complicated values more user-friendly. Below is an example of a string variable definition. variable "template" { type = string default = "01000000-0000-4000-8000-000030080200" } A string variable can then be used in resource plans. Surrounded by double quotes, string variables are a simple substitution such as the example underneath. storage = var.template List Another type of Terraform variables lists. They work much like a numbered catalogue of values. Each value can be called by their corresponding index in the list. Here is an example of a list variable definition. variable "users" { type = list default = ["root", "user1", "user2"] } Lists can be used in the resource plans similarly to strings, but you'll also need to denote the index of the value you are looking for. username = var.users[0] Map Maps are a collection of string keys and string values. These can be useful for selecting values based on predefined parameters such as the server configuration by the monthly price. variable "plans" { type = map default = { "5USD" = "1xCPU-1GB" "10USD" = "1xCPU-2GB" "20USD" = "2xCPU-4GB" } } You can access the right value by using the matching key. For example, the variable below would set the plan to "1xCPU-1GB". plan = var.plans["5USD"] The values matching to their keys can also be used to look up information in other maps. For example, underneath is a shortlist of plans and their corresponding storage sizes. variable "storage_sizes" { type = map default = { "1xCPU-1GB" = "25" "1xCPU-2GB" = "50" "2xCPU-4GB" = "80" } } These can then be used to find the right storage size based on the monthly price as defined in the previous example. size = lookup(var.storage_sizes, var.plans["5USD"]) Boolean The last of the available variable type is boolean. They give the option to employ simple true or false values. For example, you might wish to have a variable that decides when to generate the root user password on a new deployment. variable "set_password" { default = false } The above example boolean can be used similarly to a string variable by simply marking down the correct variable. create_password = var.set_password By default, the value is set to false in this example. However, you can overwrite the variable at deployment by assigning a different value in a command-line variable. terraform apply -var set_password="true"
Question 30
Dawn has created the below child module. Without changing the module, can she override the instance_type from t2.micro to t2.large form her code while calling this module? 1. resource "aws_instance" "myec2" 2. { 3. ami = "ami-082b5a644766e0e6f" 4. instance_type = "t2.micro 5. }
Correct Answer: B
As the instance_type is hard-coded in source module, you will not be able to change its value from destination module. Instead of hard-coding you should use variable with default values.