Review Project

Let’s explore the generated app folder.

App Modules

Let’s check out the app/modules/example folder.

main.tf

resource "aws_s3_bucket" "this" {
  bucket = var.bucket # If omitted, Terraform will assign a random, unique name.
  acl    = var.acl
}

variables.tf

variable "bucket" {
  description = "The name of the bucket. If omitted, Terraform will assign a random, unique name." # IE: terraform-2020052606510241590000000
  type        = string
  default     = null
}

variable "acl" {
  description = "The canned ACL to apply. Defaults to 'private'."
  type        = string
  default     = "private"
}

You can see that the starter example module creates an s3 bucket.

Stack Modules

Next let’s look at the generated app/stacks/demo that was created:

app/stacks/demo/main.tf

resource "random_pet" "this" {
  length = 2
}

module "bucket" {
  source     = "../../modules/example"
  bucket     = "bucket-${random_pet.this.id}"
  acl        = var.acl
}

You can see that it’s just another terraform module, that will use the app/modules/example module. It uses the random_pet resource to provide a random but friendly name for the s3 bucket.

Modules vs Stacks

Both modules and stacks are terraform modules. The difference is organizational and how they are meant to be used.

  • Modules are reusable pieces of code. Generally, it contains non-business specific logic.
  • Stacks are meant to be used to group together modules. Generally, this is where business-specific logic goes.

For example, the app1 stack could be designed to use the instance module. Another, app2 stack could use the instance and rds modules.

Next, we’ll deploy the infrastructure.