Skip to content

This is an informational repo with a quick code analysis to introduce you to some basic Terraform code etiquette and resource blocks.

Notifications You must be signed in to change notification settings

tkerbe2/iac-intro-exercise-1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 

Repository files navigation

tkdev_space_200


🔬 Exercise 1 - Basic Terraform Code Concepts and Analysis

In this lab we will quickly cover some basic Terraform concepts and analyze some code. No resources will be created but after this lab you will be ready for your first challenge.


💡 Terms and Concepts

Files and Structure

In this repo you will notice a bunch of files with the extension of .tf. The .tf file is the Terraform file extension. Any file with this extension will be seen as a configuration file written in HashiCorp Configuration Language. You can generally name TF files what you would like, however there are some best practices for common files with common configuration. The following file names are from the HashiCorp Style Guide. By following these standards your code will be easier for others to read and consume.

  • A backend.tf file that contains your backend configuration. You can define multiple terraform blocks in your configuration to separate your backend configuration from your Terraform and provider versioning configuration.
  • A main.tf file that contains all resource and data source blocks.
  • A outputs.tf file that contains all output blocks in alphabetical order.
  • A providers.tf file that contains all provider blocks and configuration.
  • A terraform.tf file that contains a single terraform block which defines your required_version and required_providers.
  • A variables.tf file that contains all variable blocks in alphabetical order.
  • A locals.tf file that contains local values. Refer to local values for more information.
  • A override.tf file that contains override definitions for your configuration. Terraform loads this and all files ending with _override.tf last. Use them sparingly and add comments to the original resource definitions, as these overrides make your code harder to - > reason about. Refer to the override files documentation for more information.

HashiCorp. (n.d.). Style Guide.


Resource Blocks and Arguments

Terraform is mostly a large combination of resource blocks. These resource blocks define a specific resource type with a name that is only referenced in Terraform (local name). See the example below for a breakdown. Terraform arguments are specific settings that can be used to configure a resource a specific type of way.

AWS S3 Bucket Code Example

resource "aws_s3_bucket" "my_bucket" {
  bucket = "tkdev-test-bucket"

  tags = {
    Name        = "tkdev-test-bucket"
    Environment = "Dev"
  }
}

  • On the first line you will see the resource, followed by the type as aws_s3_bucket. This is our resource type and the "my_bucket" is the local name.
  • Next you have the bucket which is an optional argument. This means that if you don't supply a value or the argument is not present, the argument has a default value or action.
  • Lastly, tags are optional and in AWS they're just a way to label and organize resources. You can supply values or leave them blank.

To see all the different ways you can configure a specific resource block you will use the Terraform Registry. You can use this registry to find all resource types and how they can be configured and defined with Terraform.

✍️ AWS VPC Code Exercise

resource "aws_vpc" "main_vpc" {
  cidr_block       = "192.168.0.1/24"
  instance_tenancy = "default"
  region           = "us-east-1"

  tags = {
    Name        = "tkdev_vpc"
    Environment = "Prod"
    Department  = "Development"
  }
}

For this code block above I want you to answer the following questions:

  1. Which arguments are required?
  2. Which arguments are optional?
  3. What is the resource local name?

Use the registry and search vpc to find the aws_vpc resource page.


🗹 Answers

(Check here if you're stuck or ready to compare) Remeber to use the registry.


Question 1.

None, actually
Question 2.
All of the arguments are optional
Question 3.
main_vpc

📖 Suggested Reading


✨ Congratulations!


You've finished this lab and have completed the following items:

  • ✅ Learned about Terraform files.
  • ✅ Learned the basics about resource blocks.
  • ✅ Learned the basics about arguments.
  • ✅ Analyzed some Terraform code.

About

This is an informational repo with a quick code analysis to introduce you to some basic Terraform code etiquette and resource blocks.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages