撰寫 Node.js 多重檢查藍圖的 JSON 組態 - Amazon CloudWatch

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

撰寫 Node.js 多重檢查藍圖的 JSON 組態

Node.js 多檢查藍圖可讓您建立 Canary,在單一 Canary 執行中執行多個驗證檢查。當您想要測試多個端點、驗證應用程式的不同層面,或依序執行一系列相關檢查時,此藍圖非常有用。

根組態結構

根組態會定義進階 API 藍圖 Canary 的整體結構。

結構描述屬性
屬性 Type 必要 Description
globalSettings 物件 套用至所有步驟的預設組態
variables 物件 跨步驟的可重複使用值 (上限為 10)
steps 物件 監控步驟的集合 (1-10 個步驟)

範例

{ "globalSettings": { "stepTimeout": 30000, "userAgent": "CloudWatch-Synthetics-Advanced/1.0" }, "variables": { "baseUrl": "https://api.example.com", "apiVersion": "v1" }, "steps": { "1": { "stepName": "healthCheck", "checkerType": "HTTP", "url": "${baseUrl}/health", "httpMethod": "GET" } } }

驗證規則

  • 必須至少包含一個步驟

  • 允許最多 10 個步驟

  • 不允許超過 globalSettingsvariables和 的其他屬性 steps

全域設定

除非在步驟層級覆寫,否則全域設定會提供適用於所有步驟的預設組態。

屬性

全域設定屬性
屬性 Type 預設 範圍 Description
stepTimeout integer 30000 5000-300000 所有步驟的預設逾時 (毫秒)

範例

{ "globalSettings": { "stepTimeout": 60000, } }

變數和資料管理

變數可讓您定義可重複使用的值,這些值可以使用${variableName}語法在整個組態中參考。

變數屬性

屬性 Type 說明
變數名稱 string 必須符合模式 ^[a-zA-Z][a-zA-Z0-9_]*$
變數值 string 任何字串值

限制

  • 每個組態最多 10 個變數

  • 變數名稱必須以字母開頭

  • 變數名稱只能包含字母、數字和底線

  • 結構描述中未指定的長度上限

範例

{ "variables": { "baseUrl": "https://api.example.com", "apiKey": "${AWS_SECRET:my-api-key}", "timeout": "30000", "userEmail": "test@example.com" } }

組態用量

{ "steps": { "1": { "url": "${baseUrl}/users", "timeout": "${timeout}", "headers": { "Authorization": "Bearer ${apiKey}" } } } }

步驟定義

步驟定義個別監控操作。每個步驟的編號從 1 到 10,並包含特定類型的檢查。

常見步驟屬性

屬性 Type 必要 描述
stepName string 步驟的唯一識別符
checkerType string 檢查類型:HTTPDNSSSLTCP
extractors 陣列 資料擷取組態

步驟名稱驗證

  • 模式 - ^【a-zA-Z】【a-zA-Z0-9_-】*$

  • 長度上限 - 64 個字元

  • 必須以字母開頭

步驟編號

  • 步驟編號為字串索引鍵:"1"、"2"、...、"10"

  • 模式:^(【1-9】|10)$

  • 至少需要 1 個步驟

  • 允許最多 10 個步驟

範例

{ "steps": { "1": { "stepName": "loginAPI", "checkerType": "HTTP", "url": "https://api.example.com/login", "httpMethod": "POST" }, "2": { "stepName": "dnsCheck", "checkerType": "DNS", "domain": "example.com" } } }

檢查類型

HTTP 檢查

透過全面的請求和回應驗證來監控 Web 端點和 APIs。

必要屬性

屬性 Type Description
url string 目標 URL (必須是有效的 URI 格式)
httpMethod string HTTP 方法:GETPOSTPUTPATCHDELETEHEAD、、 OPTIONS

選用屬性

屬性 Type 預設 範圍 Description
timeout integer 30000 5000-300000 請求逾時 (毫秒)
waitTime integer 0 0-60 請求之前的延遲 (秒)
headers object - - 自訂 HTTP 標頭
body string - - POST/PUT 操作的請求內文
authentication object - - 身分驗證組態
assertions 陣列 - - 回應驗證規則

範例

{ "stepName": "createUser", "checkerType": "HTTP", "url": "https://api.example.com/users", "httpMethod": "POST", "timeout": 15000, "headers": { "Content-Type": "application/json", "X-API-Version": "v1" }, "body": "{\"name\":\"John Doe\",\"email\":\"john@example.com\"}", "authentication": { "type": "API_KEY", "apiKey": "${AWS_SECRET:api-credentials}", "headerName": "X-API-Key" }, "assertions": [ { "type": "STATUS_CODE", "operator": "EQUALS", "value": 201 } ] }

DNS 檢查

驗證 DNS 解析並記錄資訊。

必要屬性

屬性 Type Description
domain string 要查詢的網域名稱 (主機名稱格式)

選用屬性

屬性 Type 預設 Description
recordType string 「A」 DNS 記錄類型:ACNAMEMXTXTNS
nameserver string - 要查詢的特定 DNS 伺服器
timeout integer 30000 查詢逾時 (5000-300000ms)
port integer 53 DNS 伺服器連接埠 (1-65535)
protocol string 「UDP」 通訊協定: UDPTCP
assertions 陣列 - DNS 回應驗證規則

範例

{ "stepName": "dnsResolution", "checkerType": "DNS", "domain": "example.com", "recordType": "A", "nameserver": "8.8.8.8", "timeout": 10000, "assertions": [ { "type": "RECORD_VALUE", "operator": "CONTAINS", "value": "192.168" } ] }

SSL 檢查

監控 SSL 憑證運作狀態和組態。

必要屬性

屬性 Type Description
hostname string 目標主機名稱 (主機名稱格式)

選用屬性

屬性 Type 預設 描述
port integer 443 SSL 連接埠 (1-65535)
timeout integer 30000 連線逾時 (5000-300000ms)
sni boolean TRUE 伺服器名稱指示
verifyHostname boolean TRUE 主機名稱驗證
allowSelfSigned boolean FALSE 接受自我簽署憑證
assertions 陣列 - 憑證驗證規則

範例

{ "stepName": "sslCertCheck", "checkerType": "SSL", "hostname": "secure.example.com", "port": 443, "sni": true, "verifyHostname": true, "assertions": [ { "type": "CERTIFICATE_EXPIRY", "operator": "GREATER_THAN", "value": 30, "unit": "DAYS" } ] }

TCP 檢查

測試 TCP 連接埠連線和回應驗證。

必要屬性

屬性 Type Description
hostname string 目標主機名稱 (主機名稱格式)
port integer 目標連接埠 (1-65535)

選用屬性

屬性 Type 預設 描述
timeout integer 30000 整體逾時 (5000-300000ms)
connectionTimeout integer 3000 連線逾時 (5000-300000ms)
readTimeout integer 2000 資料讀取逾時 (5000-300000ms)
sendData string - 連線後要傳送的資料
expectedResponse string - 預期的回應資料
encoding string 「UTF-8」 資料編碼:UTF-8ASCIIHEX
assertions 陣列 - 連線和回應驗證

範例

{ "stepName": "databaseConnection", "checkerType": "TCP", "hostname": "db.example.com", "port": 3306, "connectionTimeout": 5000, "sendData": "SELECT 1", "expectedResponse": "1", "assertions": [ { "type": "CONNECTION_SUCCESSFUL", "value": true } ] }

身分驗證方法

無身分驗證

{ "type": "NONE" }

基本身分驗證

屬性 Type 必要 描述
type string 必須為 "BASIC"
username string 身分驗證的使用者名稱
password string 用於身分驗證的密碼

範例

{ "type": "BASIC", "username": "admin", "password": "${AWS_SECRET:basic-auth:password}" }

API 金鑰身分驗證

屬性 Type 必要 預設 Description
type string - 必須為 "API_KEY"
apiKey string - API 金鑰值
headerName string "X-API-Key" API 金鑰的標頭名稱

範例

{ "type": "API_KEY", "apiKey": "${AWS_SECRET:api-credentials}", "headerName": "Authorization" }

OAuth 用戶端登入資料

屬性 Type 必要 預設 Description
type string - 必須為 "OAUTH_CLIENT_CREDENTIALS"
tokenUrl string - OAuth 字符端點 URL
clientId string - OAuth 用戶端 ID
clientSecret string - OAuth 用戶端秘密
scope string - OAuth 範圍
audience string - OAuth 對象
resource string - OAuth 資源
tokenApiAuth 陣列 - Token API 驗證方法:BASIC_AUTH_HEADERREQUEST_BODY
tokenCacheTtl integer 3600 字符快取 TTL (至少 60 秒)

範例

{ "type": "OAUTH_CLIENT_CREDENTIALS", "tokenUrl": "https://auth.example.com/oauth/token", "clientId": "${AWS_SECRET:oauth-creds:client_id}", "clientSecret": "${AWS_SECRET:oauth-creds:client_secret}", "scope": "read write", "tokenCacheTtl": 7200 }

AWS Signature (第 4 版)

屬性 Type 必要 描述
type string 必須為 "SIGV4"
service string AWS 服務的名稱 (例如,「execute-api」)
region string AWS 區域
roleArn string 用於簽署的 IAM 角色 ARN

範例

{ "type": "SIGV4", "service": "execute-api", "region": "us-east-1", "roleArn": "arn:aws:iam::123456789012:role/SyntheticsRole" }

聲明和驗證

HTTP 聲明

狀態碼聲明

屬性 Type 必要 描述
type string 必須為 "STATUS_CODE"
operator string EQUALS, NOT_EQUALS, GREATER_THAN, LESS_THAN, IN_RANGE
value integer 有條件 HTTP 狀態碼 (100-599)
rangeMin integer 有條件 最小範圍值 (適用於 IN_RANGE)
rangeMax integer 有條件 最大範圍值 (適用於 IN_RANGE)
{ "type": "STATUS_CODE", "operator": "EQUALS", "value": 200 }

回應時間聲明

屬性 Type 必要 預設 Description
type string - 必須為 "RESPONSE_TIME"
operator string - LESS_THAN, GREATER_THAN, EQUALS
value 數字 - 時間值 (最小值為 0)
unit string 「MILLISECONDS」 必須為 "MILLISECONDS"
{ "type": "RESPONSE_TIME", "operator": "LESS_THAN", "value": 500, "unit": "MILLISECONDS" }

頭部聲明

屬性 Type 必要 描述
type string 必須為 "HEADER"
headerName string 要驗證的標頭名稱
operator string EQUALS, NOT_EQUALS, CONTAINS, NOT_CONTAINS, REGEX_MATCH, EXIST
value string/布林值 有條件 預期值 (EXIST運算子的布林值)
{ "type": "HEADER", "headerName": "Content-Type", "operator": "CONTAINS", "value": "application/json" }

內文聲明

屬性 Type 必要 預設 Description
type string - 必須為 "BODY"
target string 「JSON」 JSONTEXT
path string 有條件 - JSONPath (JSON 目標需要)
operator string - CONTAINS, NOT_CONTAINS, EQUALS, NOT_EQUALS, EXISTS
value string/布林值 - 預期值 (EXISTS運算子的布林值)
{ "type": "BODY", "target": "JSON", "path": "$.users[0].name", "operator": "EQUALS", "value": "John Doe" }

DNS 聲明

記錄值宣告

屬性 Type 必要 範圍 Description
type string - 必須為 "RECORD_VALUE"
operator string - EQUALS, NOT_EQUALS, CONTAINS, NOT_CONTAINS, REGEX_MATCH
value string - 預期的記錄值

記錄計數宣告

屬性 Type 必要 範圍 Description
type string - 必須為 "RECORD_COUNT"
operator string - EQUALS, GREATER_THAN, LESS_THAN
value integer ≥ 0 預期計數 (最低 0)

授權聲明

屬性 Type 必要 範圍 Description
type string - 必須為 "AUTHORITATIVE"
value boolean - 預期的授權狀態

TTL 聲明

屬性 Type 必要 範圍 Description
type string - 必須為 "TTL"
operator string - EQUALS, GREATER_THAN, LESS_THAN
value integer ≥ 0 預期的 TTL (最低 0)

SSL 聲明

憑證過期聲明

屬性 Type 必要 預設 Description
type string - 必須為 "CERTIFICATE_EXPIRY"
operator string - GREATER_THAN, LESS_THAN
value integer - 時間值 (最小值為 0)
unit string 「天」 DAYS, HOURS

憑證主體聲明

屬性 Type 必要 預設 Description
type string - 必須為 "CERTIFICATE_SUBJECT"
field string - 主旨欄位:CNOOUCSTL
operator string - CONTAINS, EQUALS, REGEX_MATCH
value string - 預期的欄位值

憑證發行者聲明

屬性 Type 必要 預設 Description
type string - 必須為 "CERTIFICATE_ISSUER"
field string - 發行者欄位:CNO
operator string - CONTAINS, EQUALS
value string - 預期的欄位值

TCP 聲明

連線成功聲明

屬性 Type 必要 預設 Description
type string - 必須為 "CONNECTION_SUCCESSFUL"
value boolean - 預期的連線狀態

回應資料聲明

屬性 Type 必要 預設 Description
type string - 必須為 "RESPONSE_DATA"
operator string - CONTAINS, EQUALS, NOT_CONTAINS, REGEX_MATCH, STARTS_WITH, ENDS_WITH
value string - 預期的回應資料
encoding string 「UTF-8」 UTF-8, ASCII, HEX

資料擷取

擷取器可讓您從回應擷取資料,以用於後續步驟或報告目的。

擷取屬性

屬性 Type 必要 預設 Description
name string - 擷取資料的變數名稱
type string - 擷取類型: BODY
path string - 用於內文擷取的 JSONPath
regex string - 規則表達式模式
regexGroup integer 0 Regex 擷取群組 (最低 0)

擷取名稱驗證

  • 模式:^[a-zA-Z][a-zA-Z0-9_]*$

  • 必須以字母開頭

  • 可包含字母、數字和底線

限制 – 替代不適用於結構描述中具有特定 ENUM 值的欄位

擷取類型

{ "name": "userId", "type": "BODY", "path": "$.user.id" }
{ "stepName": "loginAndExtract", "checkerType": "HTTP", "url": "https://api.example.com/login", "httpMethod": "POST", "body": "{\"username\":\"test\",\"password\":\"pass\"}", "extractors": [ { "name": "textVariable", "type": "BODY", "path": "$.myvalue" } ] }, { "stepName": "substituteVariable", "checkerType": "HTTP", "url": "https://api.example.com/get/${textVariable}", "httpMethod": "GET", "assertions": [ { "type": "BODY", "target": "JSON", "path": "$.users[0].name", "operator": "EQUALS", "value": "${textVariable}" } ] }