You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Terraform自动更新aws_synthetics_canary的runtimeVersion?

如何自动化更新AWS Synthetics Canary的运行时版本?

当然可以实现自动化更新,下面是几种实用的方案,适配不同的运维流程:

方案1:Terraform结合外部脚本动态获取最新版本

如果你的Canary用Terraform管理,可以通过external数据源调用脚本,自动拉取AWS官方提供的最新非弃用运行时版本,避免硬编码版本号。

步骤:

  1. 编写一个Shell脚本(比如get-latest-runtime.sh),用AWS CLI查询最新版本:
#!/bin/bash
# 查询所有未弃用的运行时,按版本号排序后取最新的一个
aws synthetics describe-runtimes --query 'RuntimeVersions[?DeprecationDate==null] | sort_by(@, Version) | [-1].Version' --output text
  1. 在Terraform配置中引用这个脚本的输出:
# 调用外部脚本获取最新运行时版本
data "external" "latest_canary_runtime" {
  program = ["bash", "${path.module}/get-latest-runtime.sh"]
}

resource "aws_synthetics_canary" "canary" {
  name              = var.name
  handler           = "up.handler"
  # 直接使用动态获取的版本号
  runtime_version   = data.external.latest_canary_runtime.result.version
  
  # 其他必填配置(比如execution_role_arn、zip_file等)
}

每次执行terraform plan/apply时,都会自动拉取最新的可用版本,无需手动修改代码。

方案2:用CI/CD工具定期自动更新并提交变更

如果想定期自动检查更新并生成PR(或直接应用),可以用GitHub Actions、GitLab CI这类工具设置定时任务。

以GitHub Actions为例,配置一个每周运行的Workflow:

name: Auto-Update Canary Runtime Version
on:
  schedule:
    - cron: '0 0 * * 0' # 每周日凌晨UTC时间运行
  workflow_dispatch: # 支持手动触发

jobs:
  update-runtime:
    runs-on: ubuntu-latest
    steps:
      - name: 拉取代码
        uses: actions/checkout@v4

      - name: 配置AWS凭证
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1 # 替换为你的Canary所在区域

      - name: 获取最新非弃用运行时版本
        id: get-version
        run: |
          LATEST_VERSION=$(aws synthetics describe-runtimes --query 'RuntimeVersions[?DeprecationDate==null] | sort_by(@, Version) | [-1].Version' --output text)
          echo "latest_version=$LATEST_VERSION" >> $GITHUB_OUTPUT

      - name: 更新Terraform配置文件
        run: |
          # 替换main.tf中的runtime_version值
          sed -i "s/runtime_version      = \"syn-nodejs-puppeteer-.\"/runtime_version      = \"${{ steps.get-version.outputs.latest_version }}\"/" main.tf
          # 格式化Terraform代码(可选)
          terraform fmt

      - name: 创建Pull Request
        uses: peter-evans/create-pull-request@v5
        with:
          title: "更新Canary运行时版本至${{ steps.get-version.outputs.latest_version }}"
          body: "自动更新到AWS官方最新的非弃用Synthetics运行时版本"
          branch: auto-update-canary-runtime-${{ steps.get-version.outputs.latest_version }}

这个Workflow会每周自动检查最新版本,修改代码后创建PR,你只需要审核合并即可,也可以直接添加terraform apply步骤自动应用(需谨慎,建议先测试)。

方案3:用AWS Lambda定期直接更新Canary配置

如果不依赖Terraform,想直接在AWS内部完成自动化,可以写一个Lambda函数,定时触发更新Canary的运行时版本。

Lambda示例代码(Python):

import boto3

def lambda_handler(event, context):
    synthetics_client = boto3.client('synthetics')
    
    # 获取所有未弃用的运行时版本,按版本号倒序取第一个
    response = synthetics_client.describe_runtimes()
    active_runtimes = [rt for rt in response['RuntimeVersions'] if rt['DeprecationDate'] is None]
    active_runtimes.sort(key=lambda x: x['Version'], reverse=True)
    latest_version = active_runtimes[0]['Version']
    
    # 替换为你的Canary名称
    target_canary = "your-canary-name"
    
    try:
        # 更新Canary运行时版本
        synthetics_client.update_canary(
            Name=target_canary,
            RuntimeVersion=latest_version
        )
        print(f"成功将Canary {target_canary} 更新至运行时版本: {latest_version}")
    except Exception as e:
        print(f"更新失败: {str(e)}")
        raise e

然后给Lambda配置CloudWatch Events定时触发(比如每周一次),并确保Lambda拥有synthetics:UpdateCanary和synthetics:DescribeRuntimes的权限。

注意事项:

  • 所有方案都优先筛选未弃用的版本,避免更新到即将被淘汰的运行时;
  • 如果用Terraform管理,建议优先用方案1或2,避免直接修改资源导致Terraform状态漂移;
  • 自动化更新后,最好添加验证步骤(比如触发Canary测试运行),确保新版本下Canary能正常工作。

内容的提问来源于stack exchange,提问作者Mohammed Salah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 22:37:03