如何在Terraform中为“aws\U apigatewayv2\U route”添加对“aws\U apigatewayv2\U stage”的依赖关系?
我的地形设置如下:如何在Terraform中为“aws\U apigatewayv2\U route”添加对“aws\U apigatewayv2\U stage”的依赖关系?,terraform,terraform-provider-aws,Terraform,Terraform Provider Aws,我的地形设置如下: resource "aws_apigatewayv2_route" "signup_route" { api_id = "${aws_apigatewayv2_api.signup_redirect.id}" route_key = "POST /signup" target = "integrations/${aws_apigatewayv2_integration.lambda_integration.id}" } resource "aws_a
resource "aws_apigatewayv2_route" "signup_route" {
api_id = "${aws_apigatewayv2_api.signup_redirect.id}"
route_key = "POST /signup"
target = "integrations/${aws_apigatewayv2_integration.lambda_integration.id}"
}
resource "aws_apigatewayv2_stage" "staging_stage" {
api_id = "${aws_apigatewayv2_api.signup_redirect.id}"
name = "staging"
auto_deploy = true
route_settings {
route_key = "POST /signup"
logging_level = "INFO"
detailed_metrics_enabled = true
}
}
我在部署时遇到以下错误:
Error: error creating API Gateway v2 stage: NotFoundException: Unable to find Route by key POST /signup within the provided RouteSettings
似乎该阶段是在创建路线之前部署的。如何在stage上添加依赖项以依赖于
route
?它看起来不像aws\u apigatewayv2\u route
导出我们可以使用的任何有用属性。但是,依赖于
在这种情况下不起作用吗?-
resource "aws_apigatewayv2_stage" "staging_stage" {
depends_on = [aws_apigatewayv2_route.signup_route]
...
=====
(在这里编辑,因为我还没有足够的代表对另一个答案发表评论)我没有意识到您可以使用一个资源的输入作为属性。这很好,而且绝对是一种方法。在Terraform中创建依赖关系的最佳方法是编写对您想要依赖的资源的引用。在这种情况下,可能是这样的:
resource "aws_apigatewayv2_route" "signup_route" {
api_id = "${aws_apigatewayv2_api.signup_redirect.id}"
route_key = "POST /signup"
target = "integrations/${aws_apigatewayv2_integration.lambda_integration.id}"
}
resource "aws_apigatewayv2_stage" "staging_stage" {
api_id = aws_apigatewayv2_api.signup_redirect.id
name = "staging"
auto_deploy = true
route_settings {
route_key = aws_apigatewayv2_route.signup_route.route_key
logging_level = "INFO"
detailed_metrics_enabled = true
}
}
由于route\u settings
中的route\u键
指的是aws\u apigatewayv2\u路由。注册\u路由
,Terraform将其视为对该资源的依赖。让依赖项像这样隐含是很好的,因为它允许您专注于描述数据如何从一个资源传播到另一个资源,如果您稍后删除此route\u设置
块,那么它隐含的依赖项将自动删除,而无需记住更新某些其他声明
然而,在某些情况下,底层系统的设计使得这种明确的数据流依赖关系不可能实现。其中一个例子是AWS IAM角色,其中附加到角色的策略与角色本身是分开的,因此自然数据流推断的依赖关系是,将承担角色的策略和对象都依赖于角色,而承担角色的对象自然不依赖于策略。在这种情况下,我们往往需要添加额外的显式依赖项dependens\u on
,以确保系统在应用其策略之前不会尝试承担该角色:
resource "aws_iam_role" "for_lambda" {
name = "lambda_function"
assume_role_policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
})
}
resource "aws_iam_role_policy" "for_lambda" {
# (policy that the lambda function needs to do its work)
}
resource "aws_lambda_function" "example" {
name = "example"
# ...
# This reference makes the function depend on the role,
# but the role isn't ready to use until the associated
# policy has been attached to it too.
role = aws_iam_role.for_lambda.arn
# ...so we need to explicitly declare this hidden dependency:
depends_on = [aws_iam_role_policy.for_lambda]
}
在中有更多关于依赖关系如何在Terraform中工作的信息