一种简化aws标签固定群集的工具
aws-tag-a-da的Python项目详细描述
AWS-每日标签
一个工具,它通过给一个标签添加一天标签来简化多个标签的固定。
安装
pip install aws-tag-a-day
快速启动
# Generate configuration file mkdir -p ~/.config/tagaday/ cat > ~/.config/tagaday/config.yml <<EOYdynamodb-table-name: 'tag-proposals'dynamodb-table-region: 'eu-west-2'services:- rds- ec2- s3- emrregions:- us-east-1- us-west-1- eu-west-1- ap-southeast-1- ap-northeast-1required-tags:- Project- Owner- Name- Service- AvailabilityEOY# Create dynamodb table defined in the above config file.# If the table exists already, the utility will not overwrite it. tag-a-day-initialise # Start proposing tags tag-a-day
动机
处理批标记的工具很多,但帮助大规模填充空标记的工具不多。
建议的工作流程是:
- 创建配置文件。
- 通过运行
tag-a-day-initialise
创建dynamodb表。这不是破坏性操作,现有的表将不会被修改或删除。如果表已经存在,则实用程序将引发错误。 - 让一个或多个用户开始使用
tag-a-day
。 - 通过运行
tag-a-day-reconcile
协调任何不同或重复的标记,并讨论任何重复的标记建议。 - 对资源应用标记。
note:此实用程序尚未实现步骤4和5。
参考
支持的服务
配置文件&cli选项
Configuration File Key | Type | CLI Option | Type | Description |
---|---|---|---|---|
^{ | List | ^{ | Comma separated string | List of regions to audit resources in |
^{ | List | ^{ | Comma separated string | List of services to audit |
^{ | List | ^{ | Comma separated string | List of tags which must be present on all resources |
^{ | String | ^{ | String | Name of the DynamoDB table to propose tags to. If used in conjunction with ^{ |
^{ | String | ^{ | String | AWS Region in which to look for ^{ |
^{ | Comma separated string | List of resource ids, to filter only for specific resources to propose tags for. | ||
^{ | String | File path pointing to a folder where tag profiles are located | ||
^{ | String | File name (including .yaml/.yml suffix) point to a particular tagging profile in ^{ |
延伸
aws-tag-a-day
是在插件体系结构上构建的,使用^{entry_point
。
若要添加更多标记处理程序,可以向此repo添加类,也可以使用
拥有setup.py,并使用tag_a_day.tag_handlers
入口点连接到插件体系结构。
创建一个新类,继承自
tag_a_day.services.service.Service
fromtag_a_day.services.serviceimportServiceclassCustomTagHandler(Service):pass
为标记处理程序设置唯一的名称
fromtag_a_day.services.serviceimportServiceclassCustomTagHandler(Service):name='custom_handler'
创建两个stub方法,
resources
和handler
匹配下面的签名:fromtag_a_day.services.serviceimportServiceclassVpcTagHandler(Service):name='ec2_vpc'defresources(self,session):passdefhandler(self,resource,expected_tags,region,session,cache,proposals):pass
实现
resources(...)
以返回iterable。如果使用BOTO3资源, 看起来应该是:defresources(self,session):returnsession.resource('ec2').vpc.all()
如果使用boto3客户机,不要忘记实现分页,应该如下所示:
defresources(self,session):ec2=session.client('ec2')paginator=ec2.get_paginator('describe_vpcs')forpageinpaginator.paginate():forvpcinpage:yieldvpc
实现
handle(...)
以产生描述标记建议的有效负载(例如使用boto3.resources):defhandle(self,vpc,expected_tags,region,session,cache,proposals):# This boilerplate logic will handle checking the tags which have already been# evaluated for this user.evaluated_tags=self._progress.evaluated_tags(vpc.vpc_id)vpc_info,missing_tags= \ self._build_tag_sets(expected_tags,evaluated_tags,vpc.tags)# Check if the user has proposed values for all the missing tags ifself._progress.has_finished(vpc.vpc_id,expected_tags):# Print a skip messageself._skip(vpc.vpc_id)returnifany(missing_tags):# Print information about this resource, which could be useful # to provide context around tagging.self._print_table(("VpcID",vpc.vpc_id),*vpc_info)# Allow the user to skip auditing this resource ifself._user_skip():return# Build our user prompt to ask for new tagstag_prompt=self._build_tag_prompt(missing_tags)fortag_keyinmissing_tags:# Yield a proposal for a new tag key/value pair for the given# resource id.yield{'resource_id':vpc.vpc_id,'tag_key':tag_key,'tag_value':tag_prompt(tag_key),}
最后,通过扩展
setup.py
:fromsetuptoolsimportsetupsetup(...,entry_points={'tag_a_day.tag_handlers':['vpc = my_package.my_module:VPCTagHandler',],})