LoginSignup
0
0

More than 3 years have passed since last update.

AWSのEC2にユーザーデータで再起動時シェル実行スクリプト作成

Last updated at Posted at 2020-11-09

初めに

Amazon EC2でインスタンス起動する時、起動後にそのインスタンスにユーザーデータを渡し、一般的な自動設定タスクを実行したり、スクリプトを実行したりできます。

下のスクリプト例では、スクリプトがウェブサーバーを作成し、設定します。

サンプルコード
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="<任意ファイル名>.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="<任意ファイル名>.txt"

#!/bin/bash
yum update -y
amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
yum install -y httpd mariadb-server
systemctl start httpd
systemctl enable httpd
usermod -a -G apache ec2-user
chown -R ec2-user:apache /var/www
chmod 2775 /var/www
find /var/www -type d -exec chmod 2775 {} \;
find /var/www -type f -exec chmod 0664 {} \;
echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

参考URL: AWS公式サイト

EC2 インスタンスを再度起動し、スクリプトが正しく実行されていることを検証します。

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