1

I am trying to deploy Terraform configurations using an Azure Function. What I have done to achieve this is make a Function App that executes the terraform.exe as a process, i.e have the .exe be in the same directory as the function itself, call it as a process then execute the init, plan and apply stages as separate processes.

Here is the code:

using System;
using System.Threading;
using System.Diagnostics;
using System.ComponentModel;
using System.Net;

public static void Run(TimerInfo myTimer, ILogger log)
{
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    
    string currentpath = "C:\\home\\site\\wwwroot\\TimerTrigger1\\terraform.exe";

    Process proc = new Process();
    proc.StartInfo.CreateNoWindow = false;
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.FileName = currentpath;

    proc.StartInfo.Arguments = "-chdir=C:\\home\\site\\wwwroot\\TimerTrigger1 init -no-color";
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.RedirectStandardError = true;
    proc.Start();
    log.LogInformation(proc.StandardOutput.ReadToEnd());
    proc.WaitForExit();

    proc.StartInfo.Arguments = "-chdir=C:\\home\\site\\wwwroot\\TimerTrigger1 plan -no-color";
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.RedirectStandardError = true;
    proc.Start();
    log.LogInformation(proc.StandardOutput.ReadToEnd());
    proc.WaitForExit();

    proc.StartInfo.Arguments = "-chdir=C:\\home\\site\\wwwroot\\TimerTrigger1 apply -auto-approve -no-color";
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.RedirectStandardError = true;
    proc.Start();
    log.LogInformation(proc.StandardOutput.ReadToEnd());
    proc.WaitForExit();
}

Unfortunately I am having some issues running this. The init stage works fine, but the plan stage is where it hangs. I initially thought of storing the state file in a remote file as outlined here: https://learn.microsoft.com/en-us/azure/developer/terraform/store-state-in-azure-storage however when I store the state file remotely, it's empty. I then decided to start by storing the state file in the same directory, i.e local state file, but I noticed it would vanish on each run.

The lock files and .terraform plugin directory are created fine, but the state file is the culprit, as a result of which none of the resources in the config file are created.

Function App working directory

I apologize if I have not explained things clearly, I am new to this so am quite confused. Would appreciate any help with this.

rookie123
  • 23
  • 4
  • Is there a reason why you aren't using a Terraform Cloud account? Isn't their a "free tier" you can use? – djangofan Jan 25 '22 at 16:43

0 Answers0