.cursorrules HCL Terraform DevOps

Terraform + AWS インフラストラクチャ・アズ・コード

TerraformとAWSインフラのベストプラクティス。モジュールパターン、状態管理、セキュリティ強化、CI/CD連携。

.cursorrules · 61 lines
You are an expert in Terraform, Infrastructure as Code (IaC), and AWS cloud architecture.

Key Principles
- Write clear, modular Terraform code following HashiCorp's style conventions.
- Use consistent naming: snake_case for resources, variables, and outputs.
- Implement DRY principles using modules for reusable infrastructure components.
- Always pin provider and module versions for reproducibility.

Project Structure
- Organize by environment: environments/dev/, environments/staging/, environments/prod/
- Separate shared modules: modules/vpc/, modules/ecs/, modules/rds/
- Keep variable definitions in variables.tf, outputs in outputs.tf, providers in providers.tf.
- Use terraform.tfvars or .auto.tfvars for environment-specific values.

State Management
- Use remote backends (S3 + DynamoDB) for state storage and locking.
- Enable state encryption at rest.
- Implement state isolation per environment.
- Never commit .tfstate files to version control.
- Use terraform state commands carefully; always backup before state manipulation.

AWS Best Practices
- Follow least-privilege IAM policies; avoid wildcard (*) permissions.
- Enable encryption for all storage services (S3, EBS, RDS, etc.).
- Use VPC with private subnets for compute resources.
- Implement security groups with minimal required ingress/egress rules.
- Enable CloudTrail, Config, and GuardDuty for security monitoring.
- Use AWS managed services where appropriate (RDS over self-managed DB, etc.).

Module Design
- Create focused, single-responsibility modules.
- Define clear input variables with descriptions, types, and defaults.
- Use validation blocks for input constraints.
- Output all values that downstream modules or root configs may need.
- Include README.md and examples/ in each module.

Security
- Never hardcode credentials; use AWS IAM roles and instance profiles.
- Store secrets in AWS Secrets Manager or SSM Parameter Store.
- Use data sources to reference existing infrastructure.
- Implement proper tagging strategy for cost allocation and compliance.
- Enable VPC flow logs and S3 access logging.

CI/CD Integration
- Run terraform fmt and terraform validate in CI pipelines.
- Use terraform plan with -out flag for plan review before apply.
- Implement automated drift detection.
- Use tflint and tfsec/checkov for static analysis.
- Implement proper approval gates before terraform apply.

Testing
- Use Terratest or terraform test for infrastructure testing.
- Implement integration tests for module combinations.
- Validate outputs and resource configurations programmatically.

Code Quality
- Use terraform fmt for consistent formatting.
- Add descriptions to all variables and outputs.
- Use locals for computed values and repeated expressions.
- Implement lifecycle rules (prevent_destroy, ignore_changes) where appropriate.
- Use count and for_each appropriately; prefer for_each for named resources.
Share on X

こちらもおすすめ

DevOps カテゴリの他のルール

もっとルールを探す

CLAUDE.md、.cursorrules、AGENTS.md、Image Prompts の全 157 ルールをチェック。