Skip to content

Instantly share code, notes, and snippets.

@Sarafian
Last active January 16, 2017 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sarafian/20eb2a449d6e2d6260015de60946c6fb to your computer and use it in GitHub Desktop.
Save Sarafian/20eb2a449d6e2d6260015de60946c6fb to your computer and use it in GitHub Desktop.
Download the AWS cloud formation samples in both JSON and AWS format
param(
[Parameter(Mandatory=$false)]
[ValidateSet("ap-northeast-1","ap-northeast-2","ap-south-1","ap-southeast-1","ap-southeast-2","ca-central-1","eu-central-1","eu-west-1","eu-west-2","sa-east-1","us-east-1","us-east-2","us-west-1","us-west-2")]
[string]$Region="eu-west-1",
[Parameter(Mandatory=$false)]
[string]$OutputPath="$env:USERPROFILE\Documents\AWSCloudFormation-samples-json-yaml"
)
Set-StrictMode -Version latest
$rubyPath=& "C:\Windows\System32\where.exe" ruby 2>&1
if(-not $?)
{
Write-Warning "ruby.exe is not available. Stopping"
return
}
# US East (Northern Virginia) Region url is
# https://s3.amazonaws.com/cloudformation-examples-us-east-1/AWSCloudFormation-samples.zip
if($region -eq "us-east-1")
{
$sampleUrl="https://s3.amazonaws.com/cloudformation-examples-$region/AWSCloudFormation-samples.zip"
}
else
{
$sampleUrl="https://s3-$region.amazonaws.com/cloudformation-examples-$region/AWSCloudFormation-samples.zip"
}
$tempSamplePath=Join-Path $env:TEMP "AWSCloudFormation-samples.zip"
if(Test-Path -Path $tempSamplePath)
{
Remove-Item -Path $tempSamplePath -Force
}
Invoke-WebRequest -Uri $sampleUrl -UseBasicParsing -OutFile $tempSamplePath
$expandPath=Join-Path $env:TEMP "AWSCloudFormation-samples"
if(Test-Path -Path $expandPath)
{
Remove-Item -Path $expandPath -Recurse -Force
}
Expand-Archive -Path $tempSamplePath -DestinationPath $expandPath
Get-ChildItem -Path $expandPath |ForEach-Object {
$jsonPath="$($_.FullName).json"
$yamlPath="$($_.FullName).yaml"
$_|Rename-Item -NewName $jsonPath
$rubyArgs=@(
"-ryaml"
"-rjson"
"-e"
"puts YAML.dump(JSON.parse(File.read('$jsonPath')))"
)
Write-Host "Converting $jsonPath to $yamlPath"
& $rubyPath $rubyArgs 2>&1 |Out-File -Encoding ascii -FilePath $yamlPath -Force
}
if(-not (Test-Path $OutputPath -PathType Container))
{
$null=New-Item $OutputPath -ItemType Directory
}
Move-Item -Path "$expandPath\*.*" -Destination $OutputPath -Force
@Sarafian
Copy link
Author

Sarafian commented Jan 11, 2017

Requires ruby to be installed. Quick way to install with Chocolatey choco install ruby -y.

Usage examples

# Download samples from EU West (Ireland) into AWSCloudFormation-samples-json-yaml in your documents folder. e.g. c:\users\username\documents\AWSCloudFormation-samples-json-yaml
& Get-AWSCloudFormationSamples.ps1

# Download samples from Asia Pacific (Tokyo) into C:\AWSCloudFormation-samples-json-yaml
& Get-AWSCloudFormationSamples.ps1 -Region ap-northeast-1 -OutputPath "C:\AWSCloudFormation-samples-json-yaml"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment