

# Python Canary スクリプトの記述
<a name="CloudWatch_Synthetics_Canaries_WritingCanary_Python"></a>

このスクリプトは、合格して正常な実行となり、文字列を返します。失敗した Canary がどのように見えるかを確認するには、fail = False を fail = True に変更します

```
def basic_custom_script():
    # Insert your code here
    # Perform multi-step pass/fail check
    # Log decisions made and results to /tmp
    # Be sure to wait for all your code paths to complete 
    # before returning control back to Synthetics.
    # In that way, your canary will not finish and report success
    # before your code has finished executing
    fail = False
    if fail:
        raise Exception("Failed basicCanary check.")
    return "Successfully completed basicCanary checks."
def handler(event, context):
    return basic_custom_script()
```

## Python Canary ファイルのパッケージング
<a name="CloudWatch_Synthetics_Canaries_WritingCanary_Python_package"></a>

複数の .py ファイルがある場合、またはスクリプトに依存関係がある場合は、それらすべてを単一の ZIP ファイルにバンドルできます。`syn-python-selenium-1.1` ランタイムを使用する場合、ZIP ファイルには、`python/my_canary_filename.py` などの `python` フォルダ内にメインの Canary .py ファイルが含まれている必要があります。` syn-python-selenium-1.1` 以降を使用する場合は、オプションで、`python/myFolder/my_canary_filename.py` などの別のフォルダを使用できます。

この ZIP ファイルには、必要なすべてのフォルダとファイルが含まれている必要がありますが、他のファイルは `python` フォルダ内にある必要はありません。

スクリプトのエントリポイントのファイル名および関数名に一致するように、Canary のスクリプトのエントリポイントを ` my_canary_filename.functionName` として設定します。`syn-python-selenium-1.0` ランタイムを使用している場合は、`functionName` は `handler` である必要があります。` syn-python-selenium-1.1` 以降を使用している場合、このハンドラー名の制限は適用されません。また、オプションで Canary を ` python/myFolder/my_canary_filename.py` などの別のフォルダに保存することもできます。別のフォルダに保存する場合は、スクリプトエントリポイントでそのパスを指定します (` myFolder/my_canary_filename.functionName` など)。

## Synthetics Canary を使用するようにするための既存の Selenium の変更
<a name="CloudWatch_Synthetics_Canaries_WritingCanary_Python_Selenium"></a>

Canary として使用するために、Python と Selenium の既存のスクリプトをすばやく変更できます。Selenium の詳細については、[www.selenium.dev/](https://www.selenium.dev/) をご参照ください。

この例では、次の Selenium スクリプトから始めます。

```
from selenium import webdriver

def basic_selenium_script():
    browser = webdriver.Chrome()
    browser.get('https://example.com')
    browser.save_screenshot('loaded.png')

basic_selenium_script()
```

変更の手順は次のとおりです。

**Selenium スクリプトを Canary として使用するように変換するには**

1. ` aws_synthetics` モジュールから Selenium を使用するように `import` ステートメントを変更します。

   ```
   from aws_synthetics.selenium import synthetics_webdriver as webdriver
   ```

   `aws_synthetics` の Selenium モジュールは、Canary がメトリクスとログを出力し、HAR ファイルを生成し、他の CloudWatch Synthetics 機能で動作することを保証します。

1. ハンドラ関数を作成し、Selenium メソッドを呼び出します。このハンドラーは、スクリプトのエントリポイント関数です。

   `syn-python-selenium-1.0` を使用している場合、ハンドラー関数には `handler` という名前を付ける必要があります。`syn-python-selenium-1.1` 以降を使用している場合、関数には任意の名前を付けることができますが、スクリプトで使用されている名前と同じである必要があります。また、`syn-python-selenium-1.1` 以降を使用している場合は、スクリプトを任意のフォルダに保存し、そのフォルダをハンドラー名の一部として指定できます。

   ```
   def handler(event, context):
       basic_selenium_script()
   ```

これで、スクリプトが CloudWatch Synthetics Canary に更新されました。更新されたスクリプトは次のとおりです。

`webdriver` は [SyntheticsWebDriver](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Synthetics_Canaries_Library_Python.html#CloudWatch_Synthetics_Library_Python_SyntheticsWebDriver) クラスのインスタンスであり、`webdriver.Chrome()` によって返されるブラウザは [SyntheticsBrowser](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Synthetics_Canaries_Library_Python.html#CloudWatch_Synthetics_Library_Python_SyntheticsBrowser) のインスタンスです。

```
from aws_synthetics.selenium import synthetics_webdriver as webdriver

def basic_selenium_script():
    browser = webdriver.Chrome()
    browser.get('https://example.com')
    browser.save_screenshot('loaded.png')

def handler(event, context):
    basic_selenium_script()
```

## 既存の Puppeteer Synthetics スクリプトを変更して非標準の証明書を認証する
<a name="Canaries_Non-Standard_Certificates"></a>

Synthetics Canaries の重要なユースケースの 1 つは、独自のエンドポイントをモニタリングすることです。外部トラフィックに対応していないエンドポイントをモニタリングする場合、信頼できるサードパーティーの認証局によって署名された適切な証明書が存在しない可能性があります。

このシナリオで考えられる解決策は、次の 2 つです。
+ クライアント証明書を認証するには、「[How to validate authentication using Amazon CloudWatch Synthetics – Part 2](https://aws.amazon.com/blogs/mt/how-to-validate-authentication-using-amazon-cloudwatch-synthetics-part-2/)」を参照してください。
+ 自己署名証明書を認証するには、「[How to validate authentication with self-signed certificates in Amazon CloudWatch Synthetics](https://aws.amazon.com/blogs/mt/how-to-validate-authentication-with-self-signed-certificates-in-amazon-cloudwatch-synthetics/)」を参照してください。

CloudWatch Synthetics Canary を使用する場合は、これら 2 つのオプションに限定されません。Canary コードを拡張することで、これらの機能を拡張し、ビジネスロジックを追加できます。

**注記**  
Python ランタイムで実行される Synthetics Canary は、もともと ` --ignore-certificate-errors` フラグが有効になっているため、これらの Canary が非標準の証明書構成のサイトに到達しても問題ないはずです。