I am trying to create a jenkins job that when triggered will update all the Elasticache Redis replication groups with a certain tag.
The main workflow is that I find all the Redis replica groups in a region for example us-east-1
def findAllRedisReplicationGroups(label, region) {
query = "ReplicationGroups[*].{arn: ARN}"
output = sh(
script: "aws --region ${region} elasticache describe-replication-groups --query '${query}' --output text",
label: label,
returnStdout: true
)
return output
}
The output will be a string example here
String a = """
arn:aws:elasticache:us-west-2:AccountID:replicationgroup:application-2-test
arn:aws:elasticache:us-west-2:AccountID:replicationgroup:application-1-test
"""
Then I split the string into a list with each arn being an element.
Then using the for loop I will iterate through all the Redis replica groups and get their tags, if the tag is like Environment: test then the arn of the Redis replica group will be added to the list of arn
def findCorrectEnvReplicationGroups(label, region, environment, redis_arns){
def arn_list = redis_arns.split();
def correct_env_arn_list = [];
for( def arn : arn_list) {
def redisTags = getRedisTags(label, region, arn)
def jsonSlurper = new groovy.json.JsonSlurper()
def object = jsonSlurper.parseText(redisTags)
EnvironmentFromTag = object.TagList.find { it.Key == "Environment"}
if (EnvironmentFromTag.Value == environment) {
correct_env_arn_list.add(arn)
}
break
}
return correct_env_arn_list
}
def getRedisTags(label, region, arn) {
output = sh(
script: "aws --region ${region} elasticache list-tags-for-resource --resource-name ${arn} --output json",
label: label,
returnStdout: true
)
return output
}
I get through 1 loop. Tested by printing out the arn for each cycle but it crashes when trying to run the script in the getRedisTags method again.
The output should be a list of arns whose tags matches
Has anyone come across such an error or has any experience with groovy and can maybe help me out on why the jenkinsfile crashes when trying to run the aws cli command in a loop
many thanks