Skip to content

Terraform External Provider and Datasource

Step-01: Introduction

Step-02: Review Terraform Configs

  • Files were copied from 11-01-Terraform-Azure-Linux-Virtual-Machine
  • c1-versions.tf
  • c2-resource-group.tf
  • c3-virtual-network.tf
  • c4-linux-virtual-machine.tf
  • c5-external-datasource.tf
  • app-scripts/app1-cloud-init.txt
  • shell-scripts/ssh_key_generator.sh

Step-03: c4-linux-virtual-machine.tf

  • public_key argument will be changed with External Datasource Value.
# Before
  admin_ssh_key {
    username = "azureuser"
    public_key = file("${path.module}/ssh-keys/terraform-azure.pub")
  }
# After
  admin_ssh_key {
    username = "azureuser"
    public_key = data.external.ssh_key_generator.result.public_key
  }

Step-04: Execute Terraform Commands

# Terraform Initialize
terraform init

# Terraform Validate
terraform validate

# Terraform Plan
terraform plan

# Observation
1. Its just datasource, so either we execute terraform plan or apply, shell script "ssh_key_generator.sh" will be triggered  and Public and Private Keys are generated

# Terraform Apply 
terraform apply -auto-approve

# Connect to VM (should be successful)
chmod 400 shell-scripts/terraformdemo-dev 
ssh -i shell-scripts/terraformdemo-dev azureuser@<PUBLIC-IP-OF-VM>

# Access Sample App
http://<PUBLIC-IP-OF-VM>

Step-05: Clean-Up

# Destroy Resources 
terraform destroy -auto-approve 

# Delete Files
rm -rf .terraform* 
rm -rf terraform.tfstate*