Skip to content

create_rule async

create_rule(
    *,
    config_overrides: Optional[AsyncElasticLoadBalancingv2ClientConfig] = None,
    listener_arn: Optional[ListenerArn] = None,
    conditions: Optional[RuleConditionList] = None,
    priority: Optional[RulePriority] = None,
    actions: Optional[Actions] = None,
    tags: Optional[TagList] = None,
    transforms: Optional[RuleTransformList] = None,
) -> CreateRuleOutput

Creates a rule for the specified listener. The listener must be associated with an Application Load Balancer or a dual-stack Network Load Balancer.

Each rule consists of a priority, one or more actions, and one or more conditions. Rules are evaluated in priority order, from the lowest value to the highest value. When the conditions for a rule are met, its actions are performed. If the conditions for no rules are met, the actions for the default rule are performed. For more information, see Listener rules in the Application Load Balancers Guide or Listener rules in the Network Load Balancers Guide.

Parameters:

  • listener_arn (Optional[ListenerArn], default: None ) –

    The Amazon Resource Name (ARN) of the listener.

  • conditions (Optional[RuleConditionList], default: None ) –

    The conditions.

  • priority (Optional[RulePriority], default: None ) –

    The rule priority. A listener can't have multiple rules with the same priority.

  • actions (Optional[Actions], default: None ) –

    The actions.

  • tags (Optional[TagList], default: None ) –

    The tags to assign to the rule.

  • transforms (Optional[RuleTransformList], default: None ) –

    The transforms to apply to requests that match this rule. You can add one host header rewrite transform and one URL rewrite transform.

Raises:

Examples:

To create a rule This example creates a rule that forwards requests to the specified target group if the URL contains the specified pattern (for example, /img/*).

>>> await client.create_rule(listener_arn='arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2', conditions=[{'Field': 'path-pattern', 'Values': ['/img/*']}], priority=10, actions=[{'Type': 'forward', 'TargetGroupArn': 'arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067'}])
To create a listener rule that routes IPv4 traffic on a Network Load Balancer
This example creates a rule on a dual-stack Network Load Balancer that routes IPv4 source traffic to an IPv4 target group.
>>> await client.create_rule(listener_arn='arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/net/my-nlb/1234567890abcdef/1234567890abcdef', conditions=[{'Field': 'source-ip', 'SourceIpConfig': {'IpAddressType': 'ipv4'}}], priority=10, actions=[{'Type': 'forward', 'TargetGroupArn': 'arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-ipv4-targets/1234567890abcdef'}])
Source code in docs_src/capo_elastic_load_balancing_v2/_services/async_elastic_load_balancingv2.py
async def create_rule(
    self,
    *,
    config_overrides: Optional[AsyncElasticLoadBalancingv2ClientConfig] = None,
    listener_arn: Optional[
        "capo_elastic_load_balancing_v2.types.listener_arn.ListenerArn"
    ] = None,
    conditions: Optional[
        "capo_elastic_load_balancing_v2.types.rule_condition_list.RuleConditionList"
    ] = None,
    priority: Optional[
        "capo_elastic_load_balancing_v2.types.rule_priority.RulePriority"
    ] = None,
    actions: Optional[
        "capo_elastic_load_balancing_v2.types.actions.Actions"
    ] = None,
    tags: Optional["capo_elastic_load_balancing_v2.types.tag_list.TagList"] = None,
    transforms: Optional[
        "capo_elastic_load_balancing_v2.types.rule_transform_list.RuleTransformList"
    ] = None,
) -> "capo_elastic_load_balancing_v2.types.create_rule_output.CreateRuleOutput":
    """<p>Creates a rule for the specified listener. The listener must be associated with an Application Load Balancer or a dual-stack Network Load Balancer.</p> <p>Each rule consists of a priority, one or more actions, and one or more conditions. Rules are evaluated in priority order, from the lowest value to the highest value. When the conditions for a rule are met, its actions are performed. If the conditions for no rules are met, the actions for the default rule are performed. For more information, see <a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-listeners.html#listener-rules">Listener rules</a> in the <i>Application Load Balancers Guide</i> or <a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/network/load-balancer-listeners.html#listener-rules">Listener rules</a> in the <i>Network Load Balancers Guide</i>.</p>

    Args:
        listener_arn: <p>The Amazon Resource Name (ARN) of the listener.</p>
        conditions: <p>The conditions.</p>
        priority: <p>The rule priority. A listener can't have multiple rules with the same priority.</p>
        actions: <p>The actions.</p>
        tags: <p>The tags to assign to the rule.</p>
        transforms: <p>The transforms to apply to requests that match this rule. You can add one host header rewrite transform and one URL rewrite transform.</p>

    Raises:
        capo_elastic_load_balancing_v2.errors.incompatible_protocols_exception.IncompatibleProtocolsException: <p>The specified configuration is not valid with this protocol.</p>
        capo_elastic_load_balancing_v2.errors.invalid_configuration_request_exception.InvalidConfigurationRequestException: <p>The requested configuration is not valid.</p>
        capo_elastic_load_balancing_v2.errors.invalid_load_balancer_action_exception.InvalidLoadBalancerActionException: <p>The requested action is not valid.</p>
        capo_elastic_load_balancing_v2.errors.listener_not_found_exception.ListenerNotFoundException: <p>The specified listener does not exist.</p>
        capo_elastic_load_balancing_v2.errors.priority_in_use_exception.PriorityInUseException: <p>The specified priority is in use.</p>
        capo_elastic_load_balancing_v2.errors.target_group_association_limit_exception.TargetGroupAssociationLimitException: <p>You've reached the limit on the number of load balancers per target group.</p>
        capo_elastic_load_balancing_v2.errors.target_group_not_found_exception.TargetGroupNotFoundException: <p>The specified target group does not exist.</p>
        capo_elastic_load_balancing_v2.errors.too_many_actions_exception.TooManyActionsException: <p>You've reached the limit on the number of actions per rule.</p>
        capo_elastic_load_balancing_v2.errors.too_many_registrations_for_target_id_exception.TooManyRegistrationsForTargetIdException: <p>You've reached the limit on the number of times a target can be registered with a load balancer.</p>
        capo_elastic_load_balancing_v2.errors.too_many_rules_exception.TooManyRulesException: <p>You've reached the limit on the number of rules per load balancer.</p>
        capo_elastic_load_balancing_v2.errors.too_many_tags_exception.TooManyTagsException: <p>You've reached the limit on the number of tags for this resource.</p>
        capo_elastic_load_balancing_v2.errors.too_many_target_groups_exception.TooManyTargetGroupsException: <p>You've reached the limit on the number of target groups for your Amazon Web Services account.</p>
        capo_elastic_load_balancing_v2.errors.too_many_targets_exception.TooManyTargetsException: <p>You've reached the limit on the number of targets.</p>
        capo_elastic_load_balancing_v2.errors.too_many_unique_target_groups_per_load_balancer_exception.TooManyUniqueTargetGroupsPerLoadBalancerException: <p>You've reached the limit on the number of unique target groups per load balancer across all listeners. If a target group is used by multiple actions for a load balancer, it is counted as only one use.</p>
        capo_elastic_load_balancing_v2.errors.unsupported_protocol_exception.UnsupportedProtocolException: <p>The specified protocol is not supported.</p>
        capo_elastic_load_balancing_v2.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        To create a rule
        This example creates a rule that forwards requests to the specified target group if the URL contains the specified pattern (for example, /img/*).

        >>> await client.create_rule(listener_arn='arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2', conditions=[{'Field': 'path-pattern', 'Values': ['/img/*']}], priority=10, actions=[{'Type': 'forward', 'TargetGroupArn': 'arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067'}])
        To create a listener rule that routes IPv4 traffic on a Network Load Balancer
        This example creates a rule on a dual-stack Network Load Balancer that routes IPv4 source traffic to an IPv4 target group.

        >>> await client.create_rule(listener_arn='arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/net/my-nlb/1234567890abcdef/1234567890abcdef', conditions=[{'Field': 'source-ip', 'SourceIpConfig': {'IpAddressType': 'ipv4'}}], priority=10, actions=[{'Type': 'forward', 'TargetGroupArn': 'arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-ipv4-targets/1234567890abcdef'}])
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_elastic_load_balancing_v2.types.create_rule_input.CreateRuleInput]",
    ) -> AsyncOperationResponse[
        "capo_elastic_load_balancing_v2.types.create_rule_output.CreateRuleOutput"
    ]:
        import capo_elastic_load_balancing_v2._operations.elastic_load_balancing_v10.create_rule

        (
            output,
            http_response,
        ) = await capo_elastic_load_balancing_v2._operations.elastic_load_balancing_v10.create_rule.async_create_rule(
            req.options, req.input
        )
        return AsyncOperationResponse(output=output, response=http_response)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("CreateRule")
    )
    input_: capo_elastic_load_balancing_v2.types.create_rule_input.CreateRuleInput = {}
    if listener_arn is not None:
        input_["listener_arn"] = listener_arn
    if conditions is not None:
        input_["conditions"] = conditions
    if priority is not None:
        input_["priority"] = priority
    if actions is not None:
        input_["actions"] = actions
    if tags is not None:
        input_["tags"] = tags
    if transforms is not None:
        input_["transforms"] = transforms

    response = await aexecute_pipeline(
        AsyncOperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    await response.response.aclose()
    return response.output