排程器 CLI - AWS 上的執行個體排程器

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

排程器 CLI

AWS 命令列界面 (CLI) 上的執行個體排程器可讓您設定排程和期間,並預估特定排程的成本節省。

先決條件

此解決方案中的 CLI 需要 Python 3.8+ 和最新版本的 boto3。

登入資料

若要使用排程器 CLI,您必須擁有 AWS CLI 的登入資料。如需詳細資訊,請參閱《AWS CLI 使用者指南》中的組態和登入資料檔案設定

您的登入資料必須具有下列許可:

  • lambda:InvokeFunction - 在排程器堆疊中叫用 InstanceSchedulerMain 函數,並從命令列更新排程器組態資料庫中的排程和期間資訊

  • cloudformation:DescribeStackResource - 從堆疊擷取 AWS Lambda 函數的實體資源 ID,以處理 CLI 請求

排程器 CLI 提出的請求和回應會記錄在AdminCliRequestHandler-yyyymmdd日誌串流中。

注意

如果您使用 profile-name 引數指定設定檔,您指定的設定檔必須具有這些許可。如需 profile-name 引數的詳細資訊,請參閱常見引數

安裝排程器 CLI

  1. 下載排程器 CLI 套件 (instance_scheduler_cli.zip),並將其放在電腦上的目錄中。

    重要

    如果您未將檔案放入自己的目錄中,安裝將會失敗,然後從該目錄中安裝它們。

  2. 將 zip 封存解壓縮至自己的目錄 (instance_scheduler_cli)。

  3. 從您放置解壓縮 CLI 套件的相同目錄中,將 scheduler-cli 安裝到您的環境:

    注意

    Scheduler-CLI 需要 Python 3.8 或更新版本,以及最新版的 pip 和 boto3。如果您的本機電腦上未安裝所有這些項目,請先參閱 pip 的官方文件以取得安裝指示,再嘗試安裝 Scheduler-CLI。

    pip install --no-index --find-links=instance_scheduler_cli instance_scheduler_cli
  4. 驗證安裝是否成功:

    scheduler-cli --help
注意

如果偏好,則可以使用與上述相同的程序安裝 CLI 的 sdist 和 。

命令結構

排程器 CLI 在命令列上使用分段結構。下一個部分指定排程器 CLI python 指令碼。排程器 CLI 具有命令,可指定要在期間和排程上執行的操作。操作的特定引數可以在命令列上以任何順序指定。

scheduler-cli <command> <arguments>

常見引數

排程器 CLI 支援所有命令可以使用的下列引數:

引數 說明

--stack [replaceable]<stackname>

排程器堆疊的名稱。

重要:所有命令都需要此引數。

--region [replaceable]<regionname>

部署排程器堆疊的區域名稱。

注意:當預設組態和登入資料檔案未安裝在與解決方案堆疊相同的區域中時,您必須使用此引數。

--profile-name [replaceable] <profilename>

用來執行命令的設定檔名稱。如果未指定設定檔名稱,則會使用預設設定檔。

--query

控制命令輸出的 JMESPath 表達式。如需控制輸出的詳細資訊,請參閱《AWS CLI 使用者指南》中的從 AWS 命令列界面控制命令輸出

--help

顯示排程器 CLI 的有效命令和引數。與特定命令搭配使用時,會顯示該命令的有效子命令和引數。

--version

顯示排程器 CLI 的版本編號。

可用命令

create-period

說明

建立期間。期間必須至少包含下列其中一個項目:begintimemonths、、 endtime weekdaysmonthdays

引數

--name
  • 期間的名稱

    類型:字串

    必要:是

--description
  • 期間的描述

    類型:字串

    必要:否

--begintime
  • 執行期間開始的時間。如果endtime未指定 begintime和 ,則執行期間為 00:00 - 23:59。

    類型:字串

    限制條件: H:MMHH:MM 格式

    必要:否

--endtime
  • 執行期間停止的時間。如果endtime未指定 begintime和 ,則執行期間為 00:00 - 23:59。

    類型:字串

    限制條件: H:MMHH:MM 格式

    必要:否

--weekdays
  • 該期間的星期幾

    類型:字串

    限制條件:縮寫日名稱 (mon) 或數字 (0) 的逗號分隔清單。使用 - 指定範圍。使用 / 指定一週中的每 n^th^ 天。

    必要:否

--months
  • 期間的月份

    類型:字串

    限制條件:縮寫月份名稱 (jan) 或數字 (1) 的逗號分隔清單。使用 - 指定範圍。使用 / 指定每個 n^th^ 月。

    必要:否

--monthdays
  • 該期間當月的天數

    類型:字串

    限制條件:縮寫月份名稱 (jan) 或數字 (1) 的逗號分隔清單。使用 - 指定範圍。使用 / 指定每月的每個 n^th^ 天。

    必要:否

範例

$ scheduler-cli create-period --name "weekdays" --begintime 09:00 --endtime 18:00 --weekdays mon-fri --stack Scheduler
{
   "Period": {
      "Name": "weekdays",
      "Endtime": "18:00",
      "Type": "period",
      "Begintime": "09:00",
      "Weekdays": [
         "mon-fri"
      ]
   }
    }

create-schedule

說明

建立排程。

引數

--name
  • 排程的名稱

    類型:字串

    必要:是

--description
  • 排程的描述

    類型:字串

    必要:否

--enforced
  • 強制執行執行個體的排程狀態

    必要:否

--use-metrics
  • 收集 Amazon CloudWatch 指標

    必要:否

--periods
  • 排程的執行期間清單。如果指定多個期間,如果其中一個期間評估為 ,解決方案將啟動執行個體true

    類型:字串

    限制條件:以逗號分隔的期間清單。使用 <period-name>@[replaceable]<instance type>指定期間的執行個體類型。例如 weekdays@t2.large

    必要:是

--retain-running
  • 如果在執行期間開始之前手動啟動執行個體, 會防止解決方案在執行期間結束時停止執行個體。

    必要:否

--ssm-maintenance-window
  • 將 AWS Systems Manager 維護時段新增為 Amazon EC2 執行個體排程的執行期間。

    類型:字串

    必要:否

--do-not-stop-new-instances
  • 如果執行個體在執行期間之外執行,請勿在第一次標記時停止執行個體

    必要:否

--timezone
  • 排程將使用的時區

    類型:字串陣列

    必要:否 (如果未使用此引數,則會使用主要解決方案堆疊的預設時區。)

--use-maintenance-window
  • 將 Amazon RDS 維護時段新增為 Amazon RDS 執行個體排程的執行期間,或將 AWS Systems Manager 維護時段新增為 Amazon EC2 執行個體排程的執行期間

    類型:true/false

    必要:否 (預設 true)

範例

$ scheduler-cli create-schedule --name LondonOfficeHours --periods weekdays,weekends --timezone Europe/London --stack Scheduler
{
   "Schedule": {
      "Enforced": false,
      "Name": "LondonOfficeHours",
      "StopNewInstances": true,
      "Periods": [
         "weekends",
         "weekdays"
      ],
      "Timezone": "Europe/London",
      "Type": "schedule"
   }
}

delete-period

--name
  • 適用期間的名稱

    類型:字串

    必要:是

重要

如果在現有排程中使用期間,您必須先從這些排程中移除該期間,才能將其刪除。

範例

$ scheduler-cli delete-period --name weekdays --stack Scheduler
{
   "Period": "weekdays"
}

delete-schedule

說明

刪除現有的排程

引數

--name
  • 適用排程的名稱

    類型:字串

    必要:是

範例

$ scheduler-cli delete-schedule --name LondonOfficeHours --stack Scheduler
{
   "Schedule": "LondonOfficeHours"
}

describe-periods

說明

列出執行個體排程器堆疊的設定期間

引數

--name
  • 您想要描述的特定期間名稱

    類型:字串

    必要:否

範例

$ scheduler-cli describe-periods --stack Scheduler
{
   "Periods": [
      {
         "Name": "first-monday-in-quarter",
         "Months": [
            "jan/3"
         ],
         "Type": "period",
         "Weekdays": [
            "mon#1"
         ],
         "Description": "Every first Monday of each quarter"
      },
      {
         "Description": "Office hours",
         "Weekdays": [
            "mon-fri"
         ],
         "Begintime": "09:00",
         "Endtime": "17:00",
         "Type": "period",
         "Name": "office-hours"
      },

      {
         "Name": "weekdays",
         "Endtime": "18:00",
         "Type": "period",
         "Weekdays": [
            "mon-fri"
         ],
         "Begintime": "09:00"
      },
      {
         "Name": "weekends",
         "Type": "period",
         "Weekdays": [
            "sat-sun"
         ],
         "Description": "Days in weekend"
      }
   ]
}

describe-schedules

說明

列出執行個體排程器堆疊的已設定排程。

引數

--name
  • 您想要描述的特定排程名稱

    類型:字串

    必要:否

範例

$ scheduler-cli describe-schedules --stack Scheduler

{
   "Schedules": [
      {
         "OverrideStatus": "running",
         "Type": "schedule",
         "Name": "Running",
         "UseMetrics": false
      },
      {
         "Timezone": "UTC",
         "Type": "schedule",
         "Periods": [
            "working-days@t2.micro",
            "weekends@t2.nano"
         ],
         "Name": "scale-up-down"
      },
      {
         "Timezone": "US/Pacific",
         "Type": "schedule",
         "Periods": [
            "office-hours"
         ],
         "Name": "seattle-office-hours"
      },
      {
         "OverrideStatus": "stopped",
         "Type": "schedule",
         "Name": "stopped",
         "UseMetrics": true
      }
   ]
}

describe-schedule-usage

說明

列出排程內執行的所有期間,並計算執行個體的計費時數。使用此命令來模擬排程以計算潛在節省,並在建立或更新排程後執行期間。

引數

--name
  • 適用排程的名稱

    類型:字串

    必要:是

--startdate
  • 用於計算的期間開始日期。預設日期是目前日期。

    類型:字串

    必要:否

--enddate
  • 用於計算的期間結束日期。預設日期是目前日期。

    類型:字串

    必要:否

範例

$ scheduler-cli describe-schedule-usage --stack InstanceScheduler --name seattle-office-hours
{
   "Usage": {
      "2017-12-04": {
         "BillingHours": 8,
         "RunningPeriods": {
            "Office-hours": {
               "Begin": "12/04/17 09:00:00",
               "End": "12/04/17 17:00:00",
               "BillingHours": 8,
               "BillingSeconds": 28800
            }
         },
         "BillingSeconds": 28800
      }
   },
   "Schedule": "seattle-office-hours"

update-period

說明

更新現有期間

引數

update-period 命令支援與 create-period命令相同的引數。如需引數的詳細資訊,請參閱建立期間命令

重要

如果您未指定引數,該引數將從期間中移除。

update-schedule

說明

更新現有的排程

引數

update-schedule 命令支援與 create-schedule命令相同的引數。如需引數的詳細資訊,請參閱建立排程命令

重要

如果您未指定 引數,則該引數將從排程中移除。

help

說明

顯示排程器 CLI 的有效命令和引數清單。

範例

$ scheduler-cli --help
usage: scheduler-cli [-h] [--version]
                     {create-period,create-schedule,delete-period,delete-schedule,describe-periods,describe-schedule-usage,describe-schedules,update-period,update-schedule}
                     ...

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit

subcommands:
  Valid subcommands

  {create-period,create-schedule,delete-period,delete-schedule,describe-periods,describe-schedule-usage,describe-schedules,update-period,update-schedule}
                        Commands help
    create-period       Creates a period
    create-schedule     Creates a schedule
    delete-period       Deletes a period
    delete-schedule     Deletes a schedule
    describe-periods    Describes configured periods
    describe-schedule-usage
                        Calculates periods and billing hours in which
                        instances are running
    describe-schedules  Described configured schedules
    update-period       Updates a period
    update-schedule     Updates a schedule

與特定命令搭配使用時,引--help數會顯示該命令的有效子命令和引數。

特定命令範例

$ scheduler-cli describe-schedules --help
usage: scheduler-cli describe-schedules [-h] [--name NAME] [--query QUERY]
                                        [--region REGION] --stack STACK

optional arguments:
  -h, --help         show this help message and exit
  --name NAME        Name of the schedule
  --query QUERY      JMESPath query to transform or filter the result
  --region REGION    Region in which the Instance Scheduler stack is
                        deployed
  --stack STACK, -s STACK
                     Name of the Instance Scheduler stack