Home > Mobile >  Change entry in json file that matches the condition in CloudFront Update Distribution
Change entry in json file that matches the condition in CloudFront Update Distribution

Time:10-29

We have a scenario where we need to replace the TargetOriginID of CacheBehaviour in the distribution of a json file. We need to replace the Existing TargetOriginID with New Values. I have tried with below jq command but not getting any closer

for targetoriginID in $(jq '(.CacheBehaviors.Items[].TargetOriginId)' distconfig.json);

         do 
                  
                 
                 echo "#######fetch the new value from change behaviour json file"#######
                 NewValue=$(jq -r "map(select(.targetoriginid == ""$targetoriginID""))[].targetorigindr" changebehaviour.json)


echo "#########replace value in dist config json file with new value from change behaviour###########"
jq -r '(.CacheBehaviors.Items[].TargetOriginId | select(. == "$targetoriginID")) = "$NewValue"' distconfig.json > "tmp" && mv "tmp" distconfig.json
{

      "CachedMethods": {
        "Quantity": 3,
        "Items": [
          "HEAD",
          "GET",
          "OPTIONS"
        ]
      }
    },
    "SmoothStreaming": false,
    "Compress": false,
    "LambdaFunctionAssociations": {
      "Quantity": 0
    },
    "FunctionAssociations": {
      "Quantity": 0
    },
    "FieldLevelEncryptionId": "",
    "ForwardedValues": {
      "QueryString": true,
      "Cookies": {
        "Forward": "none"
      },
      "Headers": {
        "Quantity": 9,
        "Items": [
          "Authorization",
          "Origin",
          "access-control-allow-credentials",
          "expires",
          "access-control-max-age",
          "access-control-allow-headers",
          "cache-control",
          "access-control-allow-methods",
          "pragma"
        ]
      },
      "QueryStringCacheKeys": {
        "Quantity": 1,
        "Items": [
          "*"
        ]
      }
    },
    "MinTTL": 0,
    "DefaultTTL": 86400,
    "MaxTTL": 31536000
  },
  "CacheBehaviors": {
    "Quantity": 2,
    "Items": [
      {
        "PathPattern": "jkl/*",
        "TargetOriginId": "nkl/Prod",
        "TrustedSigners": {
          "Enabled": false,
          "Quantity": 0
        },
        "TrustedKeyGroups": {
          "Enabled": false,
          "Quantity": 0
        },
        "ViewerProtocolPolicy": "redirect-to-https",
        "AllowedMethods": {
          "Quantity": 7,
          "Items": [
            "HEAD",
            "DELETE",
            "POST",
            "GET",
            "OPTIONS",
            "PUT",
            "PATCH"
          ],
          "CachedMethods": {
            "Quantity": 3,
            "Items": [
              "HEAD",
              "GET",
              "OPTIONS"
            ]
          }
        },
        "SmoothStreaming": false,
        "Compress": false,
        "LambdaFunctionAssociations": {
          "Quantity": 0
        },
        "FunctionAssociations": {
          "Quantity": 0
        },
        "FieldLevelEncryptionId": "",
        "ForwardedValues": {
          "QueryString": true,
          "Cookies": {
            "Forward": "all"
          },
          "Headers": {
            "Quantity": 9,
            "Items": [
              "Authorization",
              "Origin",
              "access-control-allow-credentials",
              "access-control-max-age",
              "access-control-allow-headers",
              "cache-control",
              "access-control-allow-methods",
              "expirers",
              "pragma"
            ]
          },
          "QueryStringCacheKeys": {
            "Quantity": 1,
            "Items": [
              "*"
            ]
          }
        },
        "MinTTL": 0,
        "DefaultTTL": 86400,
        "MaxTTL": 31536000
      },
      {
        "PathPattern": "fgh/*",
        "TargetOriginId":"xyz/Prod",
        "TrustedSigners": {
          "Enabled": false,
          "Quantity": 0
        },
        "TrustedKeyGroups": {
          "Enabled": false,
          "Quantity": 0
        },
        "ViewerProtocolPolicy": "redirect-to-https",
        "AllowedMethods": {
          "Quantity": 7,
          "Items": [
            "HEAD",
            "DELETE",
            "POST",
            "GET",
            "OPTIONS",
            "PUT",
            "PATCH"
          ],
          "CachedMethods": {
            "Quantity": 3,
            "Items": [
              "HEAD",
              "GET",
              "OPTIONS"
            ]
          }
        },
        "SmoothStreaming": false,
        "Compress": false,
        "LambdaFunctionAssociations": {
          "Quantity": 0
        },
        "FunctionAssociations": {
          "Quantity": 0
        },
        "FieldLevelEncryptionId": "",
        "ForwardedValues": {
          "QueryString": true,
          "Cookies": {
            "Forward": "none"
          },
          "Headers": {
            "Quantity": 10,
            "Items": [
              "access-control-allow-origin",
              "authorization",
              "Origin",
              "access-control-allow-credentials",
              "access-control-max-age",
              "access-control-allow-headers",
              "cache-control",
              "access-control-allow-methods",
              "expirers",
              "pragma"
            ]
          },
          "QueryStringCacheKeys": {
            "Quantity": 1,
            "Items": [
              "*"
            ]
          }
        },
        "MinTTL": 0,
        "DefaultTTL": 0,
        "MaxTTL": 0
      }
    ]
  }

Looking for a solution to make bulk change in CacheBehaviour for all TargetOriginID's

CodePudding user response:

Fixed (i.e. made valid) and reduced your JSON input down to the necessary minimum (the M in MRE), that is 50 lines.

{
  "CacheBehaviors": {
    "Quantity": 2,
    "Items": [
      {
        "PathPattern": "jkl/*",
        "TargetOriginId": [
          "nkl"
        ],
        "TrustedSigners": {
          "Enabled": false,
          "Quantity": 0
        },
        "TrustedKeyGroups": {
          "Enabled": false,
          "Quantity": 0
        },
        "ViewerProtocolPolicy": "redirect-to-https"
      },
      {
        "PathPattern": "fgh/*",
        "TargetOriginId": [
          "xyz"
        ],
        "TrustedSigners": {
          "Enabled": false,
          "Quantity": 0
        },
        "TrustedKeyGroups": {
          "Enabled": false,
          "Quantity": 0
        },
        "ViewerProtocolPolicy": "redirect-to-https"
      }
    ]
  },
  "CustomErrorResponses": {
    "Quantity": 0
  },
  "Comment": "vvvv",
  "Logging": {
    "Enabled": true,
    "IncludeCookies": false,
    "Bucket": "abc.s3.amazonaws.com",
    "Prefix": "std"
  },
  "WebACLId": "",
  "HttpVersion": "http2",
  "IsIPV6Enabled": true
}

You probably want to run a jq program similar to the following:

(.CacheBehaviors.Items[].TargetOriginId | select(. as $id | "nkl" | IN($id[]))) = ["wkl"]

It selects all TargetOriginIds which contain the value "nkl" and changes the list to only contain "wkl".

Output:

{
  "CacheBehaviors": {
    "Quantity": 2,
    "Items": [
      {
        "PathPattern": "jkl/*",
        "TargetOriginId": [
          "wkl"
        ],
        "TrustedSigners": {
          "Enabled": false,
          "Quantity": 0
        },
        "TrustedKeyGroups": {
          "Enabled": false,
          "Quantity": 0
        },
        "ViewerProtocolPolicy": "redirect-to-https"
      },
      {
        "PathPattern": "fgh/*",
        "TargetOriginId": [
          "xyz"
        ],
        "TrustedSigners": {
          "Enabled": false,
          "Quantity": 0
        },
        "TrustedKeyGroups": {
          "Enabled": false,
          "Quantity": 0
        },
        "ViewerProtocolPolicy": "redirect-to-https"
      }
    ]
  },
  "CustomErrorResponses": {
    "Quantity": 0
  },
  "Comment": "vvvv",
  "Logging": {
    "Enabled": true,
    "IncludeCookies": false,
    "Bucket": "abc.s3.amazonaws.com",
    "Prefix": "std"
  },
  "WebACLId": "",
  "HttpVersion": "http2",
  "IsIPV6Enabled": true
}

From the question it is unclear if "TargetOriginId": ["nkl", "xyz"] should become ["wkl"] or ["wkl", "xyz"]. Or whether ["nkl", "xyz"] is a match because it contains different values too. Maybe you only want to select those where the full TargetOriginId array matches?

If you only want to match items with single-valued target origin ids, the program becomes a bit simpler:

(.CacheBehaviors.Items[].TargetOriginId | select(. == ["nkl"])) = ["wkl"]

If your target origin ids are always an array and you want to change say "[abc,nkl,xyz]" to "[abc,REPLACED,xyz]", then select only those array elements and assign them the new value.

New input (as I understood it, the Q is quite vague on this):

{
  "CacheBehaviors": {
    "Quantity": 2,
    "Items": [
      {
        "PathPattern": "jkl/*",
        "TargetOriginId": [
          "nkl",
          "something else"
        ]
      },
      {
        "PathPattern": "fgh/*",
        "TargetOriginId": [
          "xyz",
          "abc"
        ]
      }
    ]
  },
  "IsIPV6Enabled": true
}

jq:

(.CacheBehaviors.Items[].TargetOriginId[] | select(. == "nkl")) = "wkl"

Output:

{
  "CacheBehaviors": {
    "Quantity": 2,
    "Items": [
      {
        "PathPattern": "jkl/*",
        "TargetOriginId": [
          "wkl",
          "something else"
        ]
      },
      {
        "PathPattern": "fgh/*",
        "TargetOriginId": [
          "xyz",
          "abc"
        ]
      }
    ]
  },
  "IsIPV6Enabled": true
}

CodePudding user response:

As of this writing, the posted JSON is invalid, so it's not clear exactly what is needed, but the following is illustrative:

jq --arg newvalue xyz '
  (.. | objects | select(has("TargetOriginId")) | .TargetOriginId) |= $newvalue
'
  • Related