Terraform Modules
Discover the real you
You're more awesome than you think! Take free quizzes to reveal your talents and skills. The test contains a set of questions and there is no time limit. The test is not official, it's just one of the most efficient ways to see how much you know, or don't know, about the subject.Count your score
You will get 1 point for each correct answer. At the end of the Quiz, your total score will be displayed.Reflect on you answers
Please conduct a self review of your answers once you complete the quiz test. Being an active participant in your own evaluation, honestly assess your strengths and also areas you need to improve. Good luck!--- # What of the following is not a source type for a module? 1. [ ] SSH 1. [ ] Github 1. [ ] Bitbucket 1. [ ] S3 # How do you download a module configured in your Terraform code? ```terraform module "ecs_cluster” { source = "terraform-aws-modules/ecs/aws” version = "2.8.0” // inputs } ``` 1. [ ] terraform get module ecs_cluster 1. [ ] terraform install modules ecs_cluster 1. [x] terraform init 1. [ ] terraform module init # In Terraform, What are modules used for? 1. [ ] Re-use configuration 1. [ ] Ensure best practices 1. [ ] Encapsulate configuration 1. [x] All of the options are correct # In Terraform, What are modules used for? 1. [ ] Re-use configuration 1. [ ] Ensure best practices 1. [ ] Encapsulate configuration 1. [ x] All of the options # Which among the following, is the syntax for referencing a terraform public registry module, that is integrated with terraform by default? 1. [x] `source /` > correct syntax for referencing a Terraform public registry. 1. [ ] `source = /` > because the syntax for referencing a terraform public registry is incorrect. 1. [ ] `source = ///` > Because this syntax is used to refer to a private registry 1. [ ] `source = app.terraform.io///` > because the terraform cloud private registry is referenced using this syntax. > For more information: https://www.terraform.io/docs/registry/modules/use.html#usingmodules > https://www.terraform.io/docs/registry/modules/use.html#privateregistry-module-sources # What are the benefits of using module in terraform? (Choose THREE) - [x] Module helps to organize configuration. > because modules make it easier to navigate, understand, and update your configuration by grouping together related elements. - [x] Module helps to encapsulate configuration. > because it aids in the separation of configuration into logical components. - [x] Module helps to Re-use configuration. > RIGHT CHOICE. It can be time-consuming and error-prone to write all of your configuration from scratch. - [ ] Module provides parallel execution of configuration. > because terraform provides parallel execution by default; it is not due to terraform. - [ ] Module helps to enforce authentication and to maintain private code. > because terraform modules do not have this feature. > For more information: https://learn.hashicorp.com/tutorials/terraform/module#what-aremodules-for # A user has made a reference to a child module. He wants to override one of the child module's attributes. The code for the child module is shown below. He wants to change the instance type from `m5.micro` to `m5.small`, so he passes it to the root module as an input variable. Will he succeed in doing that? ```hcl resource "aws_instance" "webapp" { ami = "ami-1abcdfghi" instance_type = "m5.micro" } ``` 1. [ ] False 1. [x] True > If a value is already set in child module, then it can be overwritten by root module. # Bob is using a module `ec2_instance` to create a server. He wants the IP address of that server, to use in the DNS module. How can this be achieved? Select one of the following options. 1. 2. [ ] export the IP address from `ec2_instance` module using environment variables > because terraform configuration does not allow us to directly export the environment values. 1. [ ] set the value of IP address generated using `TF_VAR` variables > because the `TF VAR` cannot be set via configuration. 1. [x] configure output value in `ec2_instance` module and access it in DNS module > the value is output in the ec2 instance module and referred to in the DNS module.Output values are similar to the return values of a Terraform module, and they can be used for a variety of things: A parent module can access a subset of a child module's resource attributes via outputs. After running terraform apply, a root module can use outputs to print specific values in the CLI output. Root module outputs can be accessed by other configurations via terraform remote state data when using remote state 1. [ ] Assign a static IP address and pass it to ec2_instance and DNS module. > because static IP assignment is not supported by all modules. > https://www.terraform.io/docs/configuration/outputs.html # Which among the following is not module source options? 1. [ ] Local Path 1. [ ] Terraform registry 1. [ ] Bit bucket 1. [ ] HTTP URLs 1. [x] BLOB storage > Because these are valid source options for a module, the rest of the option are incorrect. BLOB storage cannot be used as a module source. In a module block, the source argument tells Terraform where to look for the source code for the desired child module. Terraform uses this to download the source code to a directory on local disc so that it can be used by other Terraform commands during the module installation step of terraform init. The valid source options for a module are currently as follows: Local Paths, Terraform Registry, Github, Bitbucket, Generic Git, Mercurial repositories, HTTP URLs, S3 buckets, GCS buckets > https://www.terraform.io/docs/modules/sources.html # Bob (a Terraform developer) wants to make a module available on the Terraform public registry. Which of the following conditions must be met in order for it to be published? (Choose TWO) - [x] Module should be named in format terraform-- > Because public repositories must use the terraform— naming convention. - [ ] Module must be on GitHub and can be public repo or private repo > Because the public repository module should only be in the public GitHub repository. - [ ] Must be PCI-DSS compliant > Because no compliance is required to publish to the repository. - [x] To publish a module initially, at least one release tag must be present. > Because tags are used to identify module versions in the registry, so it must be present. - [ ] Only HashiCorp verified modules can be present in registry, so the submitted module should be verified before it gets published to registry. > Because while anyone can publish modules to the public repository, only verified modules are displayed during searches by default. > The following requirements must be met in order to publish a module to the registry: The module should be hosted on a public GitHub account. It should be named in format terraform-- . The module must adhere to the standard module structure. The registry uses tags to identify module versions. It should be in format x.y.z https://www.terraform.io/docs/registry/modules/publish.html#requirements # Is it mandatory to specify the module version for the public registry? (defining/releasing a module without version) 1. [ ] True 1. [x] False > for public modules, specifying the module version is optional. We recommend explicitly constraining the acceptable version numbers when using modules installed from a module registry to avoid unexpected or unwanted changes. > https://www.terraform.io/docs/language/modules/syntax.html # Using private module registry one can publish and create custom modules. 1. [ ] False 1. [x] True > Yes, one can use private registry modules provided by Terraform cloud. > Reference: https://www.terraform.io/docs/cloud/registry/index.html # You have been asked to stop using static values and make code more dynamic. How can you achieve it? Select the correct option from below. 1. [ ] Local values 1. [x] Input variables > When we compare Terraform to any other programming language, we can see that: Input Variables: Function arguments are the same as input variables. 1. [ ] Depends_on 1. [ ] Functions > https://www.terraform.io/docs/language/values/variables.html # Does using required_version >=0.12 syntax ensures Terraform is using 0.12 or higher versions? 1. [ ] False 1. [x] True > The required version syntax ensures that the infrastructure is running on the specified version or higher. > Reference: https://www.terraform.io/docs/language/settings/index.html # You have been asked to upgrade modules and plugins. Which command and flag, will you use for this purpose? 1. [x] `terraform init -upgrade` > As part of their respective installation steps, terraform init -upgrade is used to upgrade modules and plugins. 1. [ ] `terraform provider -upgrade` 1. [ ] `terraform refresh` 1. [ ] `terraform init` > https://www.terraform.io/docs/cli/commands/init.html # You have been asked to stop using static values and make code more dynamic. How can you achieve it? Select the correct option from below. 1. [ ] Local values 1. [x] Input variables > When we compare Terraform to any other programming language, we can see that: Input Variables: Function arguments are the same as input variables. 1. [ ] Depends_on 1. [ ] Functions > https://www.terraform.io/docs/language/values/variables.html # You have been asked to upgrade modules and plugins. Which command and flag, will you use for this purpose? 1. [x] `terraform init -upgrade` > As part of their respective installation steps, `terraform init -upgrade` is used to upgrade modules and plugins. 1. [ ] `terraform provider -upgrade` 1. [ ] `terraform refresh` 1. [ ] `terraform init` > https://www.terraform.io/docs/cli/commands/init.html # Terraform provider is a plugin which allows modules and multiple resources to use together 1. [ ] True 1. [x] False > Terraform provider is for API interactions and resources like AWS, GCP, Azure etc. # You're a new DevOps engineer at your company, and you notice that terraform has been in use for a while. Your company has its own module repository, and you need to add more features to the aws vpn module. You discover that your company always uses the most recent version of this module when calling it. The source code for the module can be found on GitHub. [ ] How do you add and test your changes while also ensuring that they do not break the module? 1. [x] Create a branch of this module and reference in your code this new ```terraform module "test_vpn" { source = "git@github.com:mylab/aws_vpn.git?ref=myBranch" } ``` > The best method is to create a module branch and refer to it in your test. All references associated with this module will still point to the main branch in this case. 1. [ ] Create a new tag of this module pointing to your last changes commited to master and reference it. ```terraform module "test_vpn" { source = "git@github.com:mylab/aws_vpn.git?ref=0.1" } ``` > because your changes were committed to the main branch, which is being referenced by a tag. 1. [ ] Create a branch of this module and reference in your code this new branch on the version argument in the module ```terraform module "test_vpn" { source = "git@github.com:mylab/aws_vpn.git" } ``` > Because the version must be a number tagged on code 1. [ ] None of the options are correct > References: https://www.terraform.io/language/modules/syntax#version
> https://www.terraform.io/language/modules/develop # What of the following is not a source type for a module? 1. [x] SSH > because SSH is not the source type for a module. 1. [ ] Github 1. [ ] Bitbucket 1. [ ] S3 > Reference: https://www.terraform.io/language/modules/sources # You are DevOps Consultant in an organization using a centralized modules repository. You are using the module "aws_ecs". You’ve checked that this modules has the following outputs, ```terraform output "ecs_cluster_id" { description = "ID of the ECS Cluster" value = concat(aws_ecs_cluster.this.*.id, [""])[0] } output "ecs_cluster_name" { description = "The name of the ECS cluster" value = var.name } # Your terraform scripts has the following config: module "ecs_cluster" { ource = "terraform-aws-modules/ecs/aws" version = "2.8.0" ... ``` How do you access the output "ecs_cluster_name" created? 1. [ ] module.ecs_cluster.output.ecs_cluster_name > because the keyword "output" is not required to access an output variable. 1. [ ] module.ecs_cluster.output[ecs_cluster_name] > because accessing an output variable does not require the keyword "output," and the name of the output should not be part of an index. 1. [x] module.ecs_cluster.ecs_cluster_name > Accessing the module and its output is the correct way to access output variables: module.module reference.output value 1. [ ] module.ecs_cluster[ecs_cluster_name] > because the output's name should not be included in an index. > References: https://www.terraform.io/language/modules/syntax#accessingmodule-output-values
> https://www.terraform.io/language/modules/develop/composition # How do you download a module configured in your Terraform code? ```terraform module "ecs_cluster" { source = "terraform-aws-modules/ecs/aws" version = "2.8.0" // inputs } ``` 1. [ ] terraform get module ecs_cluster 1. [ ] terraform install modules ecs_cluster 1. [x] terraform init > Terraform searches for and retrieves module blocks during terraform init. Rest of the options are not valid terraform options for initialising plugins or modules. 1. [ ] terraform module init > Reference: https://www.terraform.io/cli/commands/init # You have a module in `/home/provision/terraform/modules/my-module` on a local path. How do you make a reference in Terraform's local code? /home/provision/terraform/ is the location of your terraform local code. 1. [ ] `module "mylab" { source = "./my_module" }` > because our module is located in the "modules"directory, and we need to be one level higher. 1. [x] `module "mylab" { source = "./modules/my_module" }` > Terraform local path must o start by “.” or “./” 1. [ ] `module "mylab" { source = "my_module" }` > because there is no path specified > Reference: https://www.terraform.io/docs/language/modules/sources.html # Interact with Terraform modules You have the following code: ```terraform module "my_module" { name = var.name mark = var.mark tags = var.tags } ``` The module has an output variable called “dns_record” How can you access to this variable to be used in your terraform code? 1. [ ] `module.my_module.dns_record.output` 1. [ ] `module.my_module.output.dns_record` 1. [ ] `output.module.my_module.dns_record` 1. [x] `module.my_module.dns_record `` > module is how we get to an output variable defined in a module. MODULE NAME.OUTPUT NAME > Reference:https://learn.hashicorp.com/tutorials/terraform/module-use # What are all the arguments and meta-arguments that you can use in a module block? 1. [ ] source and version 1. [ ] source, version, providers > Because there are more arguments that can be defined, 1. [x] source, version, count, for_each, providers, and depends_on > The only one that is required is the argument source. 1. [ ] source, version, count, for_each, providers, and depend_at > Because depend at is not a meta-argument, > Reference: https://www.terraform.io/docs/language/modules/syntax.html#metaarguments # You work as a DevOps Engineer in a three-person team. You need to use EKS, and instead of writing a module from scratch, you decided to look into the Terraform Registry's public repository. How would you access the EKS Registry with your code? Module for Terraforming 1. [ ] ```terraform module "eks" { source = "hashicorp/eks/aws" version = "14.0.0" } ``` > because it does not follow the documented guidance for provisioning the module. The module would not be found when you ran terraform init. 1. [ ] module "eks" { } ```terraform module "eks" { source = "registry.terraform.io/eks/aws" version ="14.0.0" } ``` > because it does not follow the documented guidance for provisioning the module. The module would not be found when you ran terraform init. 1. [ ] ```terraform module "eks" { source = "app.terraform.io/hashicorp/eks/aws" version = "14.0.0" } ``` 1. [x] ```terraform module "eks" { source = "terraform-aws-modules/eks/aws" version = "14.0.0" } ``` > In general, you must gain access in order to review the provision instructions C is incorrect because it is attempting to access a private registry while the Query is about the public Terraform Registry. > References:
> https://registry.terraorm.io
> https://registry.terraform.io/modules/terraform-awsmodules/eks/aws/latest
> https://www.terraform.io/docs/registry/modules/use.html#usingmodules
> https://www.terraform.io/docs/registry/modules/use.html#privateregistry-module-sources # One of the main benefits of IaC is versioning. What happens if you don’t specify a version in your module block definition? 1. [x] The latest version will be used > If you don't specify a version in the block definition, the latest version of the module will be used by default. 1. [ ] An error will occur as you have to specify a version > because this argument isn't required. 1. [ ] Terraform will be prompt a message listing all the versions available where you have to type which one you want > because this does not occur in Terraform. When you run terraform init to initialise the terraform directory, the module is cloned based on the version specified (or latest) 1. [ ] Terraform will be cloned all the versions available but the latest version will be used > Terraform will not clone all versions of a particular module > References: > https://www.terraform.io/docs/language/modules/syntax.html#version > https://www.terraform.io/docs/language/modules/syntax.html#callinga-child-module
Key aspects of Terraform modules include
Module structure
Modules are structured as directories containing one or more resource configurations, variables, and outputs. The directory can also contain other resources, such as scripts or templates.
Module registry
The Terraform module registry is a public repository of reusable modules that can be used by anyone. It includes modules for popular infrastructure services, such as AWS, Azure, and Google Cloud Platform.
Private module repositories
Users can also create their own private module repositories to store and manage custom modules.
Module versioning
Modules can be versioned using tags or commit hashes to ensure consistent and predictable behavior across different projects and environments.
Module dependencies
Modules can depend on other modules, allowing users to build complex infrastructure components from smaller, reusable modules.
Module testing
Terraform modules can be tested using automated testing tools and frameworks to ensure their functionality and compatibility with different environments and infrastructure services.
Overall, Terraform modules are a powerful feature of Terraform that enable users to create reusable, shareable, and versioned infrastructure components that simplify infrastructure management, reduce errors, and increase scalability and flexibility.