1

I have written the script to change the origin path" of a specific origin on a specific distribution in CloudFront. After changing the path I am getting errors when trying to update the distribution with the changes. I do not want to change anything else on the distribution besides the "origin path", so how do I call Update-CFDistribution to make these changes ideally without having to set every parameter (future updates to the API may cause this script to fail or even worst make incomplete modifications)?

$distributions = Get-CFDistributionList 

foreach($distribution in $distributions) {
    if($distribution.Id -eq "$CloudfrontDistributionId") {
        foreach ($origin in $distribution.Origins) {
            foreach($item in $origin.Items) {
                if($item.Id -eq "OriginName") {
                    $item.OriginPath = "/$($S3BucketPrefix)"
                    Update-CFDistribution -Id $CloudfrontDistributionId -Origins_Item @($item)
                }
            }
        }
    }
}

Error

InvalidOperation: 5 validation errors detected: Value null at 'distributionConfig.defaultCacheBehavior' failed to satisfy constraint: Member must not be null; Value null at 'distributionConfig.enabled' failed to satisfy constraint: Member must not be null; Value null at 'distributionConfig.callerReference' failed to satisfy constraint: Member must not be null; Value null at 'distributionConfig.origins.quantity' failed to satisfy constraint: Member must not be null; Value at 'distributionConfig.comment' failed to satisfy constraint: Member must not be null

Observer
  • 755
  • 1
  • 14
  • 23

2 Answers2

2

I don't have the reputation to add just a comment so I'm having to put this as an answer, but it's better suited as a comment.

According to the documentation, there are a number of required fields when you issue an update, even if you're only trying to update one thing:

https://docs.aws.amazon.com/powershell/latest/reference/items/Update-CFDistribution.html

When you update a distribution, there are more required fields than when you create a distribution. When you update your distribution by using this API action, follow the steps here to get the current configuration and then make your updates, to make sure that you include all of the required fields.

Saf
  • 92
  • 2
  • 9
  • What I am not entirely clear about is whether the fields that are not set will be impacted. A trial and error thing I suppose. – Observer Nov 02 '20 at 07:47
  • The way I would probably approach it is to capture the current required fields (using the Get-CFDistribution command) and pass them to the Update-CFDistribution command. That way you'd always know they're going to be the same. If that helps. – Saf Nov 02 '20 at 08:32
0

I had the same issue and asked about it on the aws-tools-for-powershell Q&A. As far as I can tell, the answer is you just have to map all the properties. I took the time to do that today. In other PowerShell module contexts, I would expect you'd be able to just pipe in the object from a Get call with changes into an Update call and it would handle the parameter bindings. That doesn't work in this context. This is a known issue for a lot of aws-tools-for-powershell cmdlets according to aws-tools-for-powershell Issue #214.

Below is a snippet of the 66 mappings required to ensure you're updating all properties exactly as they were handed to you in the original Get call. It's possible fewer are actually required, but given I want an exact copy it felt safer to ensure all the data was available and let the API decide what could be dropped.

The etag thing is from: AWS PowerShell update CloudFront distribution

$distribution = Get-CFDistribution -Id $distributionFromList.Id

$etag = $AWSHistory.LastServiceResponse.ETag

$params = @{
    Id = $distribution.Id
    Verbose = $true
    IfMatch = $etag
    Aliases_Item = $distribution.DistributionConfig.Aliases.Items
    Aliases_Quantity = $distribution.DistributionConfig.Aliases.Quantity
    AllowedMethods_Item = $distribution.DistributionConfig.DefaultCacheBehavior.AllowedMethods.Items
    AllowedMethods_Quantity = $distribution.DistributionConfig.DefaultCacheBehavior.AllowedMethods.Quantity
    CacheBehaviors_Item = $distribution.DistributionConfig.CacheBehaviors.Items
    CacheBehaviors_Quantity = $distribution.DistributionConfig.CacheBehaviors.Quantity
    CachedMethods_Item = $distribution.DistributionConfig.DefaultCacheBehavior.AllowedMethods.CachedMethods.Items
    CachedMethods_Quantity = $distribution.DistributionConfig.DefaultCacheBehavior.AllowedMethods.CachedMethods.Quantity
    Cookies_Forward = $distribution.DistributionConfig.DefaultCacheBehavior.ForwardedValues.Cookies.Forward
    CustomErrorResponses_Item = $distribution.DistributionConfig.CustomErrorResponses.Items
    CustomErrorResponses_Quantity = $distribution.DistributionConfig.CustomErrorResponses.Quantity
    DefaultCacheBehavior_CachePolicyId = $distribution.DistributionConfig.DefaultCacheBehavior.CachePolicyId
    DefaultCacheBehavior_Compress = $distribution.DistributionConfig.DefaultCacheBehavior.Compress
    DefaultCacheBehavior_DefaultTTL = $distribution.DistributionConfig.DefaultCacheBehavior.DefaultTTL
    DefaultCacheBehavior_FieldLevelEncryptionId = $distribution.DistributionConfig.DefaultCacheBehavior.FieldLevelEncryptionId
    DefaultCacheBehavior_MaxTTL = $distribution.DistributionConfig.DefaultCacheBehavior.MaxTTL
    DefaultCacheBehavior_MinTTL = $distribution.DistributionConfig.DefaultCacheBehavior.MinTTL
    DefaultCacheBehavior_OriginRequestPolicyId = $distribution.DistributionConfig.DefaultCacheBehavior.OriginRequestPolicyId
    DefaultCacheBehavior_RealtimeLogConfigArn = $distribution.DistributionConfig.DefaultCacheBehavior.RealtimeLogConfigArn
    DefaultCacheBehavior_ResponseHeadersPolicyId = $distribution.DistributionConfig.DefaultCacheBehavior.ResponseHeadersPolicyId
    DefaultCacheBehavior_SmoothStreaming = $distribution.DistributionConfig.DefaultCacheBehavior.SmoothStreaming
    DefaultCacheBehavior_TargetOriginId = $distribution.DistributionConfig.DefaultCacheBehavior.TargetOriginId
    DefaultCacheBehavior_ViewerProtocolPolicy = $distribution.DistributionConfig.DefaultCacheBehavior.ViewerProtocolPolicy
    DistributionConfig_CallerReference = $distribution.DistributionConfig.CallerReference
    DistributionConfig_Comment = $distribution.DistributionConfig.Comment
    DistributionConfig_DefaultRootObject = $distribution.DistributionConfig.DefaultRootObject
    DistributionConfig_Enabled = $distribution.DistributionConfig.Enabled
    DistributionConfig_HttpVersion = $distribution.DistributionConfig.HttpVersion
    DistributionConfig_IsIPV6Enabled = $distribution.DistributionConfig.IsIPV6Enabled
    DistributionConfig_PriceClass = $distribution.DistributionConfig.PriceClass
    DistributionConfig_WebACLId = $distribution.DistributionConfig.WebACLId
    ForwardedValues_QueryString = $distribution.DistributionConfig.DefaultCacheBehavior.ForwardedValues.QueryString
    FunctionAssociations_Item = $distribution.DistributionConfig.DefaultCacheBehavior.FunctionAssociations.Items
    FunctionAssociations_Quantity = $distribution.DistributionConfig.DefaultCacheBehavior.FunctionAssociations.Quantity
    GeoRestriction_Item = $distribution.DistributionConfig.Restrictions.GeoRestriction.Items
    GeoRestriction_Quantity = $distribution.DistributionConfig.Restrictions.GeoRestriction.Quantity
    GeoRestriction_RestrictionType = $distribution.DistributionConfig.Restrictions.GeoRestriction.RestrictionType
    Headers_Item = $distribution.DistributionConfig.DefaultCacheBehavior.ForwardedValues.Headers.Items
    Headers_Quantity = $distribution.DistributionConfig.DefaultCacheBehavior.ForwardedValues.Headers.Quantity
    LambdaFunctionAssociations_Item = $distribution.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations.Items
    LambdaFunctionAssociations_Quantity = $distribution.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations.Quantity
    Logging_Bucket = $distribution.DistributionConfig.Logging.Bucket
    Logging_Enabled = $distribution.DistributionConfig.Logging.Enabled
    Logging_IncludeCookie = $distribution.DistributionConfig.Logging.IncludeCookies
    Logging_Prefix = $distribution.DistributionConfig.Logging.Prefix
    OriginGroups_Item = $distribution.DistributionConfig.OriginGroups.Items
    OriginGroups_Quantity = $distribution.DistributionConfig.OriginGroups.Quantity
    Origins_Item = $distribution.DistributionConfig.Origins.Items
    Origins_Quantity = $distribution.DistributionConfig.Origins.Quantity
    QueryStringCacheKeys_Item = $distribution.DistributionConfig.DefaultCacheBehavior.ForwardedValues.QueryStringCacheKeys.Items
    QueryStringCacheKeys_Quantity = $distribution.DistributionConfig.DefaultCacheBehavior.ForwardedValues.QueryStringCacheKeys.Quantity
    TrustedKeyGroups_Enabled = $distribution.DistributionConfig.DefaultCacheBehavior.TrustedKeyGroups.Enabled
    TrustedKeyGroups_Item = $distribution.DistributionConfig.DefaultCacheBehavior.TrustedKeyGroups.Items
    TrustedKeyGroups_Quantity = $distribution.DistributionConfig.DefaultCacheBehavior.TrustedKeyGroups.Quantity
    TrustedSigners_Enabled = $distribution.DistributionConfig.DefaultCacheBehavior.TrustedSigners.Enabled
    TrustedSigners_Item = $distribution.DistributionConfig.DefaultCacheBehavior.TrustedSigners.Items
    TrustedSigners_Quantity = $distribution.DistributionConfig.DefaultCacheBehavior.TrustedSigners.Quantity
    ViewerCertificate_ACMCertificateArn = $distribution.DistributionConfig.ViewerCertificate.ACMCertificateArn
    ViewerCertificate_Certificate = $distribution.DistributionConfig.ViewerCertificate.Certificate
    ViewerCertificate_CertificateSource = $distribution.DistributionConfig.ViewerCertificate.CertificateSource
    ViewerCertificate_CloudFrontDefaultCertificate = $distribution.DistributionConfig.ViewerCertificate.CloudFrontDefaultCertificate
    ViewerCertificate_IAMCertificateId = $distribution.DistributionConfig.ViewerCertificate.IAMCertificateId
    ViewerCertificate_MinimumProtocolVersion = $distribution.DistributionConfig.ViewerCertificate.MinimumProtocolVersion
    ViewerCertificate_SSLSupportMethod = $distribution.DistributionConfig.ViewerCertificate.SSLSupportMethod
    WhitelistedNames_Item = $distribution.DistributionConfig.DefaultCacheBehavior.ForwardedValues.Cookies.WhitelistedNames.Items
    WhitelistedNames_Quantity = $distribution.DistributionConfig.DefaultCacheBehavior.ForwardedValues.Cookies.WhitelistedNames.Quantity
}

Update-CFDistribution @params -WhatIf
ojintoad
  • 433
  • 3
  • 6