没有项目描述
aws-autodiscovery-templater的Python项目详细描述
AWS自动发现模板程序
aws中的自动发现对于遗留应用程序来说可能很棘手。由于时间 或者复杂性,编写自动发现代码并不总是可能的。 让应用程序在弹性/伸缩性环境中找到彼此。 有时,在配置文件中列出主机的IP/主机 够了,这个剧本就是这么做的。
安装
安装很简单
$ pip install aws-autodiscovery-templater
用法
最简单的方法是使用附带的cli工具(尽管 您可以使用的小python库)。此项目使用jinja2 templating engine,因此您可以找到 关于如何编写模板的详细信息。以下变量是 传递给模板引擎:
{ "private": { "ips": [...], "hostnames": [...] }, "public": { "ips": [...], "hostnames": [...] }, "reservations": { ... } }
前两个条目包含public和 私有、IP和主机名。reservations条目包含raw 输出 EC2.Client.describe_instances。
由此,我们可以创建一个如下所示的模板
# /path/to/config.yaml {% if private['ips'] %} private_ips:{% for ip in private['ips'] %} - {{ ip }}{% endfor %}{% endif %}
我们将在下面的示例中使用此模板。
注意:在运行其中任何一个之前,您需要有您的aws 在~/.aws/{config,credentials}或^{tt2}中设置的凭据$ 环境变量。
示例运行
$ aws-autodiscovery-templater \ --template-path /path/to/config.yaml \ # Path to jina2 formatted template --profile my-aws-profile \ # AWS credentials as defined in ~/.aws/credentials --stdout \ # Print result to stdout --filter-empty # Don't include null/missing values (eg. not all machines have public IPs
过滤实例
更重要的是,可以根据实例的标记筛选实例。这个 filter是一个json对象,其结构与 aws ec2 describe-instances, 排除根["Reservations"]键。
$ aws-autodiscovery-templater \ --template-path /path/to/config.yaml \ --profile my-aws-profile \ --stdout \ --filter-empty --filter "Name=tag:Application,Values=[mongodb,mongodb-master]" private_ips: - 10.0.0.20 - 10.0.0.10 - 10.0.0.30
内联帮助:
# For more details, have a look at the --help option $ aws-autodiscovery-templater --help usage: aws-autodiscovery-templater [-h] [--aws-access-key-id AWS_ACCESS_KEY_ID] [--aws-secret-access-key AWS_SECRET_ACCESS_KEY] [--aws-session-token AWS_SESSION_TOKEN] [--region REGION] [--profile PROFILE] [--role ROLE] [--auth-debug] [--role-session-name ROLE_SESSION_NAME] --template-path TEMPLATE_PATH --stdout [--vpc-ids] [--filter FILTER] [--filter-empty] optional arguments: -h, --help show this help message and exit AWS credentials: --aws-access-key-id AWS_ACCESS_KEY_ID AWS access key --aws-secret-access-key AWS_SECRET_ACCESS_KEY Access and secret key variables override credentials stored in credential and config files --aws-session-token AWS_SESSION_TOKEN A session token is only required if you are using temporary security credentials. --region REGION This variable overrides the default region of the in- use profile, if set. --profile PROFILE This can be the name of a profile stored in a credential or config file, or default to use the default profile. --role ROLE Fully qualified role arn to assume --auth-debug Enter debug mode, which will print credentials and then exist at `create_session`. --role-session-name ROLE_SESSION_NAME If you have assigned a role, set a --role-session-name AWS Autodiscovery Templater: --template-path TEMPLATE_PATH Path to the template to fill variables into. --stdout Prints a json object containing the retrieves resources --vpc-ids Optionally restrict the filtering to a particular list of IPs. Comma seperated list of vpc-ids. --filter FILTER Filter for ec2 instances as defined in http://boto3.re adthedocs.org/en/latest/reference/services/ec2.html#EC 2.Client.describe_instances --filter-empty By default, missing values are returned as null to keep private/public ip/hostname sets of equal length. This removes null values from the filter