Skip to content

Terraform Infrastructure

Terraform is an open-source infrastructure as code software tool that enables you to safely and predictably create, change, and improve infrastructure.

The Terraform infrastructure generator creates a Terraform infrastructure project. The generated application includes security best practices through Checkov security checks.

You can generate a new Terraform project in two ways:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - terraform#project
  5. Fill in the required parameters
    • name: tf-infra
  6. Click Generate
Parameter Type Default Description
name Required string - The name of the project.
type string application Whether this is a terraform lib (re-usable modules) or app (deployable).
directory string packages The directory of the new project.

The generator creates different file structures depending on the project type:

For application projects (--type=application), the generator creates a complete Terraform application with remote state management:

  • Directorysrc
    • main.tf Main Terraform configuration file
    • providers.tf Provider configuration with S3 backend
    • variables.tf Input variable definitions
    • outputs.tf Output value definitions
    • Directoryenv Environment-specific variable files
      • dev.tfvars Development environment variables
  • Directorybootstrap Bootstrap configuration for remote state
    • main.tf S3 bucket and policies for state storage
    • providers.tf AWS provider configuration
    • variables.tf Bootstrap variable definitions
  • project.json Project configuration and build targets

For library projects (--type=library), the generator creates a simpler structure for reusable Terraform modules:

  • Directorysrc
    • main.tf Main Terraform module file
  • project.json Project configuration and build targets

Implementing your Terraform Infrastructure

Section titled “Implementing your Terraform Infrastructure”

You can start writing your Terraform infrastructure inside src/main.tf, for example:

src/main.tf
locals {
account_id = data.aws_caller_identity.current.account_id
aws_region = data.aws_region.current.id
}
resource "null_resource" "print_info" {
# triggers = {
# always_run = timestamp()
# }
provisioner "local-exec" {
command = "echo 'AWS Region: ${local.aws_region}, AWS Account: ${local.account_id}, Environment: ${var.environment}'"
}
}
# Declare your infrastructure here
resource "aws_s3_bucket" "my_bucket" {
bucket = "my-unique-bucket-name"
}

If you wanted to execute a module from a seperate project (lib), you could do so as follows:

module "lib_module" {
source = "../../path/to/my-lib/src"
}

This will automatically update the Nx graph to add a dependency between your consuming application and your lib.

Configure environment-specific variables in the src/env/*.tfvars files.

To add new environments, create a new src/env/<environment>.tfvars file with the environment-specific variables and add new entries for apply, destroy, init, plan in the project.json for the new env configuration. For example, let’s assume we want to add a prod env:

# Production environment variables
environment = "prod"
region = "us-west-2"

Remote State Bootstrap (Application Projects Only)

Section titled “Remote State Bootstrap (Application Projects Only)”

For application projects, before deploying your infrastructure, you’ll need to bootstrap the remote state backend. This creates an S3 bucket to store your Terraform state files:

Terminal window
pnpm nx run tf-infra:bootstrap

The available targets depend on your project type:

Common Targets (Both Application and Library)

Section titled “Common Targets (Both Application and Library)”

You can validate your Terraform configuration using the validate target:

Terminal window
pnpm nx run tf-infra:validate

Format your Terraform code using the fmt target:

Terminal window
pnpm nx run tf-infra:fmt

Run security checks on your infrastructure using Checkov with the test target:

Terminal window
pnpm nx run tf-infra:test

You will find your security test results in the root dist folder, under dist/packages/<my-terraform-project>/checkov.

The following targets are only available for application type projects:

Before applying changes, you can see what Terraform will do by running the plan target:

Terminal window
pnpm nx run tf-infra:plan

This will create a plan file in dist/packages/<my-terraform-project>/terraform/dev.tfplan.

Initialize your Terraform working directory with the init target:

Terminal window
pnpm nx run tf-infra:init

After planning, you can deploy your infrastructure to AWS using the apply target:

Terminal window
pnpm nx run tf-infra:apply

Retrieve output values from your Terraform configuration:

Terminal window
pnpm nx run tf-infra:output

When you need to tear down your infrastructure, use the destroy target:

Terminal window
pnpm nx run tf-infra:destroy

To clean up the bootstrap resources (S3 bucket for state storage):

Terminal window
pnpm nx run tf-infra:bootstrap-destroy

For more information about Terraform, please refer to the Terraform Documentation and AWS Provider Documentation.