I want to identify the public ip of the terraform execution environment and add it to the security group

前端 未结 3 459
心在旅途
心在旅途 2021-02-05 07:11

I want to identify the public IP of the terraform execution environment and add it to aws security group inbound to prevent access from other environments.

Currently, I

3条回答
  •  萌比男神i
    2021-02-05 08:00

    There's an easier way to do that without any scripts. The trick is having a website such as icanhazip.com which retrieve your IP, so set it in your terraform file as data:

    data "http" "myip" {
      url = "http://ipv4.icanhazip.com"
    }
    

    And whenever you want to place your IP just use data.http.myip.body, example:

    ingress {
      from_port = 5432
      to_port = 5432
      protocol = "tcp"
      cidr_blocks = ["${chomp(data.http.myip.body)}/32"]
    }
    
    • Note I used terraform chomp() method to remove any trailing space or new line which comes with body.

    • You can use your ipv6 with http://ipv6.icanhazip.com. Take care by just using http://icanhazip.com because it can retrieve ipv4 or ipv6

提交回复
热议问题