Puppet Function: stdlib_aws::subnets_from_tag

Defined in:
lib/puppet/functions/stdlib_aws/subnets_from_tag.rb
Function type:
Ruby 4.x API

Overview

Returns a list of AWS subnets by Subnet ID for the given tag and optionally the tag’s value, and/or filtered by region.

Signatures:

  • stdlib_aws::subnets_from_tag(String $tag, Optional[String] $region)Array

    Returns a list of subnet IDs with the given tag.

    Examples:

    Retrieving the list of all AWS Subnet IDs for subnets with the given tag

    stdlib_aws::subnets_from_tag('created_by') => ['subnet-752ac411','subnet-752zx419']

    Parameters:

    • tag (String)

      The tag name.

    • region (Optional[String])

    Returns:

    • (Array)

      Returns an array of AWS Subnet IDs

  • stdlib_aws::subnets_from_tag(Hash $tagpair, Optional[String] $region)Array

    Returns a list of subnet IDs with the given tag and value(s).

    Examples:

    Retrieving the list of all AWS Subnet IDs for subnets with the given tag

    stdlib_aws::subnets_from_tag({created_by: 'puppet'}) => ['subnet-752ac411']

    Parameters:

    • tagpair (Hash)

      A hash containing a kv pair of the tag and desired value(s).

    • region (Optional[String])

    Returns:

    • (Array)

      Returns an array of AWS Subnet IDs



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/puppet/functions/stdlib_aws/subnets_from_tag.rb', line 4

Puppet::Functions.create_function(:'stdlib_aws::subnets_from_tag') do
  # Returns a list of subnet IDs with the given tag.
  # @param tag The tag name.
  # @return [Array] Returns an array of AWS Subnet IDs
  # @example Retrieving the list of all AWS Subnet IDs for subnets with the given tag
  #   stdlib_aws::subnets_from_tag('created_by') => ['subnet-752ac411','subnet-752zx419']
  dispatch :subnets_from_tag do
    param 'String', :tag
    optional_param 'String', :region
    return_type 'Array'
  end

  # Returns a list of subnet IDs with the given tag and value(s).
  # @param tagpair A hash containing a kv pair of the tag and desired value(s).
  # @return [Array] Returns an array of AWS Subnet IDs
  # @example Retrieving the list of all AWS Subnet IDs for subnets with the given tag
  #   stdlib_aws::subnets_from_tag({created_by: 'puppet'}) => ['subnet-752ac411']
  dispatch :subnets_from_tag_and_value do
    param 'Hash', :tagpair
    optional_param 'String', :region
    return_type 'Array'
  end

  def ec2_client(region)
    case region
    when nil
      Aws::EC2::Client.new
    when String
      Aws::EC2::Client.new({region: region})
    end
  end

  def subnets_from_tag(tag, region=nil)
    client = ec2_client(region)
    tag_filter = {
      filters: [
        {
          name: 'tag-key',
          values: [tag],
        },
      ],
    }

    subnets = client.describe_subnets(tag_filter).subnets
    subnets.map { |subnet| subnet.subnet_id }
  end


  def subnets_from_tag_and_value(tagpair, region=nil)
    client = ec2_client(region)

    tagname = tagpair.keys.first
    tagvalue = tagpair[tagname]

    tag_filter = {
      filters: [
        {
          name: "tag:#{tagname}",
          values: [tagvalue].flatten,
        },
      ],
    }

    subnets = client.describe_subnets(tag_filter).subnets
    subnets.map { |subnet| subnet.subnet_id }
  end
end