Skip to content

Commit 5440250

Browse files
dudymasgoruhamilldr
authored
fix: account-quota drift reduced (#1102)
Co-authored-by: Igor Rodionov <[email protected]> Co-authored-by: Dan Miller <[email protected]>
1 parent 4ce379e commit 5440250

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

modules/account-quotas/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,21 @@ aws --region us-east-1 service-quotas list-service-quotas --service-code ec2
3838
If you make a request to raise a quota, the output will show the requested value as `value` while the request is
3939
pending.
4040

41+
### Special usage Notes
42+
4143
Even though the Terraform will submit the support request, you may need to follow up with AWS support to get the request
4244
approved, via the AWS console or email.
4345

46+
#### Resources are destroyed on change
47+
48+
Because the AWS API often returns default values rather than configured or applicable values for a given quota, we have
49+
to ignore the value returned by the API or else face perpetual drift. To allow us to change the value in the future,
50+
even though we are ignoring it, we encode the value in the resource key, so that a change of value will result in a new
51+
resource being created and the old one being destroyed. Destroying the old resource has no actual effect (it does not
52+
even close an open request), so it is safe to do.
53+
54+
### Example
55+
4456
Here's an example snippet for how to use this component.
4557

4658
```yaml
@@ -128,5 +140,10 @@ components:
128140
- AWS CLI
129141
[command to list service codes](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/service-quotas/list-services.html):
130142
`aws service-quotas list-services`
143+
- AWS CLI
144+
[command to list service quotas](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/service-quotas/list-service-quotas.html)
145+
`aws service-quotas list-service-quotas`. Note where it says "For some quotas, only the default values are available."
146+
- [Medium article](https://medium.com/@jsonk/the-limit-does-not-exist-hidden-visibility-of-aws-service-limits-4b786f846bc0)
147+
explaining how many AWS service limits are not available.
131148

132149
[<img src="https://cloudposse.com/logo-300x69.svg" height="32" align="right"/>](https://cpco.io/component)

modules/account-quotas/main.tf

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ locals {
2121
quota_code = quota.quota_code != null ? quota.quota_code : data.aws_servicequotas_service_quota.by_name[k].quota_code
2222
value = quota.value
2323
} }
24+
25+
# Because the API often returns default values rather than configured or applicable values,
26+
# we have to ignore the value returned by the API or else face perpetual drift.
27+
# To allow us to change the value in the future, even though we are ignoring it,
28+
# we encode the value in the resource key, so that a change of value will
29+
# result in a new resource being created and the old one being destroyed.
30+
# Destroying the old resource has no actual effect, it does not even close
31+
# an open request, so it is safe to do.
32+
33+
quota_requests = { for k, quota in local.quotas_coded_map :
34+
format("%v/%v/%v", quota.service_code, quota.quota_code, quota.value) => merge(
35+
quota, { input_map_key = k }
36+
)
37+
}
38+
39+
quota_results = { for k, v in local.quota_requests : v.input_map_key => merge(
40+
{ for k, v in aws_servicequotas_service_quota.this[k] : k => v if k != "value" },
41+
{ "value reported (may be inaccurate)" = aws_servicequotas_service_quota.this[k].value },
42+
{ "value requested" = v.value }
43+
) }
2444
}
2545

2646
data "aws_servicequotas_service" "by_name" {
@@ -37,9 +57,15 @@ data "aws_servicequotas_service_quota" "by_name" {
3757
}
3858

3959
resource "aws_servicequotas_service_quota" "this" {
40-
for_each = local.quotas_coded_map
60+
for_each = local.quota_requests
4161

4262
quota_code = each.value.quota_code
4363
service_code = each.value.service_code
4464
value = each.value.value
65+
66+
lifecycle {
67+
# Literally about 50% of the time, the actual value set is not available,
68+
# so the default value is reported instead, resulting in permanent drift.
69+
ignore_changes = [value]
70+
}
4571
}

modules/account-quotas/outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
output "quotas" {
2-
value = aws_servicequotas_service_quota.this
2+
value = local.quota_results
33
description = "Full report on all service quotas managed by this component."
44
}

0 commit comments

Comments
 (0)