-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraform_build.ps1
85 lines (68 loc) · 2.82 KB
/
terraform_build.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function Parse-AwsFile {
param (
[string]$filePath
)
$result = @{}
if (Test-Path $filePath) {
$inSection = $false
Get-Content $filePath | ForEach-Object {
$_ = $_.Trim()
if ($_.StartsWith("[") -and $_.EndsWith("]")) {
$inSection = $true
} elseif ($inSection -and $_ -match '^\s*([^=]+)\s*=\s*(.+)\s*$') {
$key = $matches[1].Trim()
$value = $matches[2].Trim()
$result[$key] = $value
}
}
} else {
Write-Output "File not found: $filePath"
}
return $result
}
# Setup aws vars
$awsCredentialsPath = "$env:USERPROFILE\.aws\credentials"
$awsConfigPath = "$env:USERPROFILE\.aws\config"
$awsCredentials = Parse-AwsFile -filePath $awsCredentialsPath
$awsConfig = Parse-AwsFile -filePath $awsConfigPath
if ($awsCredentials.ContainsKey('aws_access_key_id') -and $awsCredentials.ContainsKey('aws_secret_access_key')) {
$env:AWS_ACCESS_KEY_ID = $awsCredentials['aws_access_key_id']
$env:AWS_SECRET_ACCESS_KEY = $awsCredentials['aws_secret_access_key']
} else {
Write-Output "AWS credentials not found."
}
if ($awsConfig.ContainsKey('region')) {
$env:AWS_DEFAULT_REGION = $awsConfig['region']
} else {
Write-Output "AWS config not found."
}
# Print the exported variables (optional)
Write-Output "AWS_ACCESS_KEY_ID=$env:AWS_ACCESS_KEY_ID"
Write-Output "AWS_SECRET_ACCESS_KEY=$env:AWS_SECRET_ACCESS_KEY"
Write-Output "AWS_DEFAULT_REGION=$env:AWS_DEFAULT_REGION"
Write-Output "`nSetting up tf variables...`n"
$env:TF_VAR_aws_key = $env:AWS_ACCESS_KEY_ID
$env:TF_VAR_aws_secret = $env:AWS_SECRET_ACCESS_KEY
$env:TF_VAR_aws_region = $env:AWS_DEFAULT_REGION
Get-ChildItem Env:TF_VAR_* | ForEach-Object { Write-Output " $($_.Name)=$($_.Value)" }
Set-Location -Path "src/app/terraform"
$env:AWS_PROFILE = 'default'
Write-Output "`n"
Write-Output "terraform init"
terraform init | ForEach-Object { Write-Output " $_" }
if ($LASTEXITCODE -ne 0) { Write-Output "Error on terraform init!"; Set-Location -Path "../../.."; exit 1 }
Write-Output "`n"
Write-Output "terraform validate"
terraform validate | ForEach-Object { Write-Output " $_" }
if ($LASTEXITCODE -ne 0) { Write-Output "Error on terraform validate!"; Set-Location -Path "../../.."; exit 1 }
Write-Output "`n"
Write-Output "terraform plan"
terraform plan | ForEach-Object { Write-Output " $_" }
if ($LASTEXITCODE -ne 0) { Write-Output "Error on terraform plan!"; Set-Location -Path "../../.."; exit 1 }
Write-Output "`n"
Write-Output "terraform apply"
terraform apply -auto-approve | ForEach-Object { Write-Output " $_" }
if ($LASTEXITCODE -ne 0) { Write-Output "Error on terraform apply!"; Set-Location -Path "../../.."; exit 1 }
Write-Output "`n"
Write-Output "Terraform build process complete!"
Set-Location -Path "../../.."