J'essaie de lister les instances sur les valeurs de tag de différentes clés de tag. Par exemple, une clé de tag - Environnement, une autre clé de tag - Rôle. Mon code est donné ci-dessous :
import argparse
import boto3
AWS_ACCESS_KEY_ID = '<Access Key>'
AWS_SECRET_ACCESS_KEY = '<Secret Key>'
def get_ec2_instances(Env,Role):
ec2 = boto3.client("ec2", region)
reservations = ec2.describe_instances(Filters={"tag:environment" : Env, "tag:role" : Role})
for reservation in reservations["Reservations"] :
for instance in reservation["Instances"]:
print "%s" % (instance.tags['Name'])
if __name__ == '__main__':
regions = ['us-east-1','us-west-1','us-west-2','eu-west-1','sa-east-1',
'ap-southeast-1','ap-southeast-2','ap-northeast-1']
parser = argparse.ArgumentParser()
parser.add_argument('Env', default="environment", help='value for tag:environment');
parser.add_argument('Role', default="role", help='value for tag:role');
args = parser.parse_args()
for region in regions: get_ec2_instances(args.Env, args.Role)
Après avoir exécuté ce script : python script.py arg1 arg2
Je reçois l'erreur suivante
Traceback (most recent call last):
File "script.py", line 27, in <module>
for region in regions: get_ec2_instances(args.Env, args.Role)
File "script.py", line 10, in get_ec2_instances
reservations = ec2.describe_instances(Filters={"tag:environment" : Env, "tag:role" : Role})
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 258, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 524, in _make_api_call
api_params, operation_model, context=request_context)
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 577, in _convert_to_request_dict
api_params, operation_model)
File "/usr/local/lib/python2.7/dist-packages/botocore/validate.py", line 270, in serialize_to_request
raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter Filters, value: {'tag:role': 'arg1', 'tag:environment': 'arg2'}, type: <type 'dict'>, valid types: <type 'list'>, <type 'tuple'>