-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup_remote_tfstate.ps1
More file actions
41 lines (36 loc) · 1.21 KB
/
setup_remote_tfstate.ps1
File metadata and controls
41 lines (36 loc) · 1.21 KB
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
#Determine Region
$region = @(Get-DefaultAWSRegion).Region
If ($region -eq $null) {
$region = Read-Host -Prompt 'Enter the AWS region for your Terraform state S3 bucket'
}
#Determine AWS Account ID
Try {
$accountId = @(get-ec2securitygroup -GroupNames "default" -Region $region)[0].OwnerId
}
Catch {
Write-Host "Error determining AWS Account ID"
Break
}
#Define Bucket Name
$bucketName = "dd-tfstate-{0}" -f $accountId
#Create Bucket
Try {
Write-Host "Creating Terraform state S3 bucket: $bucketName"
New-S3Bucket -BucketName $bucketName -Region $region -ErrorAction 'SilentlyContinue'
}
Catch [System.AggregateException]{
$safeToIgnore = $Error[0].Exception.ToString().Contains("Your previous request to create the named bucket succeeded and you already own it")
If ($safeToIgnore) {
Write-Host "Using existing bucket: $bucketName."
} Else {
Write-Host "Error creating S3 bucket: $bucketName."
Break
}
}
#Enable versioning
Write-Host "Enabling versioning."
Write-S3BucketVersioning -BucketName $bucketName -Region $region -VersioningConfig_Status Enabled
#Initialise Terraform
terraform init `
-backend-config="bucket=$bucketName" `
-backend-config="region=$region"