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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

下回分解实例创建!

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

<< 如果你希望在国内环境中调用类似的 API 并指定 `api_key`,可以按照以下步骤操作。假设你使用的是国内支持的相关模型服务(如阿里云 Qwen、百度文心一言等),下面是一些通用的指导: --- ### 1. **选择合适的国内服务商** - 阿里云:通义千问 (Qwen) - 百度:文心一言 - 华为:盘古大模型 这些平台都提供了各自的 API 文档及接入指南。 --- ### 2. **获取 API Key 和 Secret Key** 登录对应的服务商控制台(例如阿里云管理后台或百度智能云),创建应用并获得专属的: - **Access Key/Api Key**: 用户身份标识。 - **Secret Key**: 私钥用于签名验证。 注意妥善保管这两个关键数据! --- ### 3. **设置 API 地址** 不同厂商提供的接口地址各有差异,请查阅官方文档确认最新的 URL 。比如对于某些特定区域内的服务器可能会有不同的 endpoint ,形如: ```plaintext https://<region>.someprovider.com/v1/models/inference ``` 将上述 `<region>` 替换为你实际使用的地区代号即可。 --- ### 示例代码(基于假想场景) 这里以一种简单形式展示如何构造 HTTP 请求,并附带必要的认证信息(api key)。 #### Python 实现案例(针对某虚拟服务商): ```python import requests # 定义请求参数 url = "https://model.example.com/api" # 更改为真实可用的API入口点 headers = { 'Authorization': f'Bearer YOUR_API_KEY', # 将YOUR_API_KEY替换为自己申请到的实际值 } payload = {"prompt": "你好", "max_tokens": 50} # 自定义更多选项依据需求调整 response = requests.post(url, json=payload, headers=headers) if response.status_code == 200: print("成功:", response.json()) else: print(f"失败:{response.text}") ``` > 提醒:以上仅为示例模板结构仅供参考;具体字段名称需依照目标服务平台的要求为准。 --- ### 注意事项 - 确保网络环境畅通无阻,避免因防火墙等原因无法访问目标网址; - 测试阶段建议从小规模开始尝试,逐步扩大业务范围; - 对敏感资料做好加密保护措施以防泄露风险发生。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值