Skip to content

Instantly share code, notes, and snippets.

@chamindac
Created October 18, 2020 15:14
Show Gist options
  • Save chamindac/5b002b23506c1e9f1656df5510d01b3c to your computer and use it in GitHub Desktop.
Save chamindac/5b002b23506c1e9f1656df5510d01b3c to your computer and use it in GitHub Desktop.
Execute Terraform Plan By Building Parameter String
param
(
[Parameter(Mandatory=$true)]
[string] $tfStateFilePath,
[Parameter(Mandatory=$true)]
[string] $planFilePath,
[Parameter(Mandatory=$true)]
[string] $AzureSubscriptionId,
[Parameter(Mandatory=$true)]
[string] $AzureSPNAppId,
[Parameter(Mandatory=$true)]
[string] $AzureSPNPwd,
[Parameter(Mandatory=$true)]
[string] $AzureTenantId,
[Parameter(Mandatory=$true)]
[string] $projectName,
[Parameter(Mandatory=$true)]
[string] $envName,
[Parameter(Mandatory=$true)]
[string] $prodEnvName,
[Parameter(Mandatory=$true)]
[string] $location,
[Parameter(Mandatory=$true)]
[string] $dotnetCoreExtentionName,
[Parameter(Mandatory=$true)]
[string] $dotnetCoreExtentionVersion,
[Parameter(Mandatory=$true)]
[string] $dotnetFramework,
[Parameter(Mandatory=$true)]
[string] $deploySlotName,
[Parameter(Mandatory=$true)]
[string] $planKind,
[Parameter(Mandatory=$true)]
[string] $planReserved,
[Parameter(Mandatory=$true)]
[string] $planSKUTier,
[Parameter(Mandatory=$true)]
[string] $planSKUSize,
[Parameter(Mandatory=$true)]
[string] $storageTier,
[Parameter(Mandatory=$true)]
[string] $storageReplicatonType,
[Parameter(Mandatory=$true)]
[string] $functionVersion,
[Parameter(Mandatory=$true)]
[string] $keyvaultPurgeProtection,
[Parameter(Mandatory=$true)]
[string] $SqlSvrVersion,
[Parameter(Mandatory=$true)]
[string] $SqlSvrAdminUser,
[Parameter(Mandatory=$true)]
[string] $SqlSvrAdminUserPwd,
[Parameter(Mandatory=$true)]
[string] $SQLDatabaseEdition
)
$joinChar='=';
<# Use to try manually from local #>
<#$planFilePath="tempPlan.json";
$AzureSubscriptionId='AzureSubscriptionId','azuresubsid' -join $joinChar;
$AzureSPNAppId='AzureSPNAppId','spn app id' -join $joinChar;
$AzureSPNPwd='AzureSPNPwd','spn pwd' -join $joinChar;
$AzureTenantId='AzureTenantId','azure tenant' -join $joinChar;
$projectName='projectName','projname' -join $joinChar;
$envName='envName','dev' -join $joinChar;
$prodEnvName='prodEnvName','prod' -join $joinChar;
$location='location','eastus' -join $joinChar;
$dotnetCoreExtentionName='dotnetCoreExtentionName','AspNetCoreRuntime.3.1.x64' -join $joinChar;
$dotnetCoreExtentionVersion='dotnetCoreExtentionVersion','3.1.8' -join $joinChar;
$dotnetFramework='dotnetFramework','v4.0' -join $joinChar;
$deploySlotName='deploySlotName','deploy' -join $joinChar;
$planKind='planKind','Windows';
$planReserved='planReserved','0' -join $joinChar;
$planSKUTier='planSKUTier','Basic' -join $joinChar;
$planSKUSize='planSKUSize','B1' -join $joinChar;
$storageTier='storageTier','Standard' -join $joinChar;
$storageReplicatonType='storageReplicatonType','LRS' -join $joinChar;
$functionVersion='functionVersion','~3' -join $joinChar;
$keyvaultPurgeProtection='keyvaultPurgeProtection','~3' -join $joinChar;
$SqlSvrVersion='SqlSvrVersion','12.0' -join $joinChar;
$SqlSvrAdminUser='SqlSvrAdminUser','sqladmin' -join $joinChar;
$SqlSvrAdminUserPwd='SqlSvrAdminUserPwd','sqladminpwd' -join $joinChar;
$SQLDatabaseEdition='SQLDatabaseEdition','Basic' -join $joinChar;
#>
# Set terraform variable with value
$AzureSubscriptionId='AzureSubscriptionId',$AzureSubscriptionId -join $joinChar;
$AzureSPNAppId='AzureSPNAppId',$AzureSPNAppId -join $joinChar;
$AzureSPNPwd='AzureSPNPwd',$AzureSPNPwd -join $joinChar;
$AzureTenantId='AzureTenantId',$AzureTenantId -join $joinChar;
$projectName='projectName',$projectName -join $joinChar;
$envName='envName',$envName -join $joinChar;
$prodEnvName='prodEnvName',$prodEnvName -join $joinChar;
$location='location',$location -join $joinChar;
$dotnetCoreExtentionName='dotnetCoreExtentionName',$dotnetCoreExtentionName -join $joinChar;
$dotnetCoreExtentionVersion='dotnetCoreExtentionVersion',$dotnetCoreExtentionVersion -join $joinChar;
$dotnetFramework='dotnetFramework',$dotnetFramework -join $joinChar;
$deploySlotName='deploySlotName',$deploySlotName -join $joinChar;
$planKind='planKind',$planKind -join $joinChar;
$planReserved='planReserved',$planReserved -join $joinChar;
$planSKUTier='planSKUTier',$planSKUTier -join $joinChar;
$planSKUSize='planSKUSize',$planSKUSize -join $joinChar;
$storageTier='storageTier',$storageTier -join $joinChar;
$storageReplicatonType='storageReplicatonType',$storageReplicatonType -join $joinChar;
$functionVersion='functionVersion',$functionVersion -join $joinChar;
$keyvaultPurgeProtection='keyvaultPurgeProtection',$keyvaultPurgeProtection -join $joinChar;
$SqlSvrVersion='SqlSvrVersion',$SqlSvrVersion -join $joinChar;
$SqlSvrAdminUser='SqlSvrAdminUser',$SqlSvrAdminUser -join $joinChar;
$SqlSvrAdminUserPwd='SqlSvrAdminUserPwd',$SqlSvrAdminUserPwd -join $joinChar;
$SQLDatabaseEdition='SQLDatabaseEdition',$SQLDatabaseEdition -join $joinChar;
# Build up terraform varaible format string
$varPrefix= "-var";
$vars = ($varPrefix,("'",$AzureSubscriptionId,"'" -join '') -join $joinChar
),($varPrefix,("'",$AzureSPNAppId,"'" -join '') -join $joinChar
),($varPrefix,("'",$AzureSPNPwd,"'" -join '') -join $joinChar
),($varPrefix,("'",$AzureTenantId,"'" -join '') -join $joinChar
),($varPrefix,("'",$projectName,"'" -join '') -join $joinChar
),($varPrefix,("'",$envName,"'" -join '') -join $joinChar
),($varPrefix,("'",$prodEnvName,"'" -join '') -join $joinChar
),($varPrefix,("'",$location,"'" -join '') -join $joinChar
),($varPrefix,("'",$dotnetCoreExtentionName,"'" -join '') -join $joinChar
),($varPrefix,("'",$dotnetCoreExtentionVersion,"'" -join '') -join $joinChar
),($varPrefix,("'",$dotnetFramework,"'" -join '') -join $joinChar
),($varPrefix,("'",$deploySlotName,"'" -join '') -join $joinChar
),($varPrefix,("'",$planKind,"'" -join '') -join $joinChar
),($varPrefix,("'",$planReserved,"'" -join '') -join $joinChar
),($varPrefix,("'",$planSKUTier,"'" -join '') -join $joinChar
),($varPrefix,("'",$planSKUSize,"'" -join '') -join $joinChar
),($varPrefix,("'",$storageTier,"'" -join '') -join $joinChar
),($varPrefix,("'",$storageReplicatonType,"'" -join '') -join $joinChar
),($varPrefix,("'",$functionVersion,"'" -join '') -join $joinChar
),($varPrefix,("'",$keyvaultPurgeProtection,"'" -join '') -join $joinChar
),($varPrefix,("'",$SqlSvrVersion,"'" -join '') -join $joinChar
),($varPrefix,("'",$SqlSvrAdminUser,"'" -join '') -join $joinChar
),($varPrefix,("'",$SqlSvrAdminUserPwd,"'" -join '') -join $joinChar
),($varPrefix,("'",$SQLDatabaseEdition,"'" -join '') -join $joinChar
) -join ' '
# Set terraform plan file path and state file path parameters
$outParam = '-out',("'",$planFilePath,"'" -join '') -join $joinChar
$stateParam = '-state',("'",$tfStateFilePath,"'" -join '') -join $joinChar
# Build up terraform plan command
$planCommand = 'terraform plan',$outParam,$stateParam,$vars -join ' '
write-host ($planCommand)
# Execute terraform plan
Invoke-Expression $planCommand
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment