
Pythonで
・EC2を開始
できます!

この記事では
・プロファイルを指定した方法
紹介します!
※ローカルから実行します。Lambda関数からではなく。
前提
①外部ライブラリ「boto3」がインストールされていること。
pipコマンドでインストールする場合、以下でインストールできます。
pip install boto3
②AWS CLIのプロファイルが設定済みであること。
コード
from boto3.session import Session
from botocore.exceptions import ClientError
# EC2のインスタンスID
InstanceId = ["i-xxxxxxxxxxxxxxxxx"]
# プロファイル名
profile = "develop"
try:
session = Session(profile_name=profile)
ec2 = session.client(service_name="ec2")
# EC2を開始
ec2.start_instances(InstanceIds=InstanceId)
print("EC2を開始しました。")
except ClientError as e:
print("エラーが発生しました。")
print("エラーコード:" + e.response["Error"]["Code"])
print("エラーメッセージ:" + e.response["Error"]["Message"])
except Exception as e:
print("エラーが発生しました。")
print(e)
実行結果
プロファイルを指定して、EC2を開始できました。

参考
上記のコードは以下の公式サイトを参考にして作成しました。