使用Terraform来玩转华为云1-准备工作

本文介绍如何使用Terraform工具在华为云上部署基础设施,包括Terraform的安装配置、环境变量设置及工程文件编写。通过示例展示了如何定义资源、变量及输出,最后完成初始化和应用。

terraform 工具准备

https://www.terraform.io/downloads.html

选择对应平台的版本,例如 macOS 或者 Linux

下载,解压,可以放到 

/usr/local/bin/

 目录下

$ terraform -v
Terraform v0.11.11
Your version of Terraform is out of date! The latest version
is 0.11.13. You can update by downloading from www.terraform.io/downloads.html

 

确认可以正常执行

准备华为云的access_key和secret_key

华为云控制台  我的凭证  管理访问密钥  新增访问密钥

然后设置环境变量

export TF_VAR_hw_access_key=“XXX"
export TF_VAR_hw_secret_key=“YYY"
export TF_VAR_hw_region=cn-east-2

准备工程文件

$ cat main.tf

 

provider "huaweicloud" {
  # tenant_name = "cn-east-2"
  auth_url    = "https://iam.myhwclouds.com/v3"
  insecure    = "false"
  region      = "${var.hw_region}"
  access_key  = "${var.hw_access_key}"
  secret_key  = "${var.hw_secret_key}"
#  version = "1.4.0"
}
data "huaweicloud_images_image_v2" "os" {
  name = "${var.image_name}"
  most_recent = true
}
$ cat vars.tf
variable "hw_access_key" {
    type = "string"
}
variable "hw_secret_key" {
    type = "string"
}
variable "hw_region" {
    default = "cn-east-2"
    description = "The region name where the resource will be created"
}
variable "hw_az" {
    default = "cn-east-2a"
    description = "The availability zone name where the resource will be created"
}
variable "flavor_name" {
    default = "t6.small.1"
    description = "Define the instance flavor / instance type"
}
variable "image_name" {
    default = "CentOS 7.6 64bit"
    description = "The OS image of instance that we will use"
}
$ cat output.tf
output "os_image_id" {
  value = "${data.huaweicloud_images_image_v2.os.id}"
}

开搞

初始化 Terraform (下载华为云插件)

$ terraform init
Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "huaweicloud" (1.4.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.huaweicloud: version = "~> 1.4"
Terraform has been successfully initialized!

Plan

$ 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.huaweicloud_images_image_v2.os: Refreshing state...
------------------------------------------------------------------------
No changes. Infrastructure is up-to-date.

Apply,可以打印我们的output

$ terraform apply
data.huaweicloud_images_image_v2.os: Refreshing state...
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
os_image_id = 9526f9b7-423c-4fdc-92ad-f1630a524652

下回分解实例创建!

购买华为云请点击立即购买

获取AWS或其他云服务的访问密钥(ACCESS_KEY)和秘密密钥(SECRET_KEY)是管理云资源访问权限的重要步骤。以下是针对AWS及其他主流云服务提供商(如Azure、Google Cloud)的详细说明。 ### AWS 获取 ACCESS_KEY 和 SECRET_KEY 在AWS中,访问密钥和秘密密钥用于访问AWS服务的API。获取这些密钥的步骤如下: 1. 登录 [AWS管理控制台](https://aws.amazon.com/console/)。 2. 进入 **IAM (Identity and Access Management)** 服务[^2]。 3. 在左侧导航栏中选择 **用户 (Users)**。 4. 选择需要获取密钥的用户,点击 **“安全凭证” (Security credentials)** 标签。 5. 点击 **“创建访问密钥” (Create access key)**。 6. 下载或复制生成的 **Access Key ID** 和 **Secret Access Key**。这是唯一一次可以查看或下载秘密密钥的机会,请务必妥善保存。 建议将密钥配置到AWS CLI或SDK中,使用以下命令进行配置: ```bash aws configure ``` 输入提示的Access Key ID、Secret Access Key、默认区域(如`us-east-1`)和输出格式(如`json`)即可完成配置。 ### Azure 获取访问密钥 Azure 使用“存储账户访问密钥”来访问存储服务,或者使用“服务主体(Service Principal)”来访问其他资源。以下是获取存储账户密钥的方法: 1. 登录 [Azure门户](https://portal.azure.com/)。 2. 进入 **存储账户 (Storage Accounts)**。 3. 选择一个存储账户。 4. 在左侧菜单中选择 **“访问密钥 (Access keys)”**。 5. 在页面中可以看到两个密钥(key1 和 key2),可以用于访问存储服务。 对于资源管理API的访问,需创建服务主体并获取客户端ID和客户端密钥,这通常通过Azure CLI或PowerShell完成: ```bash az ad sp create-for-rbac --name "myApp" --role contributor --scopes /subscriptions/{subscription-id}/resourceGroups/{group-name} ``` ### Google Cloud Platform (GCP) 获取密钥 GCP 使用服务账户密钥文件(JSON格式)来访问API。以下是获取方法: 1. 登录 [Google Cloud Console](https://console.cloud.google.com/)。 2. 导航至 **IAM & Admin > Service Accounts**。 3. 创建或选择一个服务账户。 4. 点击 **“Keys”** 标签页。 5. 点击 **“Add Key > Create New Key”**,选择 **JSON** 格式并下载。 该JSON文件包含访问所需的私钥和客户端ID,适用于SDK和API调用。 ### 注意事项 - **安全性**:请勿将密钥硬编码在代码中或提交到公共代码仓库。建议使用环境变量或密钥管理服务(如AWS Secrets Manager、Azure Key Vault、Google Cloud Secret Manager)。 - **权限最小化**:确保密钥对应的用户或服务账户仅拥有执行任务所需的最小权限[^2]。 - **定期轮换**:定期更换密钥以减少安全风险。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值