基础设施即代码安全
学习如何保护使用 Terraform 以代码定义的云基础设施,扫描模板中的错误配置,并防止配置漂移和不安全的默认设置。
基础设施即代码安全 是 CoddyKit 上的免费 Secure Coding & OWASP Top 10 for Backend 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Secure Coding & OWASP Top 10 for Backend 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Secure Coding & OWASP Top 10 for Backend 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What Is IaC?
Infrastructure as Code (IaC) defines cloud resources in declarative files (Terraform, CloudFormation, Bicep) instead of clicking through consoles. It makes infrastructure repeatable, reviewable, and version-controlled.
That same automation means a single mistake can be deployed everywhere instantly.
Security Benefits of IaC
IaC enables security at scale:
- Changes go through code review and version history
- Configurations are consistent across environments
- Security policies can be enforced automatically
The goal is to catch insecure config before it ever reaches the cloud.
Common Misconfigurations
The most frequent IaC security mistakes include:
- Storage buckets open to the public
- Security groups allowing 0.0.0.0/0 on sensitive ports
- Unencrypted volumes and databases
- Overly broad IAM permissions
An Insecure Example
This Terraform snippet exposes a database port to the entire internet.
resource 'aws_security_group_rule' 'db' {
type = 'ingress'
from_port = 5432
to_port = 5432
protocol = 'tcp'
cidr_blocks = ['0.0.0.0/0'] # INSECURE: open to all
}The Secure Version
Restrict access to a known private range and enforce encryption by default.
resource 'aws_security_group_rule' 'db' {
type = 'ingress'
from_port = 5432
to_port = 5432
protocol = 'tcp'
cidr_blocks = ['10.0.1.0/24'] # private app subnet only
}Static Scanning
Tools like Checkov, tfsec, and Terrascan scan IaC files for insecure patterns before deployment. Run them in CI so risky templates fail the build automatically.
# Example CI step (conceptual)
# checkov -d ./infra --quiet
rules_failed = ['CKV_AWS_24: SSH open to 0.0.0.0/0']
for r in rules_failed:
print('FAIL', r)Policy as Code
Policy as Code tools like Open Policy Agent (OPA) and Sentinel let you write rules such as 'no public buckets' that block non-compliant plans automatically, turning security standards into enforceable code.
Securing State Files
Terraform state can contain secrets and resource details. Store it in an encrypted, access-controlled backend (such as an encrypted S3 bucket with locking), never in the git repository.
- Encrypt state at rest
- Restrict who can read it
- Enable state locking to prevent corruption
Avoiding Hardcoded Secrets
Never put credentials directly in IaC files. Reference a secrets manager or inject values at apply time so secrets never land in version control or state.
Detecting Drift
Drift happens when someone changes infrastructure manually, diverging from the code. Run drift detection regularly so unauthorized or accidental changes are caught and reconciled.
Least-Privilege Modules
Build reusable modules with secure defaults: encryption on, public access off, minimal IAM. Teams that consume hardened modules inherit good security without having to be experts.
Quick Check
Test your understanding of IaC security.
Recap
You learned how to secure Infrastructure as Code: review changes, scan templates with tools like Checkov, enforce policy as code, protect state files, keep secrets out of templates, and detect drift. Catching misconfiguration in code stops it before it reaches production.
常见问题解答
「基础设施即代码安全」课时是免费的吗?
是的 — 「基础设施即代码安全」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Secure Coding & OWASP Top 10 for Backend 课程的其余内容,请升级到 CoddyKit PRO。 Secure Coding & OWASP Top 10 for Backend 课程共包含 4 节课。
「基础设施即代码安全」这节课中我会学到什么?
学习如何保护使用 Terraform 以代码定义的云基础设施,扫描模板中的错误配置,并防止配置漂移和不安全的默认设置。 你通过在浏览器中直接运行的动手代码来练习 Secure Coding & OWASP Top 10 for Backend,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Secure Coding & OWASP Top 10 for Backend 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Secure Coding & OWASP Top 10 for Backend 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「基础设施即代码安全」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Secure Coding & OWASP Top 10 for Backend 课中编写并运行代码吗?
能。每节 Secure Coding & OWASP Top 10 for Backend 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。