0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【備忘録】AWSCLIでユーザデータを指定する

Last updated at Posted at 2023-07-05

AWS CLIでユーザデータを指定する

  • run-instancesコマンドと--user-dateパラメータを使用する
aws ec2 run-instances \
--image-id ami-xxxxxxxx \
--count 1 \
--instance-type m3.medium \
--key-name xxxxxxxx \
--subnet-id xxxxxxxx \
--security-group-ids sg-xxxxxxxx \
--user-data file://xxxxxxxx.bash
  • ユーザデータとして入力されたものはrootユーザとして実行されるのでsudoコマンドは使わないこと
  • -y指定のないyum updateは指定できない(ユーザフィードバック:確認が必要となるコマンドは使えない)
  • AWS CLIを使用するには、インスタンス起動時にインスタンスプロファイルを使用する
    (ユーザデータのスクリプトが必要とする認証情報が提供される)
  • var/lib/cloud/instances/instance-id/にコピーされて実行される
    実行後にスクリプトは削除できないので、インスタンスからAMIを作る際は削除すること。

(補足)インスタンスメタデータ

  • 実行中のインスタンス設定・インスタンス管理のために使われる
  • ホスト名・イベント・セキュリティグループなどでカテゴリ分けされる
  • 起動時に指定したユーザデータにアクセスできる

(補足)実行中のインスタンス内からユーザデータを取得する

IMDSのIPv4アドレス169.254.169.254を使用する

http://169.254.169.254/latest/user-data

[ec2-user ~]$ TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` \
&& curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/user-data
#!/bin/bash
yum update -y
service httpd start
chkconfig httpd on

このあたりは、別の機会に調べてみることにします。

参考

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?