Terraform: Deploy VM

1 minute read

Description

After following “Create Terraform Service Principle”, I did the following to deploy a VM following the official guide here.

To Resolve:

  1. Since cloudshell already has Terraform cli installed, I just did the following in cloudshell Bash:

    1
    2
    3
    4
    5
    6
    
    # first, make sure environmental vars are populated from previous steps (see reference to previous post)
    printenv | grep ^TF_VAR*
    
    cd clouddrive
    mkdir terra
    cd terra
    
  2. Upload main.tf using the upload tool.

  3. Now we just run a series of commands using terraform cli:

    1
    2
    3
    4
    5
    6
    7
    8
    
    # Init and install modules
    terraform init
    
    # Create a plan => Tells you what actions it will do like create resources but doesn't do it
    terraform plan -out main.tfplan
    
    # Apply the plan => Creates resources
    terraform apply main.tfplan
    
  4. SSH into the VM

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    terraform output -raw tls_private_key
    # Copy/paste to c:\scripts\key.txt
    
    # now open powershell and type:
    wsl
    cp /mnt/c/scripts/key.txt /home/gerry/key.txt
    
    mv /home/gerry/key.txt /home/gerry/mykey
    chmod 400 mykey
    # replace the IP on next step with public IP of your Ubuntu VM
    ssh -i ./mykey azureuser@13.90.207.214
    
    # notice I can ssh in with key and also have root access, 'sudo su' for example
    
  5. Now that we have deployed an environment, let’s tear it down to not incur any costs:

    1
    2
    
    terraform plan -destroy
    terraform apply -destroy
    

Comments