Created
April 17, 2018 08:13
-
-
Save theorigin/42943fe83a43d85698acc3504e249f0f to your computer and use it in GitHub Desktop.
PS script to update cloudfront distribution
This file contains hidden or 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
<# | |
.SYNOPSIS | |
Updates the Cloudfront distribution origin path with the supplied version number | |
.DESCRIPTION | |
Given a version number this script will retrieve the current distribution, extract the ETAG value, update the current-distribution.json file with the version number (for the OriginPath), | |
save the JSON and then update the distribution using the new file. A CloudFront invalidation is then created to expire all edge caches. | |
.PARAMETER version | |
A value indicating the version number to be used e.g. 1.12.1 | |
.PARAMETER id | |
A value indicating the id of the distribution to update | |
.PARAMETER env | |
A value indicating the environment that is being updated | |
.INPUTS | |
None | |
.OUTPUTS | |
None | |
.NOTES | |
Version: 1.0 | |
Author: Andy Robinson | |
Creation Date: 2018-04-04 | |
Purpose/Change: Initial script development | |
.EXAMPLE | |
Update-CloudFrontDistribution-OriginPath.ps1 -version 1.12.1 | |
#> | |
param( | |
[Parameter(Mandatory=$true)] | |
[string] $version, | |
#[string] $id = "EC6SV4J7HMZTU", | |
[string] $id = "E1HQKZHGF8T6GQ", | |
[string] $env = "dev" | |
) | |
Write-Host "Version" $version | |
Write-Host "Id =" $id | |
Write-Host "Current path =" (Get-Location) | |
Write-Host "Env =" $env | |
$currentDistributionFilename = "current-distribution.json" | |
$distributionConfigFilename = "distribution-config-$($env).json" | |
$updatedistributionConfigFilename = "updated-distribution-config.json" | |
$getDistributionCommand = "aws cloudfront get-distribution-config --id $($id) --profile dev > $($currentDistributionFilename)" | |
$createInvalidationCommand = "aws cloudfront create-invalidation --distribution-id $($id) --profile dev --paths ""/*""" | |
Write-Host "Executing getDistributionCommand '$($getDistributionCommand)'" | |
Invoke-Expression $getDistributionCommand | |
#Grab the current ETAG so we can update the configuration | |
$currentDistribution = Get-Content ".\$($currentDistributionFilename)" -raw | ConvertFrom-Json | |
$etag = $currentDistribution.ETag | |
Write-Host "Found ETAG = " $etag | |
#Load the new config and set the version number | |
$distributionConfig = Get-Content ".\$($distributionConfigFilename)" -raw | ConvertFrom-Json | |
#Find any OriginPath elements that start with / | |
foreach ($element in $distributionConfig.Origins.Items) { | |
if ($element.OriginPath.StartsWith("/")) | |
{ | |
Write-Host "Found origin element" | |
$element.OriginPath = "/$($version)" | |
} | |
} | |
# Save the modified configuration | |
Write-Host "Creating updated distribution file" | |
$distributionConfig | ConvertTo-Json -Depth 20 | set-content ".\$($updatedistributionConfigFilename)" | |
# Setup the update command | |
$updateDistributionCommand = "aws cloudfront update-distribution --id $($id) --distribution-config file://$($updatedistributionConfigFilename) --profile dev --if-match $($etag)" | |
# Update the distribution using the new file | |
Write-Host "Executing updateDistributionCommand '$($updateDistributionCommand)'" | |
Invoke-Expression $updateDistributionCommand | |
# Force a refresh on Cloudfront | |
Write-Host "Executing createInvalidationCommand '$($createInvalidationCommand)'" | |
Invoke-Expression $createInvalidationCommand |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment