Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from DefendableDesign/remote-tfstate
Remote tfstate
- Loading branch information
Showing
5 changed files
with
65 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
region = "ap-southeast-2" | ||
enable_auto_response = "false" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
variable "region" { | ||
default = "ap-southeast-2" | ||
} | ||
|
||
variable "enable_auto_response" { | ||
default = "false" | ||
} |