Skip to content

Instantly share code, notes, and snippets.

@darbio
Last active February 3, 2016 22:54
Show Gist options
  • Save darbio/25ad1f84b4fa7892d98a to your computer and use it in GitHub Desktop.
Save darbio/25ad1f84b4fa7892d98a to your computer and use it in GitHub Desktop.
cfn.ps1
#
# AWS Deploy CloudFormation template and return stack ID
# powershell.exe deploy.ps1 -template templateName.json
#
param(
[String]$template="template.json"
)
Import-Module AWSPowerShell
Function CreateStack
{
# Calculate the stack name
# Must match [A-Za-z][A-Za-z0-9]*
$stackName = 'TC{0}' -f [guid]::NewGuid().ToString().Replace("-", "").ToUpper()
Write-Host "Creating stack with name $stackName from template $template"
# Get the template full directory on the file system
$templateFilePath = Join-Path -path $PSScriptRoot -ChildPath $template
# Call AWS with the template and stack name
# This returns a JSON string which looks like:
# { "StackId" : "..." }
$response = (aws cloudformation create-stack --stack-name $stackName --template-body "file://$templateFilePath")
$responseJson = [String]::Join(" ", $response)
# Parse the response
$result = ConvertFrom-Json $responseJson
# Get the properties
$stack_id = $result.StackId
return $stack_id
}
Function DeleteStack($stack_id)
{
Write-Host "Deleting stack with id $stack_id"
aws cloudformation delete-stack --stack-name $stack_id
}
$stack_id = CreateStack
DeleteStack $stack_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment