top of page
Search
  • Akmod

Harnessing the Power of Argument Binding in Cloud Infrastructure Management

Modern cloud infrastructure management often requires linking together multiple resources in an interdependent network. With idem, we have developed an innovative tool called Argument Binding which makes creating these interconnected environments a breeze.


Understanding Argument Binding

Argument Binding in idem is a feature that allows for the output of one state execution to be used as the parameters for another. This allows for the building of larger templates that can interweave parts of different cloud providers.


In essence, it determines the order of state execution in the structured layer state (SLS) file structure. An argument binding reference uses a specific format: ${<cloud>:<state>:<property_path>}, where <cloud> is the state cloud path reference, <state> is the state declaration ID, and <property_path> is a colon-delimited path to the property value.


Argument Binding in Action: AWS Example

To truly grasp the effectiveness of argument binding, let's delve into an example where it is utilized to create and manage resources on Amazon Web Services (AWS). The SLS file structure below outlines the creation of a Virtual Private Cloud (VPC), a subnet, and two EC2 instances.

Through argument binding, we can seamlessly connect the vpc_id to the subnet, and the subnet_id to the EC2 instances. Here's the detailed state:


default_vpc:
  exec.run:
    - path: aws.ec2.vpc.get
    - kwargs:
        filters:
          - name: is-default
            values:
              - "true"

default_subnet:
  exec.run:
    - path: aws.ec2.subnet.get
    - kwargs:
        filters:
          - name: vpc-id
            values:
              - ${exec:default_vpc:resource_id}

instance_1:
  aws.ec2.instance.present:
    - subnet_id: ${exec:default_subnet:resource_id}
    - tags:
      my_tag_key: my_tag_value

instance_2:
  aws.ec2.instance.present:
    - subnet_id: ${aws.ec2.instance:instance_1:subnet_id}
    - tags: ${aws.ec2.instance:instance_1:tags}

              

In this structure, the default_vpc state is executed first, retrieving the ID of the default VPC. The output from this state is then bound as an argument to the default_subnet state, which fetches the default subnet associated with the previously retrieved VPC ID.

The instance_1 state creates the first EC2 instance within the fetched subnet. We then leverage argument binding in the instance_2 state to link this instance to the same subnet and apply the same tags as instance_1. Further sls examples of argument binding can be found in the idem-aws repo.


Deep Dive into Argument Binding Syntax

The syntax of argument binding is a key aspect to understand for efficient utilization of this powerful feature. Here, we'll dissect its components in greater detail:

  1. <cloud>: The <cloud> element of the syntax might be slightly perplexing at first glance, as it doesn't reference a specific cloud provider. Instead, it points to a provider resource location on the hub, excluding "present" or "absent" states. Examples of such references include "aws.ec2.instance" for an AWS EC2 instance or just "exec" for an exec module call.

  2. <state>: The <state> element corresponds to the top-level key for the state you wish to bind to.

  3. <property_path>: The <property_path> is a powerful tool that extracts nested keys from a value in the "new_state" from the target state. For instance, you can write an argument binding statement to retrieve the network_card_index of instance_2 like this: ${aws.ec2.instance:instance_2:network_interfaces:0:network_card_index}.

By understanding these elements, you can leverage argument binding to interlink various states effectively, thereby enhancing the reusability and scalability of your SLS files.


Argument Binding and Enforced State Management

Argument binding is even more powerful when combined with Enforced State Management (ESM). ESM, which stores the output of previous Idem state runs in a database, enables argument binding to pull new_state values from previous runs.


This interaction is essential for building large complex states that interlink multiple resources without needing to hardcode resource_ids and other values. As such, argument binding contributes significantly to the reusability and templating of SLS files.


Conclusion

In conclusion, Argument Binding is a powerful feature in idem that enables complex interdependencies between different cloud resources to be defined and managed in a simple, efficient way. This not only reduces the complexity of cloud infrastructure management but also significantly enhances reusability of SLS files, leading to more efficient and effective cloud resource management.

22 views0 comments

Recent Posts

See All
bottom of page