amazon eks - How to create a state file and acquire lock at parent level in Terraform with modular structure? - Stack Overflow

admin2025-05-02  0

I have a modular Terraform structure managed with Terragrunt, and I want to maintain the modularity of my configuration. However, I need to create and manage the state file and acquire locks at the parent directory level.

Here’s my current project structure:

.
├── backend-config.yml
├── backend.tf
├── core-dns-csi-driver-addon
│   ├── terragrunt.hcl
├── eks
│   ├── terragrunt.hcl
├── karpenter
│   ├── terragrunt.hcl
├── kube-proxy-vpc-cni-addon
│   ├── terragrunt.hcl
├── local-swarm-nodegroup
│   ├── terragrunt.hcl
├── loki
│   ├── terragrunt.hcl
├── metrics-server
│   ├── terragrunt.hcl
├── provider.tf
├── storageclass
│   ├── terragrunt.hcl
├── terragrunt.hcl  # Parent-level configuration
├── update_yaml.py
└── vpc
    ├── terragrunt.hcl

Requirements:

  • State File: A single shared state file at the parent level (e.g., root directory).
  • Locking: State file locking using DynamoDB or another mechanism at the parent level.
  • Modularity: Retain the modular structure with separate terragrunt.hcl files for each module (e.g., eks, vpc, etc.).

What I Have Tried:

  1. Terragrunt Root Configuration:

    Created a root terragrunt.hcl file with the shared back-end configuration:

    remote_state {
      backend = "s3"
      config = {
        bucket         = "my-terraform-state"
        key            = "terraform/parent-level-state.tfstate"
        region         = "us-east-1"
        dynamodb_table = "eks-locks"
        encrypt        = true
      }
    }
    
  2. Child Module Configuration:

    Referenced the parent configuration in each module's terragrunt.hcl file:

    include {
      path = find_in_parent_folders()
    }
    

How can I configure Terragrunt to enforce a single state file and locking mechanism at the parent level while keeping the modular structure intact?

How can I apply and destroy at module level, keeping state file intact at parent level?

Is there a better approach to managing modular Terraform configurations with shared state and lock files at the parent level?

转载请注明原文地址:http://anycun.com/QandA/1746117976a91917.html