Azure Terraform(六)Common Module
一,引言
之前我们在使用 Terraform 构筑一下 Azure 云资源的时候,直接将所以需要创建的资源全面写在 main.tf 这个文件中,这样写主要是为了演示使用,但是在实际的 Terraform 代码的整个项目代码结构是需要封装具体的 “Module”,这里提到了 ”Module“ 也就是新的概念 “Common Module”。“Common Mudule” 其实对于开发来说,其实就是封装的 ”类库“,通过传递不同的参数,调用方法,来实现不同的返回值;同理,terraform 的 common moudle 也是一样的。
以下是 Terraform 项目结构
--------------------Azure Terraform 系列--------------------
1,Azure Terraform(一)入门简介
2,Azure Terraform(二)语法详解
3,Azure Terraform(三)部署 Web 应用程序
4,Azure Terraform(四)状态文件存储
5,Azure Terraform(五)利用Azure DevOps 实现自动化部署基础资源
6,Azure Terraform(六)Common Module
二,正文
1,Terraform 资源 Moudle 结构定义
按照资源划分,将每种类型的资源划分为单独的 “Common Moudle”,比方 “资源组”,“流量管理配置”,“Web 应用程序”......
mian.tf:不是项目的入口,该文件中包含了特定资源中的通用资源文件。
- resource "azurerm_traffic_manager_profile" "cnbate_traffic_manager_profile" {
- name = var.traffic_manager_name
- resource_group_name = var.resource_group_name
- traffic_routing_method = var.traffic_routing_method
- dns_config {
- relative_name = var.relative_name
- ttl = var.ttl
- }
- monitor_config {
- protocol = var.protocol
- port = var.port
- path = var.path
- interval_in_seconds = var.interval_in_seconds
- timeout_in_seconds = var.timeout_in_seconds
- tolerated_number_of_failures = var.tolerated_number_of_failures
- }
- tags = var.tags
- }
- resource "azurerm_traffic_manager_endpoint" "cnbate_traffic_manager_endpoint" {
- count = var.enable_traffic_manager_endpoint && var.traffic_manager_endpoint_count > 0 ? var.traffic_manager_endpoint_count : 0
- name = element(var.traffic_manager_endpoint_names, count.index)
- resource_group_name = var.resource_group_name
- profile_name = azurerm_traffic_manager_profile.cnbate_traffic_manager_profile.name
- target_resource_id = element(var.target_resource_ids, count.index)
- type = var.traffic_manager_endpoint_type
- geo_mappings = element(var.geo_mappings, count.index)
- }
outputs.tf :包含部署输出变量的定义
- ################################ traffic manager profile ################################
- output "traffic_manager_profile_name" {
- value = azurerm_traffic_manager_profile.cnbate_traffic_manager_profile.name
- }
- output "traffic_manager_profile_id" {
- value = azurerm_traffic_manager_profile.cnbate_traffic_manager_profile.id
- }
- ################################ traffic manager profile ################################
- output "azurerm_traffic_manager_endpoint_names" {
- value = azurerm_traffic_manager_endpoint.cnbate_traffic_manager_endpoint.*.name
- }
- output "azurerm_traffic_manager_endpoint_ids" {
- value = azurerm_traffic_manager_endpoint.cnbate_traffic_manager_endpoint.*.id
- }
variables.tf:包含了当前资源所封装的变量的定义
- ################################ traffic manager profile ################################
- variable "traffic_manager_name" {
- type = string
- description = "(required)The name of the traffic manager profile"
- }
- variable "resource_group_name" {
- type = string
- description = "The Name which should be used for this Resource Group. Changing this forces a new Resource Group to be created."
- }
- variable "traffic_routing_method" {
- type = string
- description = "(required) Specifies the algorithm used to route traffic"
- }
- variable "relative_name" {
- type = string
- description = "(required) The relative domain name, this is combined with the domain name used by Traffic Manager to form the FQDN which is exported as documented below."
- }
- variable "ttl" {
- type = number
- description = "(Required) The TTL value of the Profile used by Local DNS resolvers and clients"
- }
- variable "protocol" {
- type = string
- default = "http"
- description = " (required) The protocol used by the monitoring checks, supported values are HTTP, HTTPS and TCP."
- }
- variable "port" {
- type = number
- default = 80
- description = "(required) The port number used by the monitoring checks."
- }
- variable "path" {
- type = string
- default = "/"
- description = " (optional) The path used by the monitoring checks. Required when protocol is set to HTTP or HTTPS - cannot be set when protocol is set to TCP."
- }
- variable "interval_in_seconds" {
- type = number
- default = 30
- description = "(optional) The interval used to check the endpoint health from a Traffic Manager probing agent."
- }
- variable "timeout_in_seconds" {
- type = number
- default = 10
- description = "(optional) The amount of time the Traffic Manager probing agent should wait before considering that check a failure when a health check probe is sent to the endpoint. "
- }
- variable "tolerated_number_of_failures" {
- type = string
- default = 3
- description = "(optional) The number of failures a Traffic Manager probing agent tolerates before marking that endpoint as unhealthy. Valid values are between 0 and 9."
- }
- variable "tags" {
- type = map(string)
- description = "(optional) A mapping of tags to assign to the resource."
- }
- ################################ traffic manager endpoint ################################
- variable "enable_traffic_manager_endpoint" {
- type = bool
- default = false
- description = "(required) whether to create traffic manager endpoint"
- }
- variable "traffic_manager_endpoint_count" {
- type = number
- default = 0
- description = "(required) number of create traffic manager endpoint"
- }
- variable "traffic_manager_endpoint_names" {
- type = list(string)
- description = "(required) The name of the Traffic Manager endpoint."
- }
- variable "target_resource_ids" {
- type = list(string)
- description = " (optional) The resource id of an Azure resource to target. This argument must be provided for an endpoint of type azureEndpoints or nestedEndpoints."
- }
- variable "traffic_manager_endpoint_type" {
- type = string
- description = "(required) The Endpoint type, must be one of: 1:azureEndpoints,2:externalEndpoints,3:nestedEndpoints"
- }
- variable "geo_mappings" {
- type = list(list(string))
- description = "(Optional) A list of Geographic Regions used to distribute traffic, such as WORLD, UK or DE. "
- }
2,资源 Module 引用
将 terraform 项目所封装的 common module 在一个主 mian.tf 进行引用的时候,使用 module
根据模块的位置以及使用该模块的位置,该 source 参数可能有所不同
- module "cnbate_Web_app" {
- source = "../module/web_app"
- app_service_locations = [local.location_eastAsia, local.location_southeastAsia]
- resource_group_name = data.azurerm_resource_group.cnbate_resource_group.name
- enable = var.enable
- enable_app_service_plan = var.enable_app_service_plan
- app_service_plan_count = var.app_service_plan_count
- app_service_plan_names = var.app_service_plan_names
- app_service_plans = var.app_service_plans
- enable_app_service = var.enable_app_service
- app_service_count = var.app_service_count
- app_service_names = var.app_service_names
- app_settings = var.app_settings
- }
如果模块之间由相互依赖引用,则通过 “module” 引用的方式来建立关系,同时 terraform apply 在执行部署计划的时候,terraform 也会遵循这个依赖关系先后创建资源
- module "cnbate_traffic_manager" {
- source = "../module/traffic_manager_profile"
- traffic_manager_name = var.traffic_manager_name
- resource_group_name = data.azurerm_resource_group.cnbate_resource_group.name
- traffic_routing_method = var.traffic_routing_method
- relative_name = var.relative_name
- ttl = var.ttl
- tags = var.tags
- enable_traffic_manager_endpoint = var.enable_traffic_manager_endpoint
- traffic_manager_endpoint_count = var.traffic_manager_endpoint_count
- traffic_manager_endpoint_names = var.traffic_manager_endpoint_names
- target_resource_ids = module.cnbate_Web_app.azurerm_app_service_ids
- traffic_manager_endpoint_type = var.traffic_manager_endpoint_type
- geo_mappings = var.geo_mappings
- }
注意,一旦依赖关系在 common module 阶段发生改变的时候,就需要重新执行 terraform init 初始化操作,导入的所有模块的配置
3,如何划分 Terraform 资源模块
common module 的划分和封装没有固定的标准,我们在划分和封装的时候要从多个角度去考虑问题
1,项目太小,只有一个 web 应用程序,是否需要封装?
2,是否必须严格讲每个 terraform resource 划分成单独的 common module?
3,封装的 common module 以及 module 引用是否满足项目架构需求?
所以,划分、封装 common module 不是必须的。只要我们能够清楚的看到实际项目的整体架构需求,是否使用模块化都是有利有弊的。大家要清楚的看到这一点。
完整代码请参考文章底部的 github 链接
三,结尾
参考资料:Terraform 官方,azurerm 文档
Terraform_Cnbate_Traffic_Manager github:https://github.com/yunqian44/Terraform_Cnbate_Traffic_Manager
作者:Allen
版权:转载请在文章明显位置注明作者及出处。如发现错误,欢迎批评指正。
Azure Terraform(六)Common Module的更多相关文章
- Azure Terraform(七)利用Azure DevOps 实现自动化部署基础资源(补充)
一,引言 之前一篇文章有讲解到利用 利用Azure DevOps 实现自动化部署基础资源,当时 TF 代码没有针对 Azure 各个资源的封装,所有的资源代码全部写在一个 main.tf 文件中.然后 ...
- Azure Terraform(八)利用Azure DevOps 实现Infra资源和.NET CORE Web 应用程序的持续集成、持续部署
一,引言 上一篇讲解到利用 Azure DevOps 将整个 Azure Web App,Azure Traffic Manager profile,Azure Storage Account,Azu ...
- Azure Terraform(九)利用 Azure DevOps Pipeline 的审批来控制流程发布
一,引言 Azure Pipeline 管道是一个自动化过程:但是往往我们由于某种原因,需要在多个阶段之前获得批准之后再继续下一步流程,所以我们可以向Azure Pipeline 管道添加审批!批准流 ...
- Azure Terraform(十)利用 Azure DevOps 的条件语句选择发布环境
一,引言 之前我讲过的所有的案例中,都是将整个Azure Resource 部署到同一个订阅下,没有做到灵活的在 Azure Pipeline 在运行前选择需要部署的环境.在实际的项目开发中,我们也会 ...
- Azure Terraform(二)语法详解
一,引言 上篇文章开始,我们简单介绍了以下通过基础设施管理工具----- Terraform,通过它来统一管理复杂的云基础设施资源.作为入门演示,使用Terraform 部署Azure 资源组的方式直 ...
- terraform 配置github module source
terraform 支持多种module 的source 配置 以下是一个简单的使用github source的demo 测试项目 项目结构 ├── init.tpl ├── main.tf 代码 ...
- Java-Maven(十二):idea多项目:common module进行compiler和install正常,运行domain-perf module提示:Could not resolve dependencies for project
前提: product项目下有三个module,分别是: driver module domain-perf module common module 问题: driver 和 domain-perf ...
- Azure Terraform(三)部署 Web 应用程序
一,引言 上一节关于 Terraform 的文章讲到 Terraform 使用到的一些语法,以及通过演示使用 Terraform 在Azure 上部署资源组,极大的方便了基础设施实施人员,也提高了基础 ...
- Azure Terraform(五)利用Azure DevOps 实现自动化部署基础资源
一,引言 上一篇我们结合学习 Azure Traffic Manger 的内容,做了一个负载均衡的基础设施架构.通过 Terraform 部署执行计划,将整个 Azure Traffic Manage ...
随机推荐
- Android 7.0应用之间共享文件
原文首发于微信公众号:躬行之,欢迎关注交流! 开发中经常需要将某个文件向另一个应用程序传递,如图片上传到另一个应用程序.文件在不同存储路径之间的复制粘贴等都需要共享文件,可以这样理解接收文件的应用是在 ...
- .netcore 微服务快速开发框架 Anno&Viper 注册中心 (服务上线下线预警通知)
1.微服务时代,服务上线先预警通知 在微服务大行其道的今天,相信很多人都用上了微服务或者是微服务的概念也已经有了一个深刻的了解.今天我们不在这里展开阐述,今天我们要说的是微服务伴侣预警通知. 2.注册 ...
- 任务调度框架Quartz快速入门!
目录 Quartz是什么 Quartz中的重要API及概念 超重要API 重要概念 Quartz设计理念:为什么设计Job和Trigger? 最简单的Quartz使用案例 Job实例和JobDetai ...
- mysql数据安全之利用二进制日志mysqlbinlog恢复数据
mysql数据安全之利用二进制日志mysqlbinlog恢复数据 简介:如何利用二进制日志来恢复数据 查看二进制日志文件的内容报错: [root@xdclass-public log_bin]# my ...
- CentOS7下常用安装服务软件yum方式的介绍
简介:介绍yum软件包的管理并配置本地yum源 yum安装:基于 C/S 架构,yum安装称之为傻瓜式安装 yum安装优点:方便快捷,不用考虑包依赖,自动下载软件包. yum安装缺点:人为无法干预,无 ...
- [leetcode]242. Valid Anagram判断两个字符串是不是包含相同字符的重排列
/* 思路是判断26个字符在两个字符串中出现的次数是不是都一样,如果一样就返回true. 记住这个方法 */ if (s.length()!=t.length()) return false; int ...
- 微信小程序-页面下拉
微信小程序当滑动到最顶部和最底部时,继续下拉,会将整个页面拉下去或者拉上去,本来以为是客户端自有的特性,就没去管他,直到我的禅道出现了这个记录... 其实这个问题是可以解决的,只需要在你不想出现在此情 ...
- Hive基于UDF进行文本分词
本文大纲 UDF 简介 Hive作为一个sql查询引擎,自带了一些基本的函数,比如count(计数),sum(求和),有时候这些基本函数满足不了我们的需求,这时候就要写hive hdf(user de ...
- nohup命令说明-转载
转自:https://www.ibm.com/developerworks/cn/linux/l-cn-nohup/ 我们知道,当用户注销(logout)或者网络断开时,终端会收到 HUP(hangu ...
- VScode中配置C++运行环境
目录 VScode中配置C++运行环境 1. 哪些插件 2. 配置开始 3. 编写代码并运行 VScode中配置C++运行环境 关于安装mingw的教程,网络上已经有很多了,这里不再赘述,下面就看VS ...