Phase 0:环境准备¶
目标¶
让本机的 Terraform 能用 API Key 操作 OCI,并在动手之前把费用告警设好。
本机¶
- Terraform:
winget install --id Hashicorp.Terraform -e - VS Code 扩展:
hashicorp.terraform(语法高亮、补全)
Tip
刚装完 Terraform 时,已经打开的终端认不出 terraform 命令。重开终端即可(PATH 是终端启动时读的)。
OCI Console 上的准备¶
1. Compartment¶
Identity & Security → Compartments → Create Compartment
- 名字
tf-lab,父级 root - 所有练习资源都放在这里 → 方便确认、方便清理、Budget 也能单独设
2. Budget 告警¶
Billing & Cost Management → Budgets
| 对象 | 金额 | 告警 |
|---|---|---|
Compartment tf-lab |
$1 | 实际支出超过 1% |
| root(整个 tenancy) | $5 | 同上 |
3. API Key¶
右上角头像 → My profile → API keys → Add API key → Generate API key pair
- 下载私钥,放到
~/.oci/(绝对不要放进项目目录或 Git) - 点 Add 后 Console 会显示 config 片段
4. ~/.oci/config¶
[DEFAULT]
user=ocid1.user.oc1..xxxx
fingerprint=xx:xx:...
tenancy=ocid1.tenancy.oc1..xxxx
region=ap-osaka-1
key_file=C:\Users\<you>\.oci\oci_api_key.pem
Terraform 的 provider 只写 profile 名,认证信息都从这个文件读:
坑¶
did not find a proper configuration for private key
Console 生成的片段里是 key_file=<path to your private keyfile> # TODO。
OCI config 不支持行尾注释,# TODO 会被当成路径的一部分。改路径时把 # TODO 一起删掉。
Git 里不放什么¶
.gitignore 从一开始就排除这些:
| 文件 | 原因 |
|---|---|
*.tfstate |
里面有资源的详细信息,甚至密码 |
*.tfvars(只提交 *.tfvars.example) |
OCID、本机 IP 等环境信息 |
*.pem / *.key |
私钥 |
backend.hcl |
Object Storage 的 namespace 等 |