powershellamazon-web-servicesamazon-cloudfrontpowershell-cmdletaws-powershell

AWS PowerShell update CloudFront distribution


I am trying to update my CloudFront distribution using AWSPowerShell module for PowerShell. When I use the update cmdlet from the module, I always get an error about not providing the "IfMatch" parameter.

$cfd = Update-CFDistribution @parameters -Id "E2POBWR9AXFROP"

Error: The If-Match version is missing or not valid for the resource.
Update-CFDistribution : The If-Match version is missing or not valid for the resource.

I went to the AWS doc to know about this parameter and it says:

-IfMatch: The value of the ETag header that you received when retrieving the distribution's configuration. For example: E2QWRUHAPOMQZL.

I was wondering if there is a way to to get the content of the ETag header using the AWSPowerShell module cmdlets. I don't want to call directly the AWS API doing an Http request in my PowerShell script just to get the content of the header... but maybe it's the only way.

I tried with the Get-CFDistributionConfig cmdlet but it doesn't return this information.

$cfd = Get-CFDistributionConfig @parameters -Id "E2POBWR9AXFROP"

This is the version of PowerShell I am using:

PS C:\> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      15063  608  

This is the version of AWSPowerShell module I am using:

PS C:\> Get-Module "AWSPowerShell" -ListAvailable

ModuleType Version    Name
---------- -------    ----
Binary     3.3.169.0  AWSPowerShell

Solution

  • The workaround I found to make it work for now is to call the API directly and read the headers. I had to implement Signature version 4 to get the security Authorization header.

    $headers = Get-AWSSecurityHeaders -service "cloudfront" -httpVerb "GET" -uri "/2017-03-25/distribution/$distributionId/config"
    $response = Invoke-WebRequest -Uri "https://cloudfront.amazonaws.com/2017-03-25/distribution/$distributionId/config" -Headers $headers
    $etag = $response.Headers.ETag
    

    I was then able to provide the ETag to the Update-CFDistribution cmdlet and make it work.

    $distribution = Update-CFDistribution @parameters -Id $distributionId -IfMatch $etag -Verbose
    

    Hopefully the ETag will be returned by the AWSPowerShell module in a next version to avoid having to do all that.