如何用AWS CLI筛选含指定注释的CloudFront分发?解决查询语法报错
解决AWS CloudFront查询Comment包含指定字符串的NoneType错误
你的问题出在部分CloudFront分发的Comment字段是空值(None),直接用字符串包含判断会触发类型错误。以下是正确的语法:
基础查询命令
aws cloudfront list-distributions --query "DistributionList.Items[?Comment != null && contains(Comment, 'vanity url')]"
关键逻辑说明
Comment != null:先过滤掉Comment为空的分发,避免NoneType报错contains(Comment, 'vanity url'):检查Comment字符串中是否包含目标子串
Jenkins流水线中使用示例
如果是在Jenkins的sh步骤里执行,用单引号包裹脚本块避免转义问题:
sh ''' aws cloudfront list-distributions --query "DistributionList.Items[?Comment != null && contains(Comment, 'vanity url')]" '''
可选:精简输出字段
如果只需要特定信息(比如分发ID、域名),可以追加字段筛选:
aws cloudfront list-distributions --query "DistributionList.Items[?Comment != null && contains(Comment, 'vanity url')].[Id, DomainName]"
内容的提问来源于stack exchange,提问作者Chris F




