模板是 Walrus 的核心功能之一,模板创建完成后用户可以重复使用,并在使用过程中逐渐沉淀研发和运维团队的最佳实践,进一步简化服务及资源的部署。用户可以使用 HCL 语言自定义创建模板,也可以一键复用 Terraform 社区中上万个成熟的 Module。在本文中,我们将以阿里云 EC2 为例,介绍如何创建 Walrus 阿里云 EC2 模板并使用它在阿里云上创建 ECS 实例服务。

前提条件

  1. 一个用于存储模板的 GitHub 仓库。
  2. 部署 Walrus(https://seal-io.github.io/docs/zh/deploy/standalone)。

在 GitHub 上创建仓库

  1. 在 GitHub 上创建你自己的新仓库,这里我们使用仓库 demo(https://github.com/walrus-catalog-demo/demo)

  2. 将仓库克隆到你的本机。

git clone git@github.com:walrus-catalog-demo/demo.git

创建模板文件

  1. 进入克隆的仓库目录。
cd demo

在目录中创建如下文件:

- demo
- main.tf
- outputs.tf
- variables.tf
- README.md

main.tf文件定义了要创建的资源,这里我们为模板定义了一个阿里云 ECS 实例的资源。

resource "alicloud_instance" "example" {
instance_name = "demo-instance"
instance_type = var.instance_type
image_id = var.image_id
system_disk_category = var.system_disk_category
system_disk_size = var.system_disk_size
internet_charge_type = var.internet_charge_type
internet_max_bandwidth_out = var.internet_max_bandwidth_out vswitch_id = data.alicloud_vswitches.default.vswitches.0.id host_name = var.hostname
key_name = "seal-demo" security_groups = [
data.alicloud_security_groups.default.groups.0.id
]
} data "alicloud_vpcs" "default" {
name_regex = "default"
} data "alicloud_vswitches" "default" {
vpc_id = data.alicloud_vpcs.default.vpcs.0.id
} data "alicloud_security_groups" "default" {
name_regex = "default"
} resource "null_resource" "health_check" {
depends_on = [
alicloud_instance.example,
]
}

variables.tf文件定义了模板中使用的变量,Walrus 将使用这些变量为用户生成模板填写表单。

Walrus 通过 @label@group注释来定义变量的标签和组。可选的@options 注释用于定义变量的下拉选项, 如果没有定义 @options注释,则该变量将在表单中显示为文本框。关于模板变量注释的更多详细信息可以在此链接(https://seal-io.github.io/docs/zh/operation/template#variable-style-extension)查看。

在这个例子中,我们定义了两个组: 基础高级,它们将在创建服务的模板表单中以两个选项卡的形式显示。

# @label "实例规格"
# @group "基础"
variable "instance_type" {
description = "The instance type of the ECS instance"
default = "ecs.s6-c1m2.small"
} # @label "VM镜像id"
# @group "基础"
variable "image_id" {
description = "The ID of the image used to launch the ECS instance"
default = "ubuntu_18_04_x64_20G_alibase_20230208.vhd"
} # @label "系统磁盘类型"
# @group "基础"
# @options ["ephemeral_ssd", "cloud_efficiency", "cloud_ssd", "cloud_essd", "cloud", "cloud_auto"]
variable "system_disk_category" {
description = "The category of the system disk"
default = "cloud_efficiency"
} # @label "系统盘大小"
# @group "基础"
variable "system_disk_size" {
description = "The size of the system disk, value range: [20, 500]"
default = 40
} # @label "主机名"
# @group "基础"
variable "hostname" {
type = string
description = "The hostname of the ECS instance"
default = ""
} # @label "网络计费类型"
# @group "高级"
# @options ["PayByTraffic", "PayByBandwidth"]
variable "internet_charge_type" {
description = "The billing method of the public network bandwidth"
default = "PayByTraffic"
} # @label "最大出口带宽(MB)"
# @group "高级"
variable "internet_max_bandwidth_out" {
description = "The maximum outbound bandwidth of the public network"
default = 5
}

outputs.tf文件定义了模板的输出,它们将在服务创建后作为服务的输出参数查看,并且可以被其他服务引用。

output "public_ip" {
value = alicloud_instance.example.public_ip
} output "primary_ip_address" {
value = alicloud_instance.example.primary_ip_address
}

README.md文件是模板的描述。在使用此模板创建服务时,我们可以在模板详情页面查看该模板的具体功能参数及定义。这里我们可以使用工具 terraform-docs来生成模板的描述。

terraform-docs markdown . > README.md

生成的README.md文件如下:

## Requirements

No requirements.

## Providers

| Name | Version |
|------|---------|
| <a name="provider_alicloud"></a> [alicloud](#provider\_alicloud) | n/a |
| <a name="provider_null"></a> [null](#provider\_null) | n/a | ## Modules No modules. ## Resources | Name | Type |
|------|------|
| [alicloud_instance.example](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/instance) | resource |
| [null_resource.health_check](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [alicloud_security_groups.default](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/data-sources/security_groups) | data source |
| [alicloud_vpcs.default](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/data-sources/vpcs) | data source |
| [alicloud_vswitches.default](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/data-sources/vswitches) | data source | ## Inputs | Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_hostname"></a> [hostname](#input\_hostname) | The hostname of the ECS instance | `string` | `""` | no |
| <a name="input_image_id"></a> [image\_id](#input\_image\_id) | The ID of the image used to launch the ECS instance | `string` | `"ubuntu_18_04_x64_20G_alibase_20230208.vhd"` | no |
| <a name="input_instance_type"></a> [instance\_type](#input\_instance\_type) | The instance type of the ECS instance | `string` | `"ecs.s6-c1m2.small"` | no |
| <a name="input_internet_charge_type"></a> [internet\_charge\_type](#input\_internet\_charge\_type) | The billing method of the public network bandwidth | `string` | `"PayByTraffic"` | no |
| <a name="input_internet_max_bandwidth_out"></a> [internet\_max\_bandwidth\_out](#input\_internet\_max\_bandwidth\_out) | The maximum outbound bandwidth of the public network | `number` | `5` | no |
| <a name="input_system_disk_category"></a> [system\_disk\_category](#input\_system\_disk\_category) | The category of the system disk | `string` | `"cloud_efficiency"` | no |
| <a name="input_system_disk_size"></a> [system\_disk\_size](#input\_system\_disk\_size) | The size of the system disk, value range: [20, 500] | `number` | `40` | no | ## Outputs | Name | Description |
|------|-------------|
| <a name="output_primary_ip_address"></a> [primary\_ip\_address](#output\_primary\_ip\_address) | n/a |
| <a name="output_public_ip"></a> [public\_ip](#output\_public\_ip) | n/a |

提交模板版本

git add .
git commit -m "add template files"
git push -u origin main

为模板创建一个标签作为版本。

git tag v0.0.1
git push --tags

在 Walrus 上创建模板

  1. 在浏览器中打开 Walrus 并登录。

  2. 转到 运维中心下的模板管理,使用我们刚刚创建的模板新建一个模板,这里我们将模板命名为 aliyun-ec2

导入任务完成后,模板将显示在模板列表中,我们可以看到模板版本为刚刚创建的版本 v0.0.1

  1. 运维中心的连接器下添加阿里云连接器。

  2. 配置阿里云连接器到环境.

  3. 使用模板aliyun-ec2创建一个服务, 表单组和标签的渲染是根据上述模板中定义的变量注释自动生成的。

服务创建后,我们可以看到服务的详情和模板的输出

在阿里云控制台检查 ECS 实例,我们可以看到 ECS 实例已成功创建。

Walrus 入门教程:如何创建模板以沉淀可复用的团队最佳实践的更多相关文章

  1. SharePoint 2013 入门教程之创建页面布局及页面

    在SharePoint的使用过程中,页面布局和页面时很重要的两个概念,主要用于数据个性化展示,下面,我们简单介绍一下SharePoint的页面布局和页面的个性化. 一. SharePoint页面模型概 ...

  2. 无废话SharePoint入门教程三[创建网站集和网站]

    一.前言 前两篇文章讲解了什么是SharePoint,并且介绍了在SharePoint中一些常用的概念.但概念终究是概念,我们还是要脚踏实地的去动手实践.下面的文章对于了解SharePoint的人来说 ...

  3. ActiveReports 报表控件官方中文入门教程 (2)-创建、数据源、浏览以及发布

    本篇文章将阐述首次使用 ActiveReports 报表控件 的方法,包括添加报表文件.绑定数据源以及如何发布报表等内容. ActiveReports 报表控件官方中文入门教程 (1)-安装.激活以及 ...

  4. mySQL学习入门教程——2.创建表

    二.创建表 一.创建数据表的SQL语句模型(弱类型)CREATE TABLE [IF NOT EXISTS] 表名称(字段名1 列的类型[属性][索引],字段名2 列的类型[属性][索引],-字段名n ...

  5. [SharePoint 2013 入门教程 2 ] 创建WEB应用程序,网站集,网站

    SharePoint 2013 的 Hello World 由大到小  创建WEB应用程序(老母),网站集(儿子),网站(孙子) 直接确定,其余都默认 填入标题,选好模板.网站集 儿子就有了. 点击页 ...

  6. 使用 ADD-ON SDK 开发 基于 Html JQuery 和 CSS 的 firefox 插件入门教程1: 创建一个简单的 Add-on

    [本文转载自http://sixpoint.me/942/implementing-simple-addon/] 实现一个简单的插件 教程的这个部分带你使用 SDK 来实现, 运行并打包一个插件. 这 ...

  7. WCF入门教程2——创建第一个WCF程序

    本节目标 掌握接口 理解契约式编程 创建宿主程序 创建客户端程序访问服务 什么是接口 认识一下接口 必须知道的接口特性 接口不可以被实例化(常作为类型使用) 实现类必须实现接口的所有方法(抽象类除外) ...

  8. SharePoint 2013 入门教程之创建及修改母版页

    在SharePoint 2013中,微软提供了根据HTML页面转换Master页的方法,并支持单项同步,但是这样的更新,并不完善,会使一些功能造成丢失,所以,了解Master结构的人,尽量直接去修改M ...

  9. 无废话SharePoint入门教程五[创建SharePoint页面布局]

    一.前言 文章成体系,如果有不明白的地方请查看前面的文章. 二.目录 1.创建页面布局 2.首次使用页面布局 3.修改页面布局 4.使用页面布局 5.最终效果 1.创建页面布局 (1)打开设计管理器, ...

  10. 无废话SharePoint入门教程四[创建SharePoint母版页]

    一.前言 文章成体系,如果有不明白的地方请查看前面的文章. 二.目录 1.创建HTML页面 2.将HTML文件转换为SharePoint母版页 3.在 SPD中修改母版页“PlaceHolderMai ...

随机推荐

  1. java_3.运算符、if条件结构

    运算符.if条件结构 关系运算符 == != < > >= <= 1.关系运算符运算的结果是boolean类型 2.可以使用boolean类型的变量接收关系运算的结果 publ ...

  2. 资源迁移OSS方案记录

    视频资源迁移到OSS服务器上,记录一下迁移过程. 搭建流程 在阿里云上购买oss,并获取具有该Bucket访问权限的AccessKey ID和AccessKey Secret信息. 数据迁移方案一 第 ...

  3. CSS3 rgb and rgba(透明色)的使用

    作者:WangMin 格言:努力做好自己喜欢的每一件事 对于颜色相信大家都很敏感,眼睛所见之处都存在颜色,那在css中我们用什么来表示颜色呢?CSS 中的颜色有三种定义方式:使用颜色方法(RGB.RG ...

  4. 微前端、single-spa初探

    微前端 微前端,前端这次词就不用多做解释了,这个概念的重点在于这个"微"字, 从字面意义上看,微是小的意思,小是相对于大的一个用于比较的形容词,所以通常是在项目庞大的情况下,才会考 ...

  5. 大数据分析/机器学习基础之matplotlib绘图篇

    目录 一.前言 我的运行环境 二.什么是matplotlib? 三.安装及导入 四.matplotlib的使用 一.前言 本人因在学习基于python的机器学习相关教程时第一次接触到matplotli ...

  6. MySQL-mysqldump 报错:[ERROR] unknown variable 'local_infile=1'.

    版权声明:原创作品,谢绝转载!否则将追究法律责任. ----- 作者:kirin mysqldump: [ERROR] unknown variable 'local_infile=1'. 解决方法: ...

  7. 【外包杯】【报错】微信小程序 “[app.json 文件内容错误]app.json:未找到[“pages”][0]对应 pages/xx/xx.wxml(或其他)文件” 报错 的简单解决方案(已解决)

    问题的解决方案 删除components目录下的uni-link文件夹

  8. MongoDB (操作数据库,操作集合,操作文档)的笔记

    https://www.bilibili.com/video/BV1gV411H7jN/?spm_id_from=333.999.0.0&vd_source=92305fa48ea41cb7b ...

  9. 数字孪生和GIS结合能为智慧社区带来怎样的改变?

    数字孪生和地理信息系统(GIS)是当今智慧社区发展中的两个重要技术,它们的结合将为智慧社区带来根本性的改变和巨大的发展机遇.这种结合将深刻影响社区的规划.建设.运营和管理,为居民创造更智能.便利.宜居 ...

  10. Android WebView 缓存处理

    加载html时,会在data/应用下生成database和cache两个文件夹:请求的url存在webviewcache.db下面,url的内容保存在webviewCache下面, Webview的两 ...