'Terraform: Pause creation of next instance of a resources
I have a null_resource resource to attach usage plan to api gateway in terraform with this configuration:
This will attach the same usage plan to multiple api gateways
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()
}
}
This is failing because of the limit on the number of UpdateUsagePlan
requests (1 every 20 seconds). Is there any alternative way of doing it?
Solution 1:[1]
First, I would like to recommend you to use the terraform resource to upload the usage plan.
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
}
}
Official reference: api_gateway_usage_plan
Now, if it is impossible to use the terraform resource for some reason and you must run a local-exec, you could try to add "sleep" before the command, for example:
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
}
I broke the variables above to make it easier to understand.
Set a base sleep time to 22 seconds (above 20).
Set index to the count.index and add 1 because the first index is 0.
Multiply both of them.
Sleep before running your code.
This way, it will always add additional 22 seconds sleep compared to the previous count.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Leo |