0
0

EC2インスタンス起動テンプレートの作り方

Posted at

EC2インスタンス起動テンプレートの作り方

AWSのEC2インスタンスを手早く起動するためのテンプレートを作る。

起動テンプレートって何?

ざっくりEC2インスタンスのセッティングやスペックを保存したもの。AMIやインスタンスタイプ、ネットワーキング設定が含まれてるので注意。

実際にやってみる(Python3)

import boto3


ec2 = boto3.client('ec2')

# 起動テンプレートの設定を指定
template_data = {
    'ImageId': 'ami-xxxxxxxxxxxxxxxxx',  # AMIのID
    'InstanceType': 't2.micro',           # インスタンスタイプ
    'Keyname': 'your-key-pair',           # キーペアの名前
    'SecurityGroupIds': ['sg-xxxxxxxx'],  # セキュリティグループのID
    'SubnetId': 'subnet-xxxxxxxx',        # サブネットのID
}

# 起動テンプレートを作成
response = ec2.create_launch_template(
    LaunchTemplateName='QiiraTemplate',
    VersionDescription='Initial version',
    LaunchTemplateData=template_data
)

# 作ったテンプレートのIDを取得
template_id = response['LaunchTemplate']['LaunchTemplateId']
print(f'起動テンプレートができたよ! テンプレートID: {template_id}')

# 実際にEC2インスタンスを起動
print('EC2インスタンスを起動するには、以下のコマンドを実行:')
print(f'aws ec2 run-instances --launch-template LaunchTemplateName=QiiraTemplate,Version=Initial version')

Pythonスクリプトとしてローカル環境またはAWS CLIの用意が必要

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0