ユーザーデータ名"handson-ec2-1st-httpd-userdata"のファイルを作成します。
手順に必要な設定値を変数に格納をします。
ユーザーデータ用ディレクトリを指定します。
変数の設定: DIR_USER_DATA="${HOME}/environment/conf-handson-ec2-1st"
変数の設定:
DIR_USER_DATA="${HOME}/environment/conf-handson-ec2-1st"
ディレクトリが存在することを確認し、存在しない場合は作成します。
コマンド: ls -d ${DIR_USER_DATA} > /dev/null 2>&1 \ || mkdir -p ${DIR_USER_DATA} 結果(例): (出力なし)
コマンド:
ls -d ${DIR_USER_DATA} > /dev/null 2>&1 \ || mkdir -p ${DIR_USER_DATA}
結果(例):
(出力なし)
ユーザーデータファイル名を指定します。
変数の設定: USER_DATA_NAME='handson-ec2-1st-httpd-userdata' 変数の設定: FILE_USER_DATA="${DIR_USER_DATA}/${USER_DATA_NAME}.bash" \ && echo ${FILE_USER_DATA} 結果(例): ${HOME}/environment/conf-handson-ec2-1st/handson-ec2-1st-httpd-userdata.bash
USER_DATA_NAME='handson-ec2-1st-httpd-userdata'
FILE_USER_DATA="${DIR_USER_DATA}/${USER_DATA_NAME}.bash" \ && echo ${FILE_USER_DATA}
${HOME}/environment/conf-handson-ec2-1st/handson-ec2-1st-httpd-userdata.bash
各変数に正しい設定値が格納されていることを確認します。
変数の確認: cat << END # 1. DIR_USER_DATA:"${HOME}/environment/conf-handson-ec2-1st" DIR_USER_DATA="${DIR_USER_DATA}" # 2. FILE_USER_DATA:"${HOME}/environment/conf-handson-ec2-1st/handson-ec2-1st-httpd-userdata.bash" FILE_USER_DATA="${FILE_USER_DATA}" END
変数の確認:
cat << END # 1. DIR_USER_DATA:"${HOME}/environment/conf-handson-ec2-1st" DIR_USER_DATA="${DIR_USER_DATA}" # 2. FILE_USER_DATA:"${HOME}/environment/conf-handson-ec2-1st/handson-ec2-1st-httpd-userdata.bash" FILE_USER_DATA="${FILE_USER_DATA}" END
下段の変数が入っていない、もしくは上段と同等の値が入っていない場合は、それぞれの手順番号に戻って変数の設定を行います。
ユーザーデータファイルを作成します。
変数の確認: cat << END # FILE_USER_DATA:"${HOME}/environment/conf-handson-ec2-1st/handson-ec2-1st-httpd-userdata.bash" FILE_USER_DATA="${FILE_USER_DATA}" END コマンド: cat << EOF > ${FILE_USER_DATA} #!/bin/bash GIT_REPOSITORY_ORIGIN='https://github.com/opelab/handson-cli-web-contents.git' GIT_REPOSITORY_NAME='handson-cli-web-contents' yum -y update # setup httpd yum install -y httpd systemctl start httpd.service systemctl enable httpd.service yum -y install git # get contents cd /tmp/ \\ && git clone \${GIT_REPOSITORY_ORIGIN} # setup contents cp /tmp/\${GIT_REPOSITORY_NAME}/* /var/www/html/ EOF cat ${FILE_USER_DATA} 結果(例): #!/bin/bash GIT_REPOSITORY_ORIGIN='https://github.com/opelab/handson-cli-web-contents.git' GIT_REPOSITORY_NAME='handson-cli-web-contents' yum -y update # setup httpd yum install -y httpd systemctl start httpd.service systemctl enable httpd.service yum -y install git # get contents cd /home/ec2-user/ \ && git clone ${GIT_REPOSITORY_ORIGIN} # setup contents cp /home/ec2-user/${GIT_REPOSITORY_NAME}/* /var/www/html/
cat << END # FILE_USER_DATA:"${HOME}/environment/conf-handson-ec2-1st/handson-ec2-1st-httpd-userdata.bash" FILE_USER_DATA="${FILE_USER_DATA}" END
cat << EOF > ${FILE_USER_DATA} #!/bin/bash GIT_REPOSITORY_ORIGIN='https://github.com/opelab/handson-cli-web-contents.git' GIT_REPOSITORY_NAME='handson-cli-web-contents' yum -y update # setup httpd yum install -y httpd systemctl start httpd.service systemctl enable httpd.service yum -y install git # get contents cd /tmp/ \\ && git clone \${GIT_REPOSITORY_ORIGIN} # setup contents cp /tmp/\${GIT_REPOSITORY_NAME}/* /var/www/html/ EOF cat ${FILE_USER_DATA}
#!/bin/bash GIT_REPOSITORY_ORIGIN='https://github.com/opelab/handson-cli-web-contents.git' GIT_REPOSITORY_NAME='handson-cli-web-contents' yum -y update # setup httpd yum install -y httpd systemctl start httpd.service systemctl enable httpd.service yum -y install git # get contents cd /home/ec2-user/ \ && git clone ${GIT_REPOSITORY_ORIGIN} # setup contents cp /home/ec2-user/${GIT_REPOSITORY_NAME}/* /var/www/html/
「ユーザーデータファイル名"${HOME}/environment/conf-handson-ec2-1st/handson-ec2-1st-httpd-userdata.bash"が存在する。」ことを確認します。
コマンド: ls ${FILE_USER_DATA} 結果(例): ${HOME}/environment/conf-handson-ec2-1st/handson-ec2-1st-httpd-userdata.bash
ls ${FILE_USER_DATA}