Access OS environment variables in Terraform steps

usability
(Дмитрий Бондаренко) #1

Hi,

I have an issue with environment variables in Terraform steps. For cloud authentication I need to set one specific environment variable YC_SERVICE_ACCOUNT_KEY_FILE, so it can be used in plan command. I did a step where set this variable (tried it on all environment levels — machine, user and process), but Terraform plan step can’t see it.

Is there any way to do it?

(Kenneth Bates) #3

Hi Dmitry,

Thanks for getting in touch! I think the easiest way to get this environment variable into your Terraform step would be to first create an output variable in an earlier step, which you could then call in your subsequent Terraform step.

For example, you could have an earlier PowerShell script step (that can see your environment variables) set an output variable with Set-OctopusVariable -name "VariableName" -value "EnvironmentVariable" and to call it use the syntax #{Octopus.Action[ScriptStep].Output.VariableName}.

You can also print out all variables (including environment variables) to your task log by creating a project variable named OctopusPrintVariables with a value of True, which is helpful for debugging and seeing what variables are available. Note that a new release has to be created to apply variable changes.

Would this approach work for you? Let me know what you think or if you have any further questions going forward. :slight_smile:

Best regards,

Kenny

(Дмитрий Бондаренко) #4

Hi Kenny,

Thank you for your reply!
I have an issue with this approach. I can’t reach even output variables in terraform-specific steps. There is no access to scripts in them :slight_smile:

So, I’ve found that I can add user-target environment variable (by step) and then restart Octopus service on my worker machine (by hand). When process started it acquires new snapshot of env. variables. I don’t need to change this variable on every deployment, just set once and forget. And it works.

(Kenneth Bates) #5

Hi Dmitry,

Thanks for keeping in touch. Great to hear you’ve got this working as needed, and I appreciate the time taken to elaborate on your solution! Don’t hesitate to reach out if you have any questions or concerns moving forward. :slight_smile:

Best regards,

Kenny