# CDN缓存刷新

密钥权限：

* CreateInvalidation （创建失效）
* ListDistributions  (分配列表)

<figure><img src="/files/HWXyu6Va3FuFqKevdzCN" alt=""><figcaption></figcaption></figure>

```bash
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws -version
yum install jq -y
```

**例如： 刷新包含 back 字符域名的缓存**

```bash
./auto-clean-aws-cdn-cache.sh  back
```

```bash
#!/bin/bash


# 传参
if [ "$#" -ne 1 ]; then
    echo "[ERROR] 传参用法: $0  <search_string>"
    exit 1
fi

# 从参数中获取搜索条件
SEARCH_STRING=$1
echo "[INFO] 打印模糊搜索失效匹配参数 => $SEARCH_STRING"

# 失效缓存路径,如果多个路径，空格分开
INVALIDATION_PATH="/" 

# 设置 AWS 密钥信息
export AWS_ACCESS_KEY_ID="AWS_ACCESS_KEY_ID"
export AWS_SECRET_ACCESS_KEY="AWS_ACCESS_KEY_ID"

# 设置 TG 信息
# telegram
TELEGRAM_BOT_TOKEN="TELEGRAM_BOT_TOKEN"
TELEGRAM_CHAT_ID="TELEGRAM_CHAT_ID"

# 获取所有 CloudFront 分配
distribution_data=$(aws cloudfront list-distributions --output json)

# 提取匹配的分配
matching_distributions=$(echo "$distribution_data" | jq -r '
  .DistributionList.Items[] |
  select(.Aliases.Items[] | contains("'"$SEARCH_STRING"'")) |
  {
    Id: .Id,
    DomainName: .DomainName,
    Aliases: .Aliases.Items[]
  } |
  "\(.Id) \(.DomainName) \(.Aliases)"
' | sort -u)

matching_ids=$(echo "$distribution_data" | jq -r '
  .DistributionList.Items[] |
  select(.Aliases.Items[] | contains("'"$SEARCH_STRING"'")) |
  .Id
' | sort -u)

matching_domains=$(echo "$distribution_data" | jq -r '
  .DistributionList.Items[] |
  select(.Aliases.Items[] | contains("'"$SEARCH_STRING"'")) |
  .Aliases.Items[]
' | sort -u)

# 检查是否找到匹配的分配
if [ -z "$matching_distributions" ]; then
    echo "[ERROR] 没有搜索到匹配项"
    exit 1
fi


# 打印匹配的分配
echo "[INFO] 打印匹配的分配: Id DomainNam Aliases"
#echo $matching_ids
#echo $matching_domains
echo "$matching_distributions"

echo  -e "\n"

# 循环处理
while IFS= read -r line; do
    distribution_id=$(echo "$line")
    echo "[INFO] 打印正在创建刷新缓存ID: $distribution_id"

    # 创建失效请求
    invalidation_status=$(aws cloudfront create-invalidation \
        --distribution-id "$distribution_id" \
        --paths "$INVALIDATION_PATH" \
        --output json | jq -r '.Invalidation.Status')

    echo "[INFO] 打印刷新缓存状态： Id => $distribution_id , Path => $INVALIDATION_PATH, Status => $invalidation_status"
done <<< "$matching_ids"






# 函数：发送 Telegram 消息
send_telegram_message() {
    local bot_token=$1
    local chat_id=$2
    local message=$3
    result=$(echo -e $message)
    # 发送 Telegram 消息
    curl -s -X POST "https://api.telegram.org/bot$bot_token/sendMessage" \
         -d chat_id="$chat_id" \
         -d text="$result" \
         -d parse_mode="Markdown"
}

domain=$(echo $matching_domains |tr " " "," )
echo "$matching_domain"
MESSAGE="云平台 => AWS CDN \n触发项目 => 交易所生产 \n触发动作 => 刷新缓存 \n触发原因 => 后台更新 \n触发条件 => $SEARCH_STRING\* \n刷新路径 => $INVALIDATION_PATH \n刷新域名 => $domain"

echo -e "$MESSAGE"
echo "[INFO] Sending Telegram message to chat ID: $CHAT_ID"
send_telegram_message "$TELEGRAM_BOT_TOKEN" "$TELEGRAM_CHAT_ID" "$MESSAGE"
echo "[INFO] Telegram message sent."
```

<figure><img src="/files/7JDCTt1gVzO6f8fVGNnC" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/go/aws/cdn-huan-cun-shua-xin.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
