Fix Terraform Error: CloudWatch Log Group Already Exists
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
DevOps
Fix terraform module not installed errors. Run terraform init, fix module source paths, handle Git refs
terraform initThat's it in 90% of cases. Terraform needs to download/link modules before plan or apply.
Error: Module not installed
on main.tf line 10:
10: module "vpc" {
This module is not yet installed. Run "terraform init" to install all modules
required by this configuration.terraform init — fresh clone, new module added.terraform/ deleted — cleaned up cache, CI/CD fresh workspaceterraform initThis downloads all modules to .terraform/modules/.
If source changed, you may need:
terraform init -upgraderm -rf .terraform .terraform.lock.hcl
terraform initCommon source format mistakes:
# Local module — relative path from current directory
module "vpc" {
source = "./modules/vpc" # ✅ Correct
source = "modules/vpc" # ❌ Missing ./
source = "../shared/modules/vpc" # ✅ Relative path up
}
# Registry module — org/name/provider format
module "vpc" {
source = "terraform-aws-modules/vpc/aws" # ✅
version = "~> 5.0"
}
# Git module — double slash separates repo from subdirectory
module "vpc" {
source = "git::https://github.com/org/infra.git//modules/vpc?ref=v1.0" # ✅
source = "git::https://github.com/org/infra.git/modules/vpc?ref=v1.0" # ❌ Single slash
}
# GitHub shorthand
module "vpc" {
source = "github.com/org/terraform-modules//vpc?ref=v2.0" # ✅
}# SSH key (most common)
module "vpc" {
source = "git::ssh://git@gitlab.com/myorg/modules.git//vpc?ref=v1.0"
}
# Ensure SSH key is loaded
ssh-add ~/.ssh/id_rsa
ssh -T git@gitlab.com # Test connectivity
# HTTPS with token
module "vpc" {
source = "git::https://oauth2:${GITLAB_TOKEN}@gitlab.com/myorg/modules.git//vpc?ref=v1.0"
}In CI/CD, configure Git credentials:
# GitLab CI
before_script:
- git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/".insteadOf "https://gitlab.com/"
- terraform init# ~/.terraformrc
credentials "app.terraform.io" {
token = "your-team-token"
}
credentials "gitlab.com" {
token = "your-gitlab-token"
}# See what modules are installed
ls -la .terraform/modules/
# Verbose init output
TF_LOG=DEBUG terraform init 2>&1 | grep -i module
# Verify module directory structure
find .terraform/modules/ -name "*.tf" | head -20Speed up pipelines by caching modules:
# GitLab CI
cache:
key: terraform-modules-${CI_COMMIT_REF_SLUG}
paths:
- .terraform/modules/
- .terraform/providers/Module not installed = run terraform init. If that doesn't work, check the source path format (local needs ./, Git needs // double-slash for subdirectories), verify Git SSH/HTTPS authentication for private repos, and use terraform init -upgrade when source URLs change.
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
Fix terraform import errors when a resource already exists in state. Covers state rm, state show, reimport workflow, import blocks
Fix terraform too many command line arguments errors. Correct -var syntax, quote values with spaces, and learn proper Terraform CLI argument format for plan
Fix terraform invalid escape sequence errors. Double backslashes for Windows paths, use heredocs for regex, and learn all valid HCL escape sequences.