好的,所以我有三个.tf文件:main.tf我将 azure 声明为提供者,resources.tf声明我的所有资源的位置,以及variables.tf.
我variables.tf用来存储resources.tf.
但是,我想使用存储在我的变量文件中的变量来填充后端范围中的字段,如下所示:
main.tf:
provider "azurerm" {
version = "=1.5.0"
}
terraform {
backend "azurerm" {
storage_account_name = "${var.sa_name}"
container_name = "${var.c_name}"
key = "${var.key}"
access_key = "${var.access_key}"
}
}
Run Code Online (Sandbox Code Playgroud)
变量存储variables.tf如下:
variable "sa_name" {
default = "myStorageAccount"
}
variable "c_name" {
default = "tfstate"
}
variable "key" {
default = "codelab.microsoft.tfstate"
}
variable "access_key" {
default = "weoghwoep489ug40gu ... "
}
Run Code Online (Sandbox Code Playgroud)
我在运行时得到了这个terraform …
我对 Terraform 还是很陌生,尽管我已经浏览了 Hashicorp 网站上提供的所有教学模块。
目前,我正在努力理解如何设置环境变量。我知道如何在 main.tf config ( access_key = "${var.access_key}") 中引用变量,并且我知道如何将该访问密钥保存到一个单独的文件并引用它,但是我不明白(并且找不到任何文档/说明) 是如何设置环境变量,因此我不必将访问密钥保存到文件中。
有谁知道如何最好地做到这一点?
我一直在尝试通过 Terraform 管理 Azure Kubernetes 服务 (AKS) 实例。当我按照本 MS 教程通过 Azure CLI 创建 AKS 实例,然后按照本 MS 教程安装具有静态公共 IP 的入口控制器时,一切正常。此方法隐式创建服务主体 (SP)。
当我通过 Terraform 创建 AKS 集群的精确副本时,我被迫显式提供服务主体。我为这个新的 SP“贡献者”提供了对集群整个资源组的访问权限,当我进入创建入口控制器的步骤时(使用上面教程 2 提供的相同命令:)helm install stable/nginx-ingress --set controller.replicaCount=2 --set controller.service.loadBalancerIP="XX.XX.XX.XX",入口服务出现,但它从未启动获得其公共IP。IP 状态无限期地保持为“<pending>”,我在任何日志中都找不到任何有关原因的信息。是否有日志可以告诉我为什么我的 IP 仍处于待处理状态?
再次,我相当确定,除了 SP 之外,Terraform AKS 集群是基于 MS 教程创建的集群的精确副本。运行terraform plan发现两者没有区别。有谁知道我的 AKS SP 可能需要什么权限或者我在这里可能还缺少什么?奇怪的是,我找不到通过 Azure 门户分配给隐式创建的主体的任何权限,但我想不出任何其他可能导致此行为的原因。
不确定这是否是一个转移注意力的问题,但其他用户在针对第二个教程提出的问题中抱怨了类似的问题。他们的修复似乎总是“拆除集群并重试”,但这在这种情况下不是可接受的解决方案。我需要一个可重现的工作集群,并且azurerm_kubernetes_cluster目前不允许使用隐式创建的 SP 构建 AKS 实例。
azure terraform service-principal terraform-provider-azure azure-aks
我想引用另一个资源块中的 id subnet 2,但子网没有索引值。即“${azurerm_virtual_network.test.subnet.2.id}”将不起作用。
resource "azurerm_virtual_network" "test" {
name = "virtualNetwork1"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
address_space = ["10.0.0.0/16"]
dns_servers = ["10.0.0.4", "10.0.0.5"]
subnet {
name = "subnet1"
address_prefix = "10.0.1.0/24"
}
subnet {
name = "subnet2"
address_prefix = "10.0.2.0/24"
}
subnet {
name = "subnet3"
address_prefix = "10.0.3.0/24"
security_group = "${azurerm_network_security_group.test.id}"
}
}
Run Code Online (Sandbox Code Playgroud) 我想使用 Azure DevOps 管道部署我的 terraform 基础结构,但我遇到了存储帐户防火墙的问题。以下是存储帐户的示例:
resource "azurerm_storage_account" "storage_account" {
name = "mystorageaccount"
resource_group_name = "myresourcegroup"
...
network_rules {
default_action = "Deny"
bypass = ["AzureServices", "Logging"]
ip_rules = ["192.1.1.1"]
}
}
Run Code Online (Sandbox Code Playgroud)
存储帐户的初始创建成功,但由于防火墙规则,所有进一步操作(例如添加容器)都失败并出现未经授权的异常。
不幸的是,为“AzureServices”添加绕过规则不起作用。
我必须添加防火墙规则的原因是因为公司安全准则,所以我不能删除它。
有没有办法用 azure devops 处理存储帐户防火墙规则?
我对 Azure 和 Terraform 相当陌生,正在尝试使用 Terraform 为 Azure 服务主体创建一个秘密客户端。我无法弄清楚这一点。
这就是我现在所拥有的:
provider "azuread" {
version = "=0.7.0"
client_id = var.aws_client_id
subscription_id = var.aws_subscription_id
tenant_id = var.aws_tenant_id
client_secret = var.aws_client_secret
}
# Create an application
resource "azuread_application" "app" {
name = var.azurerd_app_name
}
# Create a service principal
resource "azuread_service_principal" "app" {
application_id = azuread_application.app.application_id
}
Run Code Online (Sandbox Code Playgroud)
这就是我正在尝试的(不太确定):
resource "random_string" "password" {
length = 32
special = true
}
# Create Service Principal password
resource "azuread_service_principal_password" "app" {
end_date = "2299-12-30T23:00:00Z" # Forever …Run Code Online (Sandbox Code Playgroud) azure azure-active-directory terraform terraform-provider-azure azure-service-principal
我有 github 操作工作流程,概述了启动 terraform 以在 Azure 中创建资源的简单过程。我缺少的是如何集成 terraform 状态文件,以便在顺序运行此工作流程时,它应该将当前状态与 main.tf 文件进行比较,并且只允许净更改。目前,如果我按顺序运行此命令,第二次总是会失败,因为资源已经在 Azure 中创建。
如何配置下面的 github 工作流程以允许 terraform 状态文件比较?,我还没有找到执行此操作的单个来源
github操作工作流程:
name: Terraform deploy to Azur
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: "Checkout"
uses: actions/checkout@master
- name: "Terraform Init"
uses: hashicorp/terraform-github-actions@master
with:
tf_actions_version: 0.12.13
tf_actions_subcommand: "init"
- name: "Terraform Plan"
uses: hashicorp/terraform-github-actions@master
with:
tf_actions_version: 0.12.13
tf_actions_subcommand: "plan"
args: -var="client_secret=${{ secrets.clientSecret }}"
-var="client_id=${{ secrets.clientId }}"
-var="tenant_id=${{ secrets.tenantId }}"
-var="sub=${{ secrets.sub }}"
- name: "Terraform Apply"
uses: hashicorp/terraform-github-actions@master
with: …Run Code Online (Sandbox Code Playgroud) 当我运行时,terraform apply -auto-approve出现以下错误:
Error: A resource with the ID "/subscriptions/.../resourceGroups/RG-SCUSTFStorage" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_resource_group" for more information.
Run Code Online (Sandbox Code Playgroud)
我知道我需要运行才能terraform import将资源导入到我的工作空间中。问题是我需要一次指定所有缺失资源的资源 ID。
有没有办法terraform import自动导入所有“已存在”资源,而无需一次输入一个资源 ID?
我已手动创建 Azure 虚拟网络和资源组。我将使用 Terraform 创建一个 VM,并通过 Azure DevOps 进行部署。我的 Terraform 状态文件驻留在 Azure 存储帐户中。目前,它是空白的。我需要将现有的虚拟网络和资源组导入到状态文件中。我能够使用发布管道进行 Terraform 初始化、规划和应用,但我无法在发布管道中找到导入 terraform 的任务。我可以使用 VSCode 进行导入,但需要使用 Azure DevOps 进行此导入。
有人可以告诉我这是否可能吗?提前致谢
我为 Mongodb terraform 模块创建了 main.tf 文件,如下所示。
resource "mongodbatlas_teams" "test" {
org_id = null
name = "MVPAdmin_Team"
usernames = ["user1@email.com", "user2@email.com", "user3@email.com"]
}
resource "mongodbatlas_project" "test" {
name = "MVP_Project"
org_id = null
teams {
team_id = null
role_names = ["GROUP_CLUSTER_MANAGER"]
}
}
resource "mongodbatlas_project_ip_access_list" "test" {
project_id = null
ip_address = null
comment = "IP address for MVP Dev cluster testing"
}
resource "mongodbatlas_cluster" "test" {
name = "MVP_DevCluster"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
cluster_type = REPLICASET
state_name = …Run Code Online (Sandbox Code Playgroud)