インフラエンジニア徒然日記

なんちゃってインフラエンジニアです。てきとーに日々のことを記載します。

EC2インスタンス初期構築時のセットアップをまとめてみた

EC2を最初に作成した時に、
cloud-initにより、ec2-userなどの設定がデフォルトで動作し、AWSのデフォルトAMIではアクセスキーの作成やそれを利用した秘密鍵による認証によりサーバログインとなるため使い勝手が悪い。

よって、AWSのデフォルトAMIからインスタンスを作成し、
cloud-initの設定が反映されないように変更を行い、テンプレートとなるAMIを
作成し新規のEC2インスタンスはそのテンプレートAMIを利用していきたいと思う。

また、EC2インスタンスでは、AWSCLIコマンドを実行できるように設定していきたいと思う。


○事前準備
EC2インスタンスにアタッチするIAMロールを作成する。
とりあえずすべての操作を行いたいので、ポリシーはAWSデフォルトの
「AdministratorAccess」をアタッチします。

ロール名:aws_ec2_admin_role
ポリシー名:AdministratorAccess


続いて、インスタンスの作成から、クイックスタートの中の
Red Hat Enterprise Linux 7.3 (HVM), SSD Volume Type」を選択し
作成。

作成が終わったら、ec2-userでログインを行います。

○メイン作業
1.まずcloud-initの編集を行います。

以下をコメントアウトします。
#vi /etc/cloud/cloud.cfg

コメントアウト箇所
===========================
# - set_hostname
# - update_hostname
# - ssh

# - locale
# - set-passwords
# - timezone
system_info:
# default_user:
# name: ec2-user
# lock_passwd: true
# gecos: Cloud User
# groups: [wheel, adm, systemd-journal]
# sudo: ["ALL=(ALL) NOPASSWD:ALL"]
# shell: /bin/bash
# vim:syntax=yaml

===========================

2.sshdを変更し、rootでのパスワードログイン不可とともに
それ以外のユーザーによるパスワードログインを許可します。

以下を項目を追加します
===========================
PermitRootLogin no
PasswordAuthentication yes
===========================

3.続いて、testユーザーを作成し、sudo権限を付与。
testユーザーでsshログインを行い、ec2-userを削除します。

#useradd test
#visudo
以下を追加
===========================
test ALL=(ALL) ALL
===========================

testユーザでログインしrootにスイッチ後
以下コマンド実行
#userdel ec2-user


4.つづいてサーバのホスト名を変更、locale変更、timezone変更を行います。

▲ホスト名を変更
#vi /etc/hostname
template

▲locale変更
#localectl set-locale LANG=ja_JP.utf8
#localectl set-keymap jp106
#localectl status
以下の通り変更されます
System Locale: LANG=ja_JP.utf8
VC Keymap: jp106
X11 Layout: jp
X11 Model: jp106
X11 Options: terminate:ctrl_alt_bksp

▲timezone変更
#timedatectl set-timezone Asia/Tokyo
# timedatectl status
以下の通り変更されます。
Local time: Sat 2017-02-11 22:08:08 JST
Universal time: Sat 2017-02-11 13:08:08 UTC
RTC time: Sat 2017-02-11 13:08:07
Time zone: Asia/Tokyo (JST, +0900)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a


5.awscliコマンド,jqコマンドを導入します。

▲まず、初期構築時は、wgetとunzipが導入されていないため
インストールします。
#yum -y install wget unzip


▲つづいてawscliを導入します。(/usr/local/aws配下にインストールします)
#wget https://s3.amazonaws.com/aws-cli/awscli-bundle.zip
#unzip awscli-bundle.zip
#mkdir /usr/local/aws

#cd awscli-bundle
#./install -i /usr/local/aws -b /usr/local/bin/aws
#aws --version

以下の通り、awscliのバージョンが表示されること
aws-cli/1.11.47 Python/2.7.5 Linux/3.10.0-514.el7.x86_64 botocore/1.5.10

▲つづいてawsコマンドのコンフィグ設定を行います。
#aws configure

以下の通り入力
=========================== 
AWS Access Key ID [None]:  ←なにもしないでエンター
AWS Secret Access Key [None]: ←なにもしないでエンター
Default region name [ap-northeast-1]:ap-northeast-1
Default output format [json]:json
=========================== 

#cat /root/.aws/config
以下の通り表示されること
=========================== 
[default]
output = json
region = ap-northeast-1
=========================== 

▲さらにjqを導入します。

#wget https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64
#mv jq-linux64 /bin/jq
#jq

以下の通り表示されること
=========================== 
jq - commandline JSON processor [version 1.5]
Usage: jq [options] <jq filter> [file...]

jq is a tool for processing JSON inputs, applying the
given filter to its JSON text inputs and producing the
filter's results as JSON on standard output.
The simplest filter is ., which is the identity filter,
copying jq's input to its output unmodified (except for
formatting).
For more advanced filters see the jq(1) manpage ("man jq")
and/or https://stedolan.github.io/jq

Some of the options include:
-c compact instead of pretty-printed output;
-n use `null` as the single input value;
-e set the exit status code based on the output;
-s read (slurp) all inputs into an array; apply filter to it;
-r output raw strings, not JSON texts;
-R read raw strings, not JSON texts;
-C colorize JSON;
-M monochrome (don't colorize JSON);
-S sort keys of objects on output;
--tab use tabs for indentation;
--arg a v set variable $a to value <v>;
--argjson a v set variable $a to JSON value <v>;
--slurpfile a f set variable $a to an array of JSON texts read from <f>;
See the manpage for more options.


以上で、初期構築は完了で、これをテンプレートAMIとして利用していきたいと思います。