Skip to content

Instantly share code, notes, and snippets.

@HammoTime
Last active May 27, 2018 23:34
Show Gist options
  • Save HammoTime/698fb119e47a1264c1063d3fcd6c77c3 to your computer and use it in GitHub Desktop.
Save HammoTime/698fb119e47a1264c1063d3fcd6c77c3 to your computer and use it in GitHub Desktop.
Deploys a new template version to S3 and then recreates the stack.
Import-Module AWSPowerShell
# So our AWS Scripts work well with Proxies.
$Browser = New-Object System.Net.WebClient
$Browser.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials
$StackName = 'TestStack'
$TemplateKey = '/CloudFormation/template.json'
$FileLocation = 'C:\Temp\template.json'
$BucketName = 'test-bucket'
$RegionUrl = 'https://s3-ex-ampleregion-1.amazonaws.com/'
Write-S3Object -BucketName $BucketName -Key $TemplateKey -File $FileLocation
Try {
if(Get-CFNStack $StackName) {
Write-Host 'CloudFormation Stack Exists, deleting.'
Remove-CFNStack -StackName $StackName -Force
}
While(Get-CFNStack $StackName) {
Start-Sleep 5
Write-Host 'Waiting for the CloudFormation Stack to be deleted.'
}
} Catch [Exception] {
Write-Host ("Stack deletion failed: '" + $_.Message + "'.")
}
Try {
$Stack = New-CFNStack -StackName $StackName `
-TemplateURL ($RegionUrl + $BucketName + $TemplateKey)
While((Get-CFNStack $StackName).StackStatus -ne 'CREATE_COMPLETE') {
if((Get-CFNStack $StackName).StackStatus -eq 'ROLLBACK_COMPLETE') {
Throw 'Stack creation failed.'
}
Write-Host 'Pending stack creation...'
Start-Sleep 30
}
Write-Host 'Stack deployed successfully.'
} Catch [Exception] {
Write-Host ("Stack creation failed: '" + $_.Message + "'.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment