# Oracle Cloud 配置指南 ## 概述 本指南将帮助你配置 Oracle Cloud Infrastructure (OCI) 以便与 OpenTofu 一起使用。 ## 前提条件 1. Oracle Cloud 账户(可以使用免费层) 2. 已安装 OpenTofu 3. 已安装 OCI CLI(可选,但推荐) ## 步骤 1: 创建 Oracle Cloud 账户 1. 访问 [Oracle Cloud](https://cloud.oracle.com/) 2. 点击 "Start for free" 创建免费账户 3. 完成注册流程 ## 步骤 2: 获取必要的 OCID ### 获取 Tenancy OCID 1. 登录 Oracle Cloud Console 2. 点击右上角的用户图标 3. 选择 "Tenancy: " 4. 复制 OCID 值 ### 获取 User OCID 1. 在 Oracle Cloud Console 中 2. 点击右上角的用户图标 3. 选择 "User Settings" 4. 复制 OCID 值 ### 获取 Compartment OCID 1. 在导航菜单中选择 "Identity & Security" > "Compartments" 2. 选择你要使用的 compartment(通常是 root compartment) 3. 复制 OCID 值 ## 步骤 3: 创建 API 密钥 ### 生成密钥对 ```bash # 创建 .oci 目录 mkdir -p ~/.oci # 生成私钥 openssl genrsa -out ~/.oci/oci_api_key.pem 2048 # 生成公钥 openssl rsa -pubout -in ~/.oci/oci_api_key.pem -out ~/.oci/oci_api_key_public.pem # 设置权限 chmod 400 ~/.oci/oci_api_key.pem chmod 400 ~/.oci/oci_api_key_public.pem ``` ### 添加公钥到 Oracle Cloud 1. 在 Oracle Cloud Console 中,进入 "User Settings" 2. 在左侧菜单中选择 "API Keys" 3. 点击 "Add API Key" 4. 选择 "Paste Public Key" 5. 复制 `~/.oci/oci_api_key_public.pem` 的内容并粘贴 6. 点击 "Add" 7. 复制显示的 fingerprint ## 步骤 4: 配置 terraform.tfvars 编辑 `infrastructure/environments/dev/terraform.tfvars` 文件: ```hcl # Oracle Cloud 配置 oci_config = { tenancy_ocid = "ocid1.tenancy.oc1..aaaaaaaa_your_actual_tenancy_id" user_ocid = "ocid1.user.oc1..aaaaaaaa_your_actual_user_id" fingerprint = "aa:bb:cc:dd:ee:ff:gg:hh:ii:jj:kk:ll:mm:nn:oo:pp" private_key_path = "~/.oci/oci_api_key.pem" region = "ap-seoul-1" # 或你选择的区域 compartment_ocid = "ocid1.compartment.oc1..aaaaaaaa_your_compartment_id" } ``` ## 步骤 5: 验证配置 ```bash # 检查配置 ./scripts/setup/setup-opentofu.sh check # 初始化 OpenTofu ./scripts/setup/setup-opentofu.sh init # 生成计划 ./scripts/setup/setup-opentofu.sh plan ``` ## 可用区域 常用的 Oracle Cloud 区域: - `ap-seoul-1` - 韩国首尔 - `ap-tokyo-1` - 日本东京 - `us-ashburn-1` - 美国弗吉尼亚州 - `us-phoenix-1` - 美国亚利桑那州 - `eu-frankfurt-1` - 德国法兰克福 ## 免费层资源 Oracle Cloud 免费层包括: - 2 个 AMD 计算实例(VM.Standard.E2.1.Micro) - 4 个 Arm 计算实例(VM.Standard.A1.Flex) - 200 GB 块存储 - 10 GB 对象存储 - 负载均衡器 - 数据库等 ## 故障排除 ### 常见错误 1. **401 Unauthorized**: 检查 API 密钥配置 2. **404 Not Found**: 检查 OCID 是否正确 3. **权限错误**: 确保用户有足够的权限 ### 验证连接 ```bash # 安装 OCI CLI(可选) pip install oci-cli # 配置 OCI CLI oci setup config # 测试连接 oci iam compartment list ``` ## 安全最佳实践 1. 定期轮换 API 密钥 2. 使用最小权限原则 3. 不要在代码中硬编码凭据 4. 使用 compartment 隔离资源 5. 启用审计日志 ## 参考资料 - [Oracle Cloud Infrastructure 文档](https://docs.oracle.com/en-us/iaas/) - [OCI Terraform Provider](https://registry.terraform.io/providers/oracle/oci/latest/docs) - [Oracle Cloud 免费层](https://www.oracle.com/cloud/free/)