Skip to content

qiniu/terraform-module

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

七牛 Terraform 预设模板

基于本地 Terraform 运行

安装 Qiniu Provider

由于当前的七牛 Provider 暂未发布到 registry.terraform.io 平台, 故无法实现在线自动安装,需要手动下载插件二进制并拷贝到指定目录下。

编写本地配置文件,默认配置文件路径在$HOME/.terraformrc

// 全局插件缓存本地目录
plugin_cache_dir = "/home/zzq/.terraform.d/plugin-cache"

provider_installation {
  // 本地文件系统镜像源,qiniu 插件目前需要使用这种方式安装,需要将插件拷贝到指定镜像目录中
  filesystem_mirror {
    path    = "/home/zzq/.terraform.d/plugin-mirror"
    include = ["registry.terraform.io/hashicorp/qiniu"]
  }
  // 官方镜像源,需要排除 qiniu 插件的安装
  direct {
    exclude = ["registry.terraform.io/hashicorp/qiniu"]
  }
}

将插件复制到配置文件中配置的对应目录下,比如一个示例的目录结构如下:

➜  .terraform.d tree
.
├── checkpoint_cache
├── checkpoint_signature
├── plugin-cache
└── plugin-mirror
    └── registry.terraform.io
        └── hashicorp
            └── qiniu
                └── 1.0.0
                    └── linux_amd64
                        └── terraform-provider-qiniu

运行相应的 Terraform Module

设置环境变量

# Qiniu 账户的AK/SK
export QINIU_ACCESS_KEY="QINIU_ACCESS_KEY"
export QINIU_SECRET_KEY="<QINIU_SECRET_KEY>"
# 要操作的资源默认的区域ID
export QINIU_REGION_ID="ap-southeast-1"

一键部署单实例 MySQL 应用

cd mysql/standalone
terraform init
terraform apply
# 之后将要交互式输入各个tf模板参数,绝大部分参数都有默认值,这里只必填一个密码即可

操作界面如下:

➜  standalone git:(main) ✗ terraform apply
var.mysql_password
  MySQL password

  Enter a value:

data.qiniu_compute_images.available_official_images: Reading...
data.qiniu_compute_images.available_official_images: Read complete after 0s

Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # qiniu_compute_instance.mysql_primary_node will be created
  + resource "qiniu_compute_instance" "mysql_primary_node" {
      + cost_charge_type     = "PostPaid"
      + cpu                  = (known after apply)
      + created_at           = (known after apply)
      + description          = (known after apply)
      + id                   = (known after apply)
      + image_id             = "68007b52495c899e195a1e15"
      + image_name           = (known after apply)
      + instance_type        = "ecs.t1.c1m2"
      + memory               = (known after apply)
      + name                 = (known after apply)
      + password             = (sensitive value)
      + private_ip_addresses = (known after apply)
      + public_ip_addresses  = (known after apply)
      + region_id            = (known after apply)
      + region_name          = (known after apply)
      + state                = "Running"
      + system_disk_id       = (known after apply)
      + system_disk_size     = 20
      + system_disk_type     = "local.ssd"
      + user_data            = (sensitive value)
    }

  # random_password.mysql_instance_password will be created
  + resource "random_password" "mysql_instance_password" {
      + bcrypt_hash = (sensitive value)
      + id          = (known after apply)
      + length      = 16
      + lower       = true
      + min_lower   = 0
      + min_numeric = 0
      + min_special = 0
      + min_upper   = 0
      + number      = true
      + numeric     = true
      + result      = (sensitive value)
      + special     = true
      + upper       = true
    }

  # random_string.resource_suffix will be created
  + resource "random_string" "resource_suffix" {
      + id          = (known after apply)
      + length      = 6
      + lower       = true
      + min_lower   = 0
      + min_numeric = 0
      + min_special = 0
      + min_upper   = 0
      + number      = true
      + numeric     = true
      + result      = (known after apply)
      + special     = false
      + upper       = false
    }

Plan: 3 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + mysql_primary_endpoint = (known after apply)

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

random_string.resource_suffix: Creating...
random_password.mysql_instance_password: Creating...
random_string.resource_suffix: Creation complete after 0s [id=93wsze]
random_password.mysql_instance_password: Creation complete after 0s [id=none]
qiniu_compute_instance.mysql_primary_node: Creating...
qiniu_compute_instance.mysql_primary_node: Still creating... [00m10s elapsed]
qiniu_compute_instance.mysql_primary_node: Still creating... [00m20s elapsed]
qiniu_compute_instance.mysql_primary_node: Still creating... [00m30s elapsed]
qiniu_compute_instance.mysql_primary_node: Still creating... [00m40s elapsed]
qiniu_compute_instance.mysql_primary_node: Creation complete after 41s [id=i-69281ce0e3108870683f3b35]

Apply complete! Resources: 3 added, 0 changed, 0 destroyed.

Outputs:

mysql_primary_endpoint = "10.198.1.44:3306"

Tips: 也可以同目录创建一个 .tfvars.json 后缀结尾的 json 文件,里面放入所有 variables.tf中定义的变量值作为输入,apply 时将自动读取。

其他一些常用操作:

# 销毁所有已创建资源
terraform destroy
# 查看资源变更计划
terraform plan
# 导出资源变更计划文件
terraform plan -out="tfplan"
# 基于资源变更计划文件进行apply变更
terraform apply "tfplan"

基于七牛资源栈在线运行

TODO

资源栈 Terraform Provider 白名单列表

在七牛资源栈中运行 Terraform 时,只能使用以下已纳入白名单的 Provider。使用白名单以外的 Provider 将导致资源栈创建/更新失败。如需支持其他 Provider,请提交工单申请。

详细说明请参考:资源栈常见问题排查

Provider Source Version 用途
qiniu qiniu/qiniu 1.0.0 管理七牛云资源
random hashicorp/random 3.8.0 生成随机数
time hashicorp/time 0.13.1 处理时间相关操作
archive hashicorp/archive 2.7.1 处理压缩文件
cloudinit hashicorp/cloudinit 2.3.7 生成 cloud-init 配置
external hashicorp/external 2.3.5 执行外部程序
null hashicorp/null 3.2.4 提供空资源
http hashicorp/http 3.5.0 发起 HTTP 请求
tls hashicorp/tls 4.1.0 生成 RSA 密钥和证书
local hashicorp/local 2.5.3 操作本地文件
docker kreuzwerker/docker 3.6.2 管理 Docker 容器

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors