At the moment, I need to retrieve the last 10 values of some parameters in AWS Parameters Store
I am using the following code in kotlin :
val p1 = retrieveAllValidVersions("P1")
val p2 = retrieveAllValidVersions("P2")
val p3 = retrieveAllValidVersions("P3")
Here is the code of the retrieveAllValidVersions
private fun retrieveAllValidVersions(paramName: String): List<ParameterHistory> {
val res = mutableListOf<ParameterHistory>()
val ssmClient = AWSSimpleSystemsManagementClientBuilder.defaultClient()
var nextToken : String? = null
do {
val ssmParams = ssmClient.getParameterHistory(GetParameterHistoryRequest()
.withName(paramName)
.withWithDecryption(true)
.withNextToken(nextToken)
)
res.addAll(ssmParams.parameters)
nextToken = ssmParams.nextToken
} while (nextToken != null)
return validVersions.sortedByDescending { it.version }.take(10)
}
As described in https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html#limits_ssm, the maximum number of versions for a parameter is 100
And as described by https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_GetParameterHistory.html, you can only retrieve 50 values in maxResults so I need to make 2 calls for each parameter (because I have more than 50 versions)
So each retrieval of my 3 parameters costs 6 queries to SSM
I cache the last 10 values of each param in memory for 5 minutes
The problem is that when multiple instances of my lambdas expire their cache at the same time, they do the retrieval at the same time and
com.amazonaws.services.simplesystemsmanagement.model.AWSSimpleSystemsManagementException: Rate exceeded (Service: AWSSimpleSystemsManagement; Status Code: 400; Error Code: ThrottlingException; Request ID: xxx)
For ex, if 3 instances are concerned, that will do 18 requests in less than a second and I will hit the error (note : I do not know exactly if the number of instances that hit this code at the same time is 3, this is just a guess to illustrate that at some point you hit the error)
So I have 2 questions :
First, is there a way to retrieve last versions of a parameter first ?
That way, I will then do half of the requests so I will hit the problem less often !
Second, how do I automatically retry a throttling error ?
I have find this AWS blog [1] post saying that I have to parse the error message but this is an old post (2013) and this is very ugly (the moment AWS changes the message, all the mechanism collapses) !
A final note : I am using the parameters store with "auto-encryption" and IAM, I do not want to store the parameters in my own database nor to cache them in a shared memory cache like redis !