最新のスナップショットをあるリージョンから別のリージョンに移動するPythonスクリプトを作成しました。スクリプトは、私のマシンでローカルに資格情報を使用してEC2インスタンスIDを移動しますが、これが以下で機能するかどうかはわかりません。
コード:
import boto3
SOURCE_REGION = 'us-east-2'
DESTINATION_REGION = 'us-east-1'
# Connect to EC2 in Source region
source_client = boto3.client('ec2', region_name=SOURCE_REGION)
# Get a list of all snapshots, then sort them
snapshots = source_client.describe_snapshots(OwnerIds=['self'])
snapshots_sorted = sorted([(s['SnapshotId'], s['StartTime']) for s in snapshots['Snapshots']], key=lambda k: k[1])
latest_snapshot = snapshots_sorted[-1][0]
print ('Latest Snapshot ID is ' + latest_snapshot)
# Connect to EC2 in Destination region
destination_client = boto3.client('ec2', region_name=DESTINATION_REGION)
# Copy the snapshot
response = destination_client.copy_snapshot(
SourceSnapshotId=latest_snapshot,
SourceRegion=SOURCE_REGION,
Description='This is my copied snapshot'
)
print ('Copied Snapshot ID is ' + response['SnapshotId'])
システムマネージャーを使用してAWSからgithubでPythonコードを実行できるというこのリンクを見つけました(https://docs.aws.amazon.com/systems-manager/latest/userguide/integration-remote-scripts.html#integration-github -python)。「GitHubからPythonスクリプトを実行する」というタイトルを探します。
ガイドには、githubでコードを実行するための次のコマンドがあります。
{"owner":"owner_name", "repository": "repository_name", "path": "path_to_scripts_or_directory", "tokenInfo":"{{ssm-secure:SecureString_parameter_name}}" }
とにかく、上記のコードにタイマーを追加して、Pythonスクリプトがタイマーに従って実行されるようにすることはできますか?
Pythonスクリプトをリモートで実行するためのすべての提案を開きます。
前もって感謝します。
編集:
他の場所で資格情報を構成しないように、コードを介してAPIキーをフィードできますか?
Edit2:
コードをラムダ関数に入れようとしています。に圧縮AWS_Backup.py
されAWS_Backup.py.zip
ます。ハンドラー情報もAWS_Backup.handler
AWS内で変更されます。
コードの更新:
from __future__ import print_function
def lambda_handler(event, context):
SOURCE_REGION = 'us-east-2'
DESTINATION_REGION = 'us-east-1'
# Connect to EC2 in Source region
source_client = boto3.client('ec2', region_name=SOURCE_REGION)
# Get a list of all snapshots, then sort them
snapshots = source_client.describe_snapshots(OwnerIds=['self'])
snapshots_sorted = sorted([(s['SnapshotId'], s['StartTime']) for s in snapshots['Snapshots']], key=lambda k: k[1])
latest_snapshot = snapshots_sorted[-1][0]
print ('Latest Snapshot ID is ' + latest_snapshot)
# Connect to EC2 in Destination region
destination_client = boto3.client('ec2', region_name=DESTINATION_REGION)
# Copy the snapshot
response = destination_client.copy_snapshot(
SourceSnapshotId=latest_snapshot,
SourceRegion=SOURCE_REGION,
Description='This is my copied snapshot'
)
print ('Copied Snapshot ID is ' + response['SnapshotId'])
エラー: "errorMessage": "Handler 'handler' missing on module 'AWS_Backup'"
解決:
change the lambda handler in AWS console from 'AWS_Backup.handler' to AWS_Backup.lambda_handler. Plus some role permissions were added to give me more access to IAM role.
SSMを使用してgithubから直接コードを実行できるとは知りませんでした。
純粋なSSMソリューションと結婚していない場合は、ラムダを組み込むことができます
2つのオプション。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加