LoginSignup
8
9

More than 5 years have passed since last update.

EC2のスナップショット取得を自動化する

Posted at

1. コマンドラインからAWSをにアクセスできるようにする

IAMユーザーの作成

※ この工程はEC2にアクセスできるIAMユーザーをすでに持っていれば不要。

  • Consoleのメニューから[Services > IAM]をクリック
  • 左メニューの[Users]をクリック
  • [Create New Users]をクリックして、名前を入力して[Create]をクリック
  • アクセスキーとアクセスシークレットをメモる
  • IAMメニューのDashboardからUsersに戻り、Attach Policyから[AmazonEC2FullAccess]を選択。

aws-cliの設定

IEC2サーバーにログインし、AWSにアクセスするための設定を行う。

$ aws configure
AWS Access Key ID [None]:***************
AWS Secret Access Key [None]: ***************
Default region name [None]: ap-northeast-1
Default output format [None]:

以下のコマンドで結果が取得できれば設定完了。

$ aws ec2 describe-instances

2. シェルスクリプトを作ってCronに設定する

シェルスクリプトを作成

以下がスナップショットを作成するためのコマンドをシェルスクリプトに書く(vol-xxxxxxxxの部分はVolumeId)

/home/ec2-user/scripts/snapshot.sh
#!/bin/sh
aws ec2 create-snapshot -–volume-id vol-xxxxxxxx

Cronの設定

$ crontab -e

末尾に以下を追加。

0 3 * * * /home/ec2-user/scripts/snapshot.sh

これで、毎日深夜3時にスナップショットを取得できる。

以上。

8
9
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
8
9