Terraform:暂停创建资源的下一个实例



我有一个null_resource资源,可以使用这个配置将使用计划附加到terraform中的api网关:

这将把相同的使用计划附加到多个api网关

resource "null_resource" "usage_plan_attach" {
count = 4
provisioner "local-exec" {
# Run the script to attach this API to Usage Plan tiers.
command = "apigw-usageplan --api_id ${aws_api_gateway_rest_api.apigw[count.index].id} --stage ${aws_api_gateway_stage.stage[count.index].stage_name} --usage_plans ${jsonencode(var.api_gateway_usage_plans)}"
}
triggers = {
build_number = timestamp()
}
}

由于限制了UpdateUsagePlan请求的数量(每20秒1次),导致失败。有别的方法吗?

首先建议您使用地形资源上传使用方案

resource "aws_api_gateway_usage_plan" "example" {
name         = "my-usage-plan"
description  = "my description"
product_code = "MYCODE"
api_stages {
api_id = aws_api_gateway_rest_api.example.id
stage  = aws_api_gateway_stage.development.stage_name
}
api_stages {
api_id = aws_api_gateway_rest_api.example.id
stage  = aws_api_gateway_stage.production.stage_name
}
quota_settings {
limit  = 20
offset = 2
period = "WEEK"
}
throttle_settings {
burst_limit = 5
rate_limit  = 10
}
}

官方参考:api_gateway_usage_plan

现在,如果由于某些原因无法使用地形资源,而您必须运行local-exec,您可以尝试添加"sleep"命令前,例如:

provisioner "local-exec" {
command = <<-EOT
sleep_time=22
sleep_index=$((${count.index}+1))
sleep_total=$(($sleep_time*$sleep_index))
sleep $sleep_total
**Your code here***
EOT
}

为了更容易理解,我打破了上面的变量。

设置基本睡眠时间为22秒(高于20秒)。

设置索引为计数。

第一个索引为0。两者相乘

在运行代码前先休眠。

这样,与之前的计数相比,它总是会增加额外的22秒睡眠。

最新更新