During testing of HashiCorp Terraform plans, sometimes there is a need to create firewall rules that use your current public Internet IP address, for example for SSH access to the created instances.

There is an easy way to programmatically obtain the IP address using a Terraform External Data Source.

Create a shell script that obtains the IP address and returns the value in a json object.

whatismyip.sh

Call the external data source and use the output in a firewall rule. The example in aws_security_group_rules.tf shows how to use the public IP address in a AWS security rule created by Terraform to allow SSH access.

aws_security_group_rules.tf

Make sure to install jq utility and also make whatismyip.sh executable using:

Since you are now using an additional data source, the External Data Source, initialize Terraform using:

See more Terraform and Ansible examples:

Categories: TerraformTutorial

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of