Skip to content

Feature Request: random IP from CIDR range #517

@bschaatsbergen

Description

@bschaatsbergen

It would be nice to have a resource that generates random IPv4 or IPv6 addresses within a specified CIDR range.

Why:
I'm sure this can serve various purposes, as people are more creative than I am.

Use cases I can think of:

  • Helpful to combine with the terraform test framework, performing egress network connectivity tests against a set of generated IP addresses in a range I'm actively denying or allowing.

Example usage

Basic:

resource "random_ip" "example" {}

IPv4:

resource "random_ip" "example" {
  cidr_range   = "192.168.0.0/24"
}

IPv6:

resource "random_ip" "example" {
  cidr_range   = "2001:db8::/32"
}

Using a count:

resource "random_ip" "example" {
  count        = 5
  cidr_range   = "2001:db8::/32"
}

output "random_ipv6_addresses" {
  value = random_ip.example[*].result
}

Using a count followed by a distinct:

resource "random_ip" "example" {
  count        = 50
  cidr_range   = "192.168.1.0/28"
}

output "random_distinct_ipv4_addresses" {
  value = distinct(random_ip.example[*].result)
}

I'm would love to contribute this new resource if it aligns with the vision of this provider and is something we intend to support. :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions