RousselTMLEARNING

TP · Terraform: from discovery to expertise

Lab: A tested module (terraform test, tflint, Checkov)

What you will learn in this TP
  • Design a module with validations
  • Test with mock_provider
  • Analyse code security
Mode :

All steps and instructions are shown. Recommended for this lab: semi-guided

Introduction #

Context. Several teams need compliant buckets. You provide a secure-by-default, automatically tested module.

Prerequisites: Terraform 1.10 or later, Docker, LocalStack and terraform-local (pip install localstack terraform-local); no cloud account or charges  ·  Estimated time: 50 min

A secure-by-default module with automated tests and analysis.

Hands-on #

Guided steps, with their solution.

  1. 1. Write the module

    Why? A good module enforces best practices by default (versioning, public access block).

    Create modules/bucket with validated variables, resources and output.
  2. 2. Test the module

    Why? The mock provider makes tests instant and independent of any cloud.

    Write modules/bucket/tests/bucket.tftest.hcl and run the tests.
  3. 3. Analyse the code

    Why? tflint flags errors and conventions; Checkov reports security bad practices.

    Run tflint and Checkov on the module and read the findings (e.g. KMS encryption or access logging).
  4. 4. Use the module

    Why? The root module simply assembles tested modules.

    Call the module twice with for_each from the root and apply on LocalStack.

Validate your work

Check that each goal above is reached and that you get the result described in the introduction.

Difficulty level : ●●●○○ (3/5)

My progress

  • ⏱ Estimated time : ≈ 50 min

Sign in to track your progress and take notes. Sign in

⭐ Rate this lesson

5.0 initial rating from the teaching team

Sign in to rate this lesson →

💬 Questions about this lesson

No questions yet: be the first to ask for help or share your configuration.

Ask a question about this lesson →

Recommended Articles

Terraform modules: designing, versioning and reusing

Structure Terraform with modules: root module and child modules, inputs and outputs, sources (loc...

Testing and securing Terraform: validate, terraform test, tflint, Checkov and policies

Ensure the quality of Terraform code: fmt and validate, native terraform test tests with mocked p...

Terraform: principles, architecture and workflow

Understand Terraform: declarative infrastructure as code, architecture (core, providers, state), ...

The HCL language: resources, variables, outputs, data and expressions

Writing Terraform: terraform, provider, resource and data blocks, typed variables with validation...

Terraform across multiple clouds: AWS, Azure, Google Cloud, OVHcloud and Kubernetes

Use Terraform with the main providers: aws, azurerm, google, ovh and openstack, kubernetes and he...

Terraform state: remote backends, locking, import and refactoring

Manage Terraform state: the role of the state file, sensitive data, remote backends (S3, azurerm,...

Terraform best practices: structure, state, security, review and maintenance

Terraform best practices for teams: repository structure, naming, pinned versions, encrypted and ...

Terraform in CI/CD: pipelines, plan review, OIDC and GitOps

Automate Terraform: plan pipeline on merge requests and apply after approval, posting the plan as...

Terraform multi-environment, workspaces and repository organization

Organize Terraform for several environments: one folder per environment, tfvars files, CLI worksp...

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!