我们尝试将动态名称应用于防火墙规则,以便使用基于实例组 URL 列表的 terraform 在 GCP 中打开 8089 和 8843。它没有获取该结果并向我们提供 url 中的最后一项,而是为我们提供 https:
tf:
#This is to resolve an error when deploying to nginx
resource "google_compute_firewall" "ingress" {
for_each = toset(google_container_cluster.standard-cluster.instance_group_urls)
description = "Allow traffic on ports 8843, 8089 for nginx ingress"
direction = "INGRESS"
name = element(split("/", each.key), length(each.key))
network = "https://www.googleapis.com/compute/v1/projects/${local.ws_vars["project-id"]}/global/networks/${local.ws_vars["environment"]}"
priority = 1000
source_ranges = google_container_cluster.standard-cluster.private_cluster_config.*.master_ipv4_cidr_block
target_tags = [
element(split("/", each.key), length(each.key))
]
allow {
ports = [
"8089",
]
protocol = "tcp"
}
allow {
ports …Run Code Online (Sandbox Code Playgroud) Terraform 是否有一种从主机名获取 IP 地址的简单方法?
像这样的东西
data "some_data" "fetch_ip" {
url = "https://google.com"
}
resource "null_resource" "temp" {
google_ip = data.some_data.fetch_ip.ip // ipv4: 123.123.123.123
}
Run Code Online (Sandbox Code Playgroud) 我有一张像这样的地图
variable "mysubnets" {
type = map(string)
default = {
"subnet1" = "10.1.0.0/24"
"subnet2" = "10.1.1.0/24"
}
}
Run Code Online (Sandbox Code Playgroud)
在我的模块中,我尝试将子网放置在同一 vpc 的不同可用区中
data "aws_availability_zones" "azs" {
state = "available"
}
resource "aws_subnet" "test-subnets" {
for_each = var.mysubnets
cidr_block = "${each.value}"
vpc_id = aws_vpc.myvpc.id
availability_zone = data.aws_availability_zones.azs.names[index("${each.value}")]
tags = {
Name = "${each.key}"
}
}
Run Code Online (Sandbox Code Playgroud)
我可以从地图中获取键和值,没有问题,但是当尝试选择可用区域时,我找不到如何更改值。有没有办法获取地图的索引,或者为递增的数字创建一个计数器?
我们在 Azure 中的 terraform 远程状态文件已被完全破坏,我们现在面临着从头开始重新创建状态文件的挑战。我的选择是使用 Terraform 导入命令,使用以下简单语法:
terraform import <Terraform Resource Name>.<Resource Label> <Azure Resource ID>
Run Code Online (Sandbox Code Playgroud)
例如,要导入现有资源组,我将在 main.tf 文件中创建以下配置。
provider "azurerm" {
version="1.39.0"
}
# create resource group
resource "azurerm_resource_group" "rg"{
name = "rg-terraform"
location = "uksouth"
}
Run Code Online (Sandbox Code Playgroud)
现在,我遇到的问题如下:
当现有的 Azure 资源最初创建时,它们被分配的名称使用了极其复杂的命名约定,有些字符甚至是随机生成的。更复杂的是,它们都是独一无二的,而且有数百个。如果为它们分配一个像“main”这样的简单名称(正如许多 Terraform 示例中常用的那样),那么一切都会很美好,但不幸的是,情况并非如此。
因此,我的问题是:
将我的 main.tf 配置文件放在一起以用于导入时,是否绝对要求我的“资源标签”(在导入命令中给出)必须与创建资源时的原始“资源标签”名称相匹配?
如果这是强制性要求,是否有任何方法可以从 Azure 检索原始“资源标签”,就像我可以从 Azure 门户甚至 Az CLI 查询获取“Azure 资源 ID”一样?
如何确保导入中包含任何子资源(例如子网),而无需手动通过 Azure 门户进行搜索来识别其中的每一项?
对任何 beta 参数定义的任何 Google Cloud Platform (GCP) 资源进行地形改造都需要提供google-beta程序。是否应该google-beta使用提供者来代替或与提供者一起使用google提供程序一起使用?
换句话说,假设$GKE_CLUSTER_NAMEGCP 项目中存在某个 Google Kubernetes Engine (GKE) 集群$GCP_PROJECT_NAME某个 Google Kubernetes Engine (GKE) 集群:
gcloud container clusters list \\\n--format="value(name)" \\\n--project=$GCP_PROJECT_NAME\n\n#=>\n\n. . .\n$GKE_CLUSTER_NAME\n. . .\nRun Code Online (Sandbox Code Playgroud)\n启用配置连接器后:
\ngcloud container clusters describe $GKE_CLUSTER_NAME \\\n--format=\xe2\x80\x9cvalue(addonsConfig.configConnectorConfig.enabled)\xe2\x80\x9d \\\n--zone=$GKE_CLUSTER_ZONE\n\n#=>\n\nTrue\nRun Code Online (Sandbox Code Playgroud)\nTerraforming$GKE_CLUSTER_NAME需要一个google_container_cluster资源定义,container_cluster.tf其中包括config_connector_config参数(在addons_config块内;更多信息请参见此处)和provider参数(官方参考文档中缺少):
resource "google_container_cluster" "test" {\n addons_config {\n config_connector_config {\n enabled …Run Code Online (Sandbox Code Playgroud) google-cloud-platform google-kubernetes-engine terraform terraform-provider-gcp infrastructure-as-code
我正在使用 Terraform 创建 IAM 和 EC2,如下所示。
我想将一个名为ec2_roleEC2 实例配置文件的角色附加到该 EC2 实例配置文件中。但它似乎只能附加由 . 创建的一个aws_iam_instance_profile。
resource "aws_instance" "this" {
# ..
iam_instance_profile = aws_iam_instance_profile.this.name
}
resource "aws_iam_instance_profile" "this" {
name = "ec2-profile"
role = aws_iam_role.ec2_role.name
}
Run Code Online (Sandbox Code Playgroud)
关于ec2_role,它使用ec2_role_policy. 但如果我设置source_json = data.aws_iam_policy.amazon_ssm_managed_instance_core.policy为data "aws_iam_policy_document" "ec2_role_policy" {,它会引发错误。
resource "aws_iam_role" "ec2_role" {
name = "ec2-role"
assume_role_policy = data.aws_iam_policy_document.ec2_role_policy.json
}
resource "aws_iam_policy" "ec2_policy" {
name = "ec2-policy"
policy = data.aws_iam_policy_document.ec2_use_role_policy.json
}
resource "aws_iam_role_policy_attachment" "attach" {
role = …Run Code Online (Sandbox Code Playgroud) 我正在 AWS 中定义一个任务,我已经使用aws_ecs_task_definition模块来工作该任务。我正在使用模块中的环境变量设置一些环境变量terraform,但其中一些将通过 提供AWS SSM。没有的正常创建AWS SSM是:
environment : [
{
name : "user",
value : "name"
},
],
Run Code Online (Sandbox Code Playgroud)
这就像一个魅力。
然后我尝试:
environment : [
{
name : "user",
valueFrom : "my_env_var_name_in_ssm"
},
],
Run Code Online (Sandbox Code Playgroud)
但这不起作用。当我转到任务定义的 UI 时,该ENV变量不存在,也不存在于 UI 的 json 定义中。
然后我尝试在 UI 中创建它们,并且任务工作完美,我看到当您设置 valueFrom 时,变量ENV是在 json 定义的 Secrets 部分下创建的。所以我尝试将其复制为Terraform:
secrets : [
{
name : "user",
valueFrom : "my_env_var_name_in_ssm"
},
],
Run Code Online (Sandbox Code Playgroud)
但它也不起作用。任务定义 json 为:
{
"ipcMode": null, …Run Code Online (Sandbox Code Playgroud) 我们在多个服务内的多个脚本中使用 terraform 命令,并且使用文件或 AWS CloudWatch 访问这些脚本的输出,因此我们看到的是一堆令人困惑的颜色字符分布在几乎所有 terraform 输出行中。我希望禁用这些字符,但 github 上的讨论线程不太清楚当前禁用所有 terraform 命令的颜色输出的最佳方法是什么(例如: https: //github.com/hashicorp/terraform/issues/ 15264 )
我的问题是,如何在 2021 年 9 月最好地解决这个问题?我们目前使用的是 terraform 版本0.14.0,很快就会升级到 版本1.x,因此最好使用两个版本的解决方案,但如果只有1.x,我们可以考虑在升级时实现它。
我在下面的场景中尝试使用 terraform api_gateway 进行 POC。
path= /demo/user(GET) -> 调用 lamda 函数 (hello)。
path= /demo/user/{id)(put) -> 调用 lamda 函数(测试)。
所以我在这里创建了以下资源
resource "aws_api_gateway_rest_api" "MyDemoAPI" {
name = "MyDemoAPI"
description = "This is my API for demonstration purposes"
}
resource "aws_api_gateway_resource" "MyDemoResource" {
rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
parent_id = aws_api_gateway_rest_api.MyDemoAPI.root_resource_id
path_part = "demo"
}
resource "aws_api_gateway_integration" "MyDemoIntegration" {
rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
resource_id = aws_api_gateway_resource.MyDemoResource.id
http_method = aws_api_gateway_method.MyDemoMethod.http_method
type = "AWS_PROXY"
uri = "<lambda function arn>/invocation"
}
Run Code Online (Sandbox Code Playgroud)
在 terraform apply 中,它正在 /demo 下创建资源
,但这里如何实现路径?
path= …
我有一个输出两个值的模块,key并且value. 该模块包含第三个值is_needed,它是一个布尔值。我可以多次运行该模块并获取输出图。有没有办法根据is_needed布尔值有条件地将值添加到地图?
例如,我可以创建一个包含所有值的地图,如下所示:
locals {
map_of_values = tomap({
for instance in module.my_module : instance.key => instance.value
})
}
Run Code Online (Sandbox Code Playgroud)
我可以创建仅包含部分值的地图吗?类似于这个伪代码:
locals {
map_of_needed_values = tomap({
for instance in module.my_module if is_needed: instance.key => instance.value
})
}
Run Code Online (Sandbox Code Playgroud) terraform ×10
amazon-ec2 ×1
amazon-ecs ×1
amazon-iam ×1
azure ×1
azureportal ×1
docker ×1
element ×1
nginx ×1
split ×1