通过数组参数创建 Terraform 条件资源
Terraform conditional resource creation by array parameters
我希望能够根据发送到模块的参数和变量数组中发送的值来创建资源,我是否会部署资源。
(伪代码)
var.example= ["get","post"]
if var.example.contains ("options") {
resource "aws_api_gateway_method" "api_gateway_method_options" {
http_method = "OPTIONS"
more_data...
}}
if var.example.contains ("post") {
resource "aws_api_gateway_method" "api_gateway_method_post" {
http_method = "POST"
more_data...
}}
if var.example.contains ("get") {
resource "aws_api_gateway_method" "api_gateway_method_get" {
authorization = "NONE"
http_method = "GET"
more_data...
}
}
我觉得很容易理解我想要什么,用 terraform 可以吗?
感谢大家
更新:感谢@Marcin @Luke2302
我将模块的完整代码与也不起作用的计数选项放在一起:
resource "aws_api_gateway_resource" "api_gateway_resource" {
parent_id = var.parent_id
path_part = var.rest_api_path_part
rest_api_id = var.api_gateway_rest_api.id
}
resource "aws_api_gateway_integration" "api_gateway_integration_post" {
count = contains(var.methods, "post") ? 1 : 0
rest_api_id = var.api_gateway_rest_api.id
resource_id = aws_api_gateway_resource.api_gateway_resource.id
http_method = aws_api_gateway_method.api_gateway_method_post.http_method
uri = var.lambda_invoke_arn
integration_http_method = "POST"
passthrough_behavior = "WHEN_NO_MATCH"
content_handling = "CONVERT_TO_TEXT"
type = "AWS_PROXY"
}
resource "aws_api_gateway_integration" "api_gateway_integration_get" {
count = contains(var.methods, "get") ? 1 : 0
cache_key_parameters = []
rest_api_id = var.api_gateway_rest_api.id
resource_id = aws_api_gateway_resource.api_gateway_resource.id
http_method = aws_api_gateway_method.api_gateway_method_get.http_method
uri = var.lambda_invoke_arn
integration_http_method = "POST"
passthrough_behavior = "WHEN_NO_MATCH"
type = "AWS_PROXY"
}
resource "aws_api_gateway_integration" "api_gateway_integration_options" {
count = contains(var.methods, "options") ? 1 : 0
rest_api_id = var.api_gateway_rest_api.id
resource_id = aws_api_gateway_resource.api_gateway_resource.id
http_method = aws_api_gateway_method.api_gateway_method_options.http_method
type = "MOCK"
request_templates = {
"application/json": "{\"statusCode\": 200}"
}
}
resource "aws_api_gateway_method" "api_gateway_method_options" {
count = contains(var.methods, "options") ? 1 : 0
authorization = "NONE"
http_method = "OPTIONS"
resource_id = aws_api_gateway_resource.api_gateway_resource.id
rest_api_id = var.api_gateway_rest_api.id
request_parameters = var.options_request_parameters
}
resource "aws_api_gateway_method" "api_gateway_method_post" {
count = contains(var.methods, "post") ? 1 : 0
authorization = "NONE"
http_method = "POST"
resource_id = aws_api_gateway_resource.api_gateway_resource.id
rest_api_id = var.api_gateway_rest_api.id
request_parameters = var.post_request_parameters
}
resource "aws_api_gateway_method" "api_gateway_method_get" {
count = contains(var.methods, "get") ? 1 : 0
authorization = "NONE"
http_method = "GET"
resource_id = aws_api_gateway_resource.api_gateway_resource.id
rest_api_id = var.api_gateway_rest_api.id
request_parameters = var.get_request_parameters
}
我收到这个错误:
错误:缺少资源实例密钥
on ../../modules/apigateway/apigateway_methods/main.tf line 13, in resource "aws_api_gateway_integration" "api_gateway_integration_post":
13: http_method = aws_api_gateway_method.api_gateway_method_post.http_method
Because aws_api_gateway_method.api_gateway_method_post has "count" set, its
attributes must be accessed on specific instances.
For example, to correlate with indices of a referring resource, use:
aws_api_gateway_method.api_gateway_method_post[count.index]
我认为以下内容应该适合您:
variable "example" {
default = ["get","post"]
}
resource "aws_api_gateway_method" "api_gateway_method_options" {
count = contains(var.example, "options") ? 1 : 0
http_method = "OPTIONS"
more_data...
}
resource "aws_api_gateway_method" "api_gateway_method_post" {
count = contains(var.example, "post") ? 1 : 0
http_method = "POST"
more_data...
}
resource "aws_api_gateway_method" "api_gateway_method_get" {
count = contains(var.example, "get") ? 1 : 0
authorization = "NONE"
http_method = "GET"
more_data...
}
我希望能够根据发送到模块的参数和变量数组中发送的值来创建资源,我是否会部署资源。 (伪代码)
var.example= ["get","post"]
if var.example.contains ("options") {
resource "aws_api_gateway_method" "api_gateway_method_options" {
http_method = "OPTIONS"
more_data...
}}
if var.example.contains ("post") {
resource "aws_api_gateway_method" "api_gateway_method_post" {
http_method = "POST"
more_data...
}}
if var.example.contains ("get") {
resource "aws_api_gateway_method" "api_gateway_method_get" {
authorization = "NONE"
http_method = "GET"
more_data...
}
}
我觉得很容易理解我想要什么,用 terraform 可以吗?
感谢大家
更新:感谢@Marcin @Luke2302 我将模块的完整代码与也不起作用的计数选项放在一起:
resource "aws_api_gateway_resource" "api_gateway_resource" {
parent_id = var.parent_id
path_part = var.rest_api_path_part
rest_api_id = var.api_gateway_rest_api.id
}
resource "aws_api_gateway_integration" "api_gateway_integration_post" {
count = contains(var.methods, "post") ? 1 : 0
rest_api_id = var.api_gateway_rest_api.id
resource_id = aws_api_gateway_resource.api_gateway_resource.id
http_method = aws_api_gateway_method.api_gateway_method_post.http_method
uri = var.lambda_invoke_arn
integration_http_method = "POST"
passthrough_behavior = "WHEN_NO_MATCH"
content_handling = "CONVERT_TO_TEXT"
type = "AWS_PROXY"
}
resource "aws_api_gateway_integration" "api_gateway_integration_get" {
count = contains(var.methods, "get") ? 1 : 0
cache_key_parameters = []
rest_api_id = var.api_gateway_rest_api.id
resource_id = aws_api_gateway_resource.api_gateway_resource.id
http_method = aws_api_gateway_method.api_gateway_method_get.http_method
uri = var.lambda_invoke_arn
integration_http_method = "POST"
passthrough_behavior = "WHEN_NO_MATCH"
type = "AWS_PROXY"
}
resource "aws_api_gateway_integration" "api_gateway_integration_options" {
count = contains(var.methods, "options") ? 1 : 0
rest_api_id = var.api_gateway_rest_api.id
resource_id = aws_api_gateway_resource.api_gateway_resource.id
http_method = aws_api_gateway_method.api_gateway_method_options.http_method
type = "MOCK"
request_templates = {
"application/json": "{\"statusCode\": 200}"
}
}
resource "aws_api_gateway_method" "api_gateway_method_options" {
count = contains(var.methods, "options") ? 1 : 0
authorization = "NONE"
http_method = "OPTIONS"
resource_id = aws_api_gateway_resource.api_gateway_resource.id
rest_api_id = var.api_gateway_rest_api.id
request_parameters = var.options_request_parameters
}
resource "aws_api_gateway_method" "api_gateway_method_post" {
count = contains(var.methods, "post") ? 1 : 0
authorization = "NONE"
http_method = "POST"
resource_id = aws_api_gateway_resource.api_gateway_resource.id
rest_api_id = var.api_gateway_rest_api.id
request_parameters = var.post_request_parameters
}
resource "aws_api_gateway_method" "api_gateway_method_get" {
count = contains(var.methods, "get") ? 1 : 0
authorization = "NONE"
http_method = "GET"
resource_id = aws_api_gateway_resource.api_gateway_resource.id
rest_api_id = var.api_gateway_rest_api.id
request_parameters = var.get_request_parameters
}
我收到这个错误: 错误:缺少资源实例密钥
on ../../modules/apigateway/apigateway_methods/main.tf line 13, in resource "aws_api_gateway_integration" "api_gateway_integration_post":
13: http_method = aws_api_gateway_method.api_gateway_method_post.http_method
Because aws_api_gateway_method.api_gateway_method_post has "count" set, its
attributes must be accessed on specific instances.
For example, to correlate with indices of a referring resource, use:
aws_api_gateway_method.api_gateway_method_post[count.index]
我认为以下内容应该适合您:
variable "example" {
default = ["get","post"]
}
resource "aws_api_gateway_method" "api_gateway_method_options" {
count = contains(var.example, "options") ? 1 : 0
http_method = "OPTIONS"
more_data...
}
resource "aws_api_gateway_method" "api_gateway_method_post" {
count = contains(var.example, "post") ? 1 : 0
http_method = "POST"
more_data...
}
resource "aws_api_gateway_method" "api_gateway_method_get" {
count = contains(var.example, "get") ? 1 : 0
authorization = "NONE"
http_method = "GET"
more_data...
}