1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
| import time
import boto3 import logging, os from multiprocessing import Process, Pool import botocore.exceptions from botocore.config import Config
class create_tag: def __init__(self, awsaccount, Key_Value): self.Key_Value = Key_Value self.awsaccount = awsaccount
def awsconfig(self, region): my_config = Config( region_name=region, signature_version='v4', connect_timeout=900, retries={ 'max_attempts': 10, 'mode': 'standard' } ) return my_config
def createTag(self, regions, resourcename, arn): tag_dict = self.Key_Value target_taglen = len(tag_dict) truenum = 0 flasenum = 0 for keys in tag_dict: for key in keys: if key in resourcename: truenum += 1 else: flasenum += 1 if truenum == target_taglen: self.logtrue(arn) if flasenum != 0: self.log_msg(regions, arn) client = boto3.client('resourcegroupstaggingapi', config=self.awsconfig(regions)) client.tag_resources( ResourceARNList=[ arn, ], Tags={ key: keys[key] } )
def ifNotCTag(self, regions, arn): self.log_NoTag(regions, arn) tag_dict = self.Key_Value client = boto3.client('resourcegroupstaggingapi', config=self.awsconfig(regions)) for keys in tag_dict: for key in keys: client.tag_resources( ResourceARNList=[arn, ], Tags={ key: keys[key] })
def get_regions(self): regionList = [] region = boto3.client('ec2') regions = region.describe_regions() for reg in range(0, len(regions['Regions'])): regionList.append(regions['Regions'][reg]['RegionName']) return regionList
def logsetting(self): return logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s: %(message)s')
def log_NoResource(self, regions, Service): self.logsetting() logging.info(f"该区域{regions}没有{Service}资源")
def log_msg(self, regions, Service): self.logsetting() logging.info(f"正在处理区域{regions}中的资源:{Service}进行打标签操作")
def logtrue(self, resourceid): self.logsetting() logging.info(f"资源{resourceid}标签合规")
def log_NoTag(self, regions, Service): self.logsetting() logging.info(f"正在处理区域{regions}中的资源:{Service}进行打标签操作")
def cdn_tag(self): client = boto3.client('cloudfront') distributions = client.list_distributions() lens = len(distributions['DistributionList']['Items']) for i in range(0, lens): cdn_arn = distributions['DistributionList']['Items'][i]['ARN'] self.ifNotCTag("us-east-1", cdn_arn)
def dynamodb_tag(self): reg = self.get_regions() for regions in reg: client = boto3.client("dynamodb", config=self.awsconfig(regions)) ddb = client.list_tables() if not ddb['TableNames']: self.log_NoResource(regions, "dynamodb") continue for table in ddb['TableNames']: response = client.describe_table( TableName=table ) ddb_arn = response['Table']['TableArn'] list_tag = client.list_tags_of_resource(ResourceArn=ddb_arn) if not list_tag['Tags']: self.ifNotCTag(regions, ddb_arn) else: keys_list = [] tag_len = len(list_tag['Tags']) for taglen in range(tag_len): keys = list_tag['Tags'][taglen]['Key'] keys_list.append(keys)
self.createTag(regions, keys_list, ddb_arn)
def ebs_tag(self): reg = self.get_regions() for regions in reg: client = boto3.client('ec2', config=self.awsconfig(regions)) response = client.describe_volumes() if not response["Volumes"]: self.log_NoResource(regions, "EBS") continue for volume in response['Volumes']: volumeid = volume['VolumeId'] ebs_arn = f"arn:aws:ec2:{regions}:{self.awsaccount}:volume/{volumeid}" if 'Tags' not in volume: self.ifNotCTag(regions, ebs_arn) else: tag_len = len(volume['Tags']) keys_list = [] for taglen in range(tag_len): keys = volume['Tags'][taglen]['Key'] keys_list.append(keys) self.createTag(regions, keys_list, ebs_arn)
def ec2_tag(self): reg = self.get_regions() for regions in reg: client = boto3.client('ec2', config=self.awsconfig(regions)) response = client.describe_instances() if not response['Reservations']: self.log_NoResource(regions, "ec2") continue for instance in response['Reservations']: instance_id = instance['Instances'][0]['InstanceId'] ec2_arn = f"arn:aws:ec2:{regions}:{self.awsaccount}:ec2/{instance_id}" if 'Tags' not in instance['Instances'][0]: self.ifNotCTag(regions, ec2_arn) else: tag_len = len(instance['Instances'][0]['Tags']) keys_list = [] for taglen in range(tag_len): keys = instance['Instances'][0]['Tags'][taglen]['Key'] keys_list.append(keys) self.createTag(regions, keys_list, ec2_arn)
def eip_tag(self): reg = self.get_regions() for regions in reg: client = boto3.client('ec2', config=self.awsconfig(regions)) response = client.describe_addresses() if not response['Addresses']: self.log_NoResource(regions, "EIP") continue for eip in response['Addresses']: eipallocationid = eip['AllocationId'] eip_arn = f'arn:aws:ec2:{regions}:{self.awsaccount}:eip/{eipallocationid}' if 'Tags' not in eip: self.ifNotCTag(regions, eip_arn) else: tag_len = len(eip['Tags']) keys_list = [] for taglen in range(tag_len): keys = eip['Tags'][taglen]['Key'] keys_list.append(keys) self.createTag(regions, keys_list, eip_arn)
def vpc_tag(self): req = self.get_regions() for regions in req: ec2_client = boto3.client('ec2', config=self.awsconfig(regions)) response = ec2_client.describe_vpcs() for vpcs in response['Vpcs']: vpcid = vpcs['VpcId'] vpc_arn = f'arn:aws:ec2:{regions}:{self.awsaccount}:vpc/{vpcid}' if 'Tags' not in vpcs: self.ifNotCTag(regions, vpc_arn) else: tag_len = len(vpcs['Tags']) keys_list = [] for taglen in range(tag_len): keys = vpcs['Tags'][taglen]['Key'] keys_list.append(keys) self.createTag(regions, keys_list, vpc_arn)
def elb_tag(self): reg = self.get_regions() for regions in reg: client = boto3.client("elbv2", config=self.awsconfig(regions)) response = client.describe_load_balancers() if not response['LoadBalancers']: self.log_NoResource(regions, "ELB") continue for elbs in response['LoadBalancers']: elb_arn = elbs['LoadBalancerArn'] tag_response = client.describe_tags(ResourceArns=[elb_arn, ]) if not tag_response['TagDescriptions'][0]['Tags']: self.ifNotCTag(regions, elb_arn) else: tag_len = len(tag_response['TagDescriptions'][0]['Tags']) keys_list = [] for taglen in range(tag_len): keys = tag_response['TagDescriptions'][0]['Tags'][taglen]['Key'] keys_list.append(keys) self.createTag(regions, keys_list, elb_arn)
def lambda_tag(self): req = self.get_regions() for regions in req: client = boto3.client('lambda', config=self.awsconfig(regions)) response = client.list_functions() if not response['Functions']: self.log_NoResource(regions, "Lambda") continue for function in response['Functions']: function_arn = function['FunctionArn'] tag_response = client.list_tags(Resource=function_arn) if not tag_response['Tags']: self.ifNotCTag(regions, function_arn) else: keys_list = [] for keys in tag_response['Tags']: keys_list.append(keys) self.createTag(regions, keys_list, function_arn)
def rds_tag(self):
req = self.get_regions() for regions in req: rdsclient = boto3.client('rds', config=self.awsconfig(regions)) response = rdsclient.describe_db_instances() if not response['DBInstances']: self.log_NoResource(regions, "RDS") continue for RDS in response['DBInstances']: DBInstanceArn = RDS['DBInstanceArn'] cluster_identifier = RDS['DBClusterIdentifier'] cluster_arn = f'arn:aws:rds:{regions}:{self.awsaccount}:cluster:{cluster_identifier}' response_tags = rdsclient.list_tags_for_resource(ResourceName=DBInstanceArn) self.ifNotCTag(regions, cluster_arn) if not response_tags['TagList']: self.ifNotCTag(regions, DBInstanceArn) else: tag_len = len(response_tags['TagList']) keys_list = [] for key in range(tag_len): keys = response_tags['TagList'][key]['Key'] keys_list.append(keys) self.createTag(regions, keys_list, DBInstanceArn)
def sqs_tag(self): req = self.get_regions() for regions in req: sqs_client = boto3.client('sqs', config=self.awsconfig(regions)) response = sqs_client.list_queues() if 'QueueUrls' not in response: self.log_NoResource(regions, "SQS") continue for sqs_url in response['QueueUrls']: account = sqs_url.split("/")[3] name = sqs_url.split("/")[4] sqs_arn = f"arn:aws:sqs:{regions}:{account}:{name}" response_tags = sqs_client.list_queue_tags(QueueUrl=sqs_url) if not response_tags['Tags']: self.ifNotCTag(regions, sqs_arn) else: keys_list = [] for keys in response_tags['Tags']: keys_list.append(keys) self.createTag(regions, keys_list, sqs_arn)
def glue_tag(self): req = self.get_regions() for regions in req: client = boto3.client('glue', config=self.awsconfig(regions)) response = client.list_crawlers() if 'Crawler' not in response: self.log_NoResource(regions, "glue") for gluename in response['CrawlerNames']: glue_arn = f"arn:aws:glue:{regions}:{self.awsaccount}:crawler/{gluename}" tag_response = client.get_tags(ResourceArn=glue_arn) if not tag_response['Tags']: self.ifNotCTag(regions, glue_arn) else: keys_list = [] for keys in tag_response['Tags']: keys_list.append(keys) self.createTag(regions, keys_list, glue_arn)
def glue_Jobtag(self): req = self.get_regions() for regions in req: client = boto3.client('glue', config=self.awsconfig(regions)) response = client.list_jobs() if 'Crawler' not in response: self.log_NoResource(regions, "glue") for gluename in response['JobNames']: job_arn = f"arn:aws:glue:{regions}:{self.awsaccount}:job/{gluename}" tag_response = client.get_tags(ResourceArn=job_arn) if not tag_response['Tags']: self.ifNotCTag(regions, job_arn) else: keys_list = [] for keys in tag_response['Tags']: keys_list.append(keys) self.createTag(regions, keys_list, job_arn)
def elasticache_cluster(self): req = self.get_regions() for regions in req: client = boto3.client("elasticache", config=self.awsconfig(regions)) response = client.describe_replication_groups() if not response['ReplicationGroups']: continue for i in response['ReplicationGroups']: clusters = i['ReplicationGroupId'] ecache_arn = f'arn:aws:elasticache:{regions}:{self.awsaccount}:replicationgroup:{clusters}' self.ifNotCTag(regions, ecache_arn)
def elasticache(self): req = self.get_regions() for regions in req: client = boto3.client("elasticache", config=self.awsconfig(regions)) response = client.describe_cache_clusters() if not response['CacheClusters']: self.log_NoResource(regions, "Elasitcache") continue for i in response['CacheClusters']: elasticache_arn = i['ARN'] tag_response = client.list_tags_for_resource(ResourceName=elasticache_arn) if not tag_response['TagList']: self.ifNotCTag(regions, elasticache_arn) else: tag_len = len(tag_response['TagList']) keys_list = [] for key in range(tag_len): keys = tag_response['TagList'][key]['Key'] keys_list.append(keys) self.createTag(regions, keys_list, elasticache_arn)
def ga_tag(self): client = boto3.client('globalaccelerator', region_name="us-west-2") response = client.list_accelerators() if not response['Accelerators']: self.log_NoResource("us-west-2", "GA") return for i in response['Accelerators']: ga_arn = i['AcceleratorArn'] tag_response = client.list_tags_for_resource(ResourceArn=ga_arn) if not tag_response['Tags']: self.ifNotCTag("us-west-2", ga_arn) else: tag_len = len(tag_response['Tags']) keys_list = [] for keys in range(tag_len): keys_list.append(tag_response['Tags'][keys]['Key']) self.createTag("us-west-2", keys_list, ga_arn)
def kms_tag(self): req = self.get_regions() for regions in req: client = boto3.client('kms', config=self.awsconfig(regions)) response = client.list_keys() if not response['Keys']: self.log_NoResource(regions, "KMS") continue for key in response['Keys']: kms_arn = key['KeyArn'] kms_id = key['KeyId'] tag_response = client.list_resource_tags(KeyId=kms_id) if not tag_response['Tags']: self.ifNotCTag(regions, kms_arn) else: tag_len = len(tag_response['Tags']) keys_list = [] for keys in range(tag_len): keys_list.append(tag_response['Tags'][keys]['TagKey']) self.createTag(regions, keys_list, kms_arn)
def kinesis_tag(self): req = self.get_regions() for regions in req: client = boto3.client('kinesis', config=self.awsconfig(regions)) response = client.list_streams() if not response['StreamNames']: self.log_NoResource(regions, "kinesis") continue for kinesis in response['StreamNames']: tag_response = client.list_tags_for_stream(StreamName=kinesis) response = client.describe_stream(StreamName=kinesis) kinesis_arn = response['StreamDescription']['StreamARN'] if not tag_response['Tags']: self.ifNotCTag(regions, kinesis_arn) else: tag_len = len(tag_response['Tags']) keys_list = [] for keys in range(tag_len): keys_list.append(tag_response['Tags'][keys]['Key']) self.createTag(regions, keys_list, kinesis_arn)
def firehose_tag(self): req = self.get_regions() for regions in req: client = boto3.client('firehose', config=self.awsconfig(regions)) response = client.list_delivery_streams() if not response['DeliveryStreamNames']: self.log_NoResource(regions, "firehose") continue for i in response['DeliveryStreamNames']: tag_response = client.list_tags_for_delivery_stream(DeliveryStreamName=i) mresponse = client.describe_delivery_stream(DeliveryStreamName=i) firehose_arn = mresponse['DeliveryStreamDescription']['DeliveryStreamARN'] if not tag_response['Tags']: self.ifNotCTag(regions, firehose_arn) else: tag_len = len(tag_response['Tags']) keys_list = [] for keys in range(tag_len): keys_list.append(tag_response['Tags'][keys]['Key']) self.createTag(regions, keys_list, firehose_arn)
def pinpoint_tag(self): req = ["us-east-1", "us-west-2", "ap-south-1", "ap-northeast-2", "ap-southeast-1", "ap-northeast-1", "ca-central-1", "eu-central-1", "eu-west-1", "eu-west-2"] for regions in req: client = boto3.client('pinpoint', config=self.awsconfig(regions)) response = client.get_apps() if 'ApplicationsResponse' not in response: self.log_NoResource(regions, "pinpoint") continue msg = response['ApplicationsResponse'] for pinpoint in msg['Item']: pin_arn = pinpoint['Arn'] if 'tags' not in pinpoint: self.ifNotCTag(regions, pin_arn) else: tag_len = len(pinpoint['tags']) keys_list = [] for taglen in range(tag_len): keys = pinpoint['tags'] for keyvalue in keys: keys_list.append(keyvalue) self.createTag(regions, keys_list, pin_arn)
def redshift_tag(self): req = self.get_regions() for regions in req: client = boto3.client('redshift', config=self.awsconfig(regions)) response = client.describe_clusters() if not response['Clusters']: self.log_NoResource(regions, "redshift") continue response_len = len(response['Clusters']) for redshitlen in range(response_len): ClusterIdentifier = response['Clusters'][redshitlen]['ClusterIdentifier'] red_arn = f"arn:aws:redshift:{regions}:{self.awsaccount}:cluster:{ClusterIdentifier}" if not response['Clusters'][redshitlen]['Tags']: self.ifNotCTag(regions, red_arn) else: tag_len = len(response['Clusters'][redshitlen]['Tags']) keys_list = [] for taglen in range(tag_len): keys = response['Clusters'][redshitlen]['Tags'][taglen]['Key'] keys_list.append(keys) self.createTag(regions, keys_list, red_arn)
def secretsmanager_tag(self):
req = self.get_regions() for regions in req: client = boto3.client('secretsmanager', config=self.awsconfig(regions)) response = client.list_secrets() if not response['SecretList']: self.log_NoResource(regions, "Secretsmanager") continue for secretsmanager in response['SecretList']: SecretList_arn = secretsmanager['ARN'] if not secretsmanager['Tags']: self.ifNotCTag(regions, SecretList_arn) else: tag_len = len(secretsmanager['Tags']) keys_list = [] for taglen in range(tag_len): keys = secretsmanager['Tags'][taglen]['Key'] keys_list.append(keys) self.createTag(regions, keys_list, SecretList_arn)
def kafka(self):
req = self.get_regions() for regions in req: client = boto3.client('kafka', config=self.awsconfig(regions)) response = client.list_clusters() if not response['ClusterInfoList']: self.log_NoResource(regions, "Kafka") continue for kafka in response['ClusterInfoList']: kafka_arn = kafka['ClusterArn'] if not kafka['Tags']: self.ifNotCTag(regions, kafka_arn) else: keys_list = [] for keyvalue in kafka['Tags']: keys_list.append(keyvalue) self.createTag(regions, keys_list, kafka_arn)
def ses_tag(self): req = self.get_regions() for regions in req: client = boto3.client('sesv2', config=self.awsconfig(regions)) response = client.list_email_identities() if not response['EmailIdentities']: self.log_NoResource(regions, "ses") continue for ses_name in response['EmailIdentities']: sesv2_arn1 = f"arn:aws:ses:{regions}:{self.awsaccount}:identity/{ses_name['IdentityName']}" tag_response = client.list_tags_for_resource(ResourceArn=sesv2_arn1) if not tag_response['Tags']: self.ifNotCTag(regions, sesv2_arn1) else: tag_len = len(tag_response['Tags']) keys_list = [] for taglen in range(tag_len): keys = tag_response['Tags'][taglen]['Key'] keys_list.append(keys) self.createTag(regions, keys_list, sesv2_arn1)
def s3_tag(self): client = boto3.client('s3') response = client.list_buckets() regions = "ap-southeast-1" for bucket in response['Buckets']: s3_arn = f"arn:aws:s3:::{bucket['Name']}" try: tag_response = client.get_bucket_tagging(Bucket='aws-glue-assets-811983228092-us-east-1') except botocore.exceptions.ClientError: print(bucket['Name'], "does not have tags, adding tags") self.ifNotCTag(regions, s3_arn) else: keys_list = [] for tags in tag_response['TagSet']: keys_list.append(tags['Key']) self.createTag(regions, keys_list, s3_arn)
def main(self):
if __name__ == '__main__': startTime = time.time() process_list = [self.process1, self.process2, self.process3, self.process4, self.process5, self.process6, self.process7, self.process8] jobs = [] for i in process_list: p = Process(target=i) jobs.append(p) p.start() for i in jobs: i.join() endTime = time.time() try: self.kms_tag() except botocore.exceptions.ClientError as e: print(e) print("时间为:", endTime - startTime)
def process1(self): self.ec2_tag() self.ebs_tag()
def process2(self): self.cdn_tag() self.eip_tag()
self.lambda_tag() self.sqs_tag()
def process3(self): self.pinpoint_tag() self.redshift_tag() self.secretsmanager_tag() self.kafka()
def process4(self): self.ses_tag() self.ga_tag() self.s3_tag()
def process5(self): self.glue_tag() self.glue_Jobtag() self.dynamodb_tag()
def process6(self): self.elasticache() self.rds_tag() self.elasticache_cluster()
def process7(self): self.vpc_tag() self.elb_tag()
def process8(self): self.kinesis_tag() self.firehose_tag()
test1 = create_tag(awsaccount='你的aws账号id', Key_Value=[{"Key":"Value"},{...}]) test1.main()
|