首页 > 解决方案 > 为创建 ELB 规则而在 boto3 中传递的实际值是多少

问题描述

我正在研究一个用例,其中用户试图在 aws 中创建服务,该服务将从弹性负载均衡器中获取服务。负载均衡器已经存在,但我想使用 boto3 创建/更新规则方法为 ELBv2 创建新规则。该文档包含有关架构级别的信息。但我不确定在哪里编辑 json 中的有效规则。

Conditions=[
    {
        'Field': 'string',
        'Values': [
            'string',
        ],
        'HostHeaderConfig': {
            'Values': [
                'string',
            ]
        },
        'PathPatternConfig': {
            'Values': [
                'string',
            ]
        }]

如果负载均衡器具有 url:example.com - 我想将规则创建为“XYZ”,我可以在上面的字典中传递这些 XYZ。因此,如果我尝试访问example.com/XYZ,该 url 应该可以工作。

标签: amazon-web-servicesboto3botobotocore

解决方案


client = boto3.client('elbv2')
listener_arn = 'listener ARN'
response = client.describe_rules(ListenerArn=listener_arn)
is_rule_exists = False
for rule in response['Rules']:
    conditions = rule['Conditions']
    for condition in conditions:
        for value in condition['Values']:
            if value == 'file_pattern_you_want_to_choose':
                is_rule_exists = True
            else:
                print("Nope")

condition = [
    {
        'Field': 'path-pattern',
        'Values': [
            'path pattern value',
        ]
    }
]
actions = [{
    'Type': 'forward',
    'TargetGroupArn': 'target group arn for your instance'
}]

priority = 10

if is_rule_exists:
    print('Do Nothing. Or Else create the rule')
    client.modify_rule(ListenerArn=listener_arn, conditions=condition, actions=actions)
else:
    print('Create the rule')
    client.create_rule(ListenerArn=listener_arn, Conditions=condition, Actions=actions, Priority=priority)

以上是python代码片段,我用来获取文件路径模式并检查规则是否存在。如果没有,它将创建规则。否则,您可以修改规则。


推荐阅读