为在 Oracle Cloud Infrastructure (OCI) 上运行,将 Terraform 安装在 Windows 终端上的 PC 上的准备工作
把Terraform安装在Windows终端(PC)上,做好在Oracle Cloud Infrastructure(OCI)上运行的准备并做备忘录。彡(゚)(゚)
1. 下载、解压和配置环境变量(路径)Terraform。
下载 Terraform 并解压到任意目录。
下载 Terraform
https://www.terraform.io/downloads.html
请下载Windows 64位版本并在适当的目录下解压缩,设置好路径以备使用。
在命令提示符(或PowerShell)中执行并输出版本信息。
> terraform -v
Terraform v0.12.24
用 OpenSSL 创建 API Key 的密钥对。
从以下网站下载openssl的Windows二进制文件并进行安装。同时确保将其添加入路径。
Win32/Win64 OpenSSL
https://slproweb.com/products/Win32OpenSSL.htmlWin32/Win64 OpenSSL
https://slproweb.com/products/Win32OpenSSL.html
在命令提示符(或PowerShell)中创建私钥文件。
> openssl genrsa -out oci-terraform.pem 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
.......+++++
...................+++++
e is 65537 (0x010001)
用秘密密钥生成公开密钥。
> openssl rsa -pubout -in oci-terraform.pem -out oci-terraform_pub.pem
writing RSA key
在OCI控制台上创建Compartment、IAM用户、IAM组和IAM策略。
请为以下创建的策略附加权限(策略声明),以授权管理Compartment。由于Terraform的执行权限依赖于此策略,请授予所需的权限(策略)。
ALLOW GROUP ays-iam-group01 TO manage all-resources IN compartment ays-compartment01
4. 用户通过上传公钥来生成API密钥。
请按照以下步骤将公钥上传至 IAM 用户。
OCI控制台的身份→用户→在上述3中创建的IAM用户→添加公钥→上传在步骤2中创建的公钥。
当成功时,API密钥将会生成,并显示公开密钥的指纹,如下所示。
5. 下载 Terraform 的定义文件(*.tf)示例文件。
我已经上传了一个Terraform定义文件(*.tf)的示例。这是一个简单的示例,仅显示可用区的列表。请在任意目录中克隆git或下载它。
请提供中文的描述,只需一种选项:
https://github.com/gonsuke777/terraform/tree/master/oci_init
请提供中文的描述,只需一种选项:
git clone https://github.com/gonsuke777/terraform
6. 编辑变量定义文件(vars.tf)和复制密钥文件。
请编辑vars.tf文件以聚合变量。请参考下面的指示进行编辑。
7. 执行terraform操作(init、plan、apply)
我执行terraform。请在命令提示符(或PowerShell)中执行以下命令。
cd <tfファイルを格納したディレクトリ>
terraform init
terraform plan
terraform apply
以下是执行结果。成功了哟彡(^)(^)
> terraform init
Initializing the backend...
Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "oci" (hashicorp/oci) 3.73.0...
The following providers do not have any version constraints in configuration,
so the latest version was installed.
To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.
* provider.oci: version = "~> 3.73"
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
> terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
data.oci_identity_availability_domains.ads: Refreshing state...
------------------------------------------------------------------------
No changes. Infrastructure is up-to-date.
This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.
> terraform apply
data.oci_identity_availability_domains.ads: Refreshing state...
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
show-ads = [
{
"compartment_id" = "ocid1.tenancy.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
"id" = "ocid1.availabilitydomain.oc1..aaaaaaaaaijs56nfsz2usbwsr6k6t5b3do7hns6ib2day6le5scrvvjpeceq"
"name" = "xCoU:AP-TOKYO-1-AD-1"
},
]
如果在3AD的凤凰地区申请,结果将如下所示。
> terraform apply
:
Outputs:
show-ads = [
{
"compartment_id" = "ocid1.tenancy.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
"id" = "ocid1.availabilitydomain.oc1..aaaaaaaaupx7ga46w4xbb3fnkoh7rtyumacyb3ek6bw7bt3qff3mgt3mbrlq"
"name" = "xCoU:PHX-AD-1"
},
{
"compartment_id" = "ocid1.tenancy.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
"id" = "ocid1.availabilitydomain.oc1..aaaaaaaac7uydq54rcymamzauv5pd23qk2g2vqlrfu5jd3y6i6qxpvvzkf7a"
"name" = "xCoU:PHX-AD-2"
},
{
"compartment_id" = "ocid1.tenancy.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
"id" = "ocid1.availabilitydomain.oc1..aaaaaaaakemzo4ywaynz573sk4jpvwuqi7vmcuxajxfv4jjj2pthez3j2hbq"
"name" = "xCoU:PHX-AD-3"
},
]
8. 参考文件/文章
Terraform提供者
https://docs.oracle.com/cd/E97706_01/Content/API/SDKDocs/terraform.htmOracle Cloud基础设施提供者
https://www.terraform.io/docs/providers/oci/index.html必要的密钥和OCID
https://docs.oracle.com/cd/E97706_01/Content/API/Concepts/apisigningkey.htm【OCI】使用Terraform在Oracle Cloud中进行环境搭建
https://qiita.com/manaki/items/068e15f143d2bdbaae69在Oracle Cloud上开始使用Terraform,自动化的精髓
https://qiita.com/Brutus/items/ed2ddb74fa8a13369d9f
9. 总结
其实我本来想使用 Terraform 来构建上次更新的环境,但是准备工作比预想的困难,所以我将它作为了一个独立的文章。
下次一定要用 Terraform 进行环境搭建!彡(゚)(゚)
附录。
(2020/7/30补充) 在这个Terraform中,可以确认的xxxx:PHX-AD-3的可用区域部分xxxx的命名在每个租户之间是不同的。真的吗?(・)(・)
以下是手册描述
Oracle云基础设施为可用域名称使用租户特定前缀。例如:您的租户的可用域类似于Uocm:PHX-AD-1、Uocm:PHX-AD-2等等。