[JAWS-UG CLI] OpsWorks #2 レイヤの作成


前提条件
========

### OpsWorksへの権限 

OpsWorksに対してフル権限があること。


### AWS CLIのバージョン

以下のバージョンで動作確認済

* AWS CLI 1.11.14

```bash:コマンド:
aws --version
```

結果(例):

      aws-cli/1.11.102 Python/2.7.12 Linux/4.4.11-23.53.amzn1.x86_64 botocore/1.5.65

バージョンが古い場合は最新版に更新しましょう。

```bash:コマンド:
sudo -H pip install -U awscli
```


0. 準備
=======

まず変数の確認をします。

```bash:変数の確認:
cat << ETX

        AWS_DEFAULT_PROFILE:             (0.1) ${AWS_DEFAULT_PROFILE}
        OPSW_STACK_NAME:                 (0.2) ${OPSW_STACK_NAME}
        OPSW_CUSTOM_SG_GROUP_IDS         (0.3) ${OPSW_CUSTOM_SG_GROUP_IDS}
        OPSW_LAYER_NAME                  (0.4) ${OPSW_LAYER_NAME}
        OPSW_LAYER_SHORTNAME             (0.4) ${OPSW_LAYER_SHORTNAME}
        OPSW_LAYER_TYPE                  (0.5) ${OPSW_LAYER_TYPE}
        OPSW_LAYER_CUSTOM_RECIPES_DEPLOY (0.6) ${OPSW_LAYER_CUSTOM_RECIPES_DEPLOY}
        OPSW_LAYER_LIFECYCLE_TIMEOUT     (0.7) ${OPSW_LAYER_LIFECYCLE_TIMEOUT}
        OPSW_LAYER_LIFECYCLE_ELB_DRAINED (0.7) ${OPSW_LAYER_LIFECYCLE_ELB_DRAINED}

ETX
```

結果(例):

      AWS_DEFAULT_PROFILE:             (0.1) opsworksFull-prjZ-mbp13
      OPSW_STACK_NAME:                 (0.2) My Sample Stack (Linux)
      OPSW_CUSTOM_SG_GROUP_IDS         (0.3) <AWS-OpsWorks-WebAppセキュリティグループのID>
      OPSW_LAYER_NAME                  (0.4) Node.js App Server
      OPSW_LAYER_SHORTNAME             (0.4) nodejs-server
      OPSW_LAYER_TYPE                  (0.5) custom
      OPSW_LAYER_CUSTOM_RECIPES_DEPLOY (0.6) nodejs_demo
      OPSW_LAYER_LIFECYCLE_TIMEOUT     (0.7) 120
      OPSW_LAYER_LIFECYCLE_ELB_DRAINED (0.7) false

変数が入っていない、適切でない場合は、それぞれの手順番号について作業を
行います。


0.1. プロファイルの指定
-----------------------

プロファイルの一覧を確認します。

```bash:コマンド:
cat ~/.aws/credentials \
       | grep '\[' \
       | sed 's/\[//g' | sed 's/\]//g'
```

結果(例):

      iamFull-prjz-mbpr13
      opsworksFull-prjZ-mbp13

```bash:変数の設定:
export AWS_DEFAULT_PROFILE='opsworksFull-prjZ-mbp13'
```


0.2. スタック名の指定
---------------------

```bash:変数の設定:
OPSW_STACK_NAME='My Sample Stack (Linux)'
```

```bash:コマンド:
OPSW_STACK_ID=$( \
        aws opsworks describe-stacks \
          --query "Stacks[?Name ==\`${OPSW_STACK_NAME}\`].StackId" \
          --output text \
) \
        && echo ${OPSW_STACK_ID}
```

結果(例):

      xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx


```bash:変数の設定:
ARRAY_OPSW_STACK_IDS="${OPSW_STACK_ID}" \
        && echo ${ARRAY_OPSW_STACK_IDS}
```

0.3. セキュリティグループIDの取得
---------------------------------

```bash:変数の設定:
VPC_ID=$( \
        aws opsworks describe-stacks \
          --stack-ids $ARRAY_OPSW_STACK_IDS \
          --query 'Stacks[].VpcId' \
          --output text \
) \
        && echo ${VPC_ID}
```

```bash:変数の設定:
EC2_SG_NAME='AWS-OpsWorks-WebApp'
```

```bash:変数の設定:
EC2_SG_ID=$( \
        aws ec2 describe-security-groups \
          --query "SecurityGroups[?VpcId == \`${VPC_ID}\` && GroupName ==\`${EC2_SG_NAME}\`].GroupId" \
          --output text \
) \
        && echo ${EC2_SG_ID}
```

```bash:変数の設定:
OPSW_CUSTOM_SG_GROUP_IDS="${EC2_SG_ID}" \
        && echo ${OPSW_CUSTOM_SG_GROUP_IDS}
```


0.4. レイヤ名の指定
-------------------

```bash:変数の設定:
OPSW_LAYER_NAME='Node.js App Server'
```

```bash:変数の設定:
OPSW_LAYER_SHORTNAME='nodejs-server'
```


0.5. レイヤタイプの指定
-----------------------

```bash:変数の設定:
OPSW_LAYER_TYPE='custom'
```


0.6. カスタムレシピの指定
-------------------------

```bash:変数の設定:
OPSW_LAYER_CUSTOM_RECIPES_DEPLOY='nodejs_demo'
```


0.7. ライフサイクルイベントの指定
---------------------------------

```bash:変数の設定:
OPSW_LAYER_LIFECYCLE_TIMEOUT='120'
```

```bash:変数の設定:
OPSW_LAYER_LIFECYCLE_ELB_DRAINED='false'
```


再確認
------

設定されている変数の内容を再確認します。

```bash:変数の確認:
cat << ETX

        AWS_DEFAULT_PROFILE:             (0.1) ${AWS_DEFAULT_PROFILE}
        OPSW_STACK_NAME:                 (0.2) ${OPSW_STACK_NAME}
        OPSW_CUSTOM_SG_GROUP_IDS         (0.3) ${OPSW_CUSTOM_SG_GROUP_IDS}
        OPSW_LAYER_NAME                  (0.4) ${OPSW_LAYER_NAME}
        OPSW_LAYER_SHORTNAME             (0.4) ${OPSW_LAYER_SHORTNAME}
        OPSW_LAYER_TYPE                  (0.5) ${OPSW_LAYER_TYPE}
        OPSW_LAYER_CUSTOM_RECIPES_DEPLOY (0.6) ${OPSW_LAYER_CUSTOM_RECIPES_DEPLOY}
        OPSW_LAYER_LIFECYCLE_TIMEOUT     (0.7) ${OPSW_LAYER_LIFECYCLE_TIMEOUT}
        OPSW_LAYER_LIFECYCLE_ELB_DRAINED (0.7) ${OPSW_LAYER_LIFECYCLE_ELB_DRAINED}

ETX
```

結果(例):

      AWS_DEFAULT_PROFILE:             (0.1) opsworksFull-prjZ-mbp13
      OPSW_STACK_NAME:                 (0.2) My Sample Stack (Linux)
      OPSW_CUSTOM_SG_GROUP_IDS         (0.3) <AWS-OpsWorks-WebAppセキュリティグループのID>
      OPSW_LAYER_NAME                  (0.4) Node.js App Server
      OPSW_LAYER_SHORTNAME             (0.4) nodejs-server
      OPSW_LAYER_TYPE                  (0.5) custom
      OPSW_LAYER_CUSTOM_RECIPES_DEPLOY (0.6) nodejs_demo
      OPSW_LAYER_LIFECYCLE_TIMEOUT     (0.7) 120
      OPSW_LAYER_LIFECYCLE_ELB_DRAINED (0.7) false


1. 事前作業
===========



2. 本作業
=========


作成
----

```bash:変数の設定:
OPSW_LAYER_CUSTOM_RECIPES_STRING="Deploy=${OPSW_LAYER_CUSTOM_RECIPES_DEPLOY}" \
        && echo ${OPSW_LAYER_CUSTOM_RECIPES_STRING}
```

```bash:変数の設定:
OPSW_LAYER_LIFECYCLE_STRING="Shutdown={ExecutionTimeout=120,DelayUntilElbConnectionsDrained=false}" \
        && echo ${OPSW_LAYER_LIFECYCLE_STRING}
```

```bash:変数の確認:
cat << ETX

        AWS_DEFAULT_REGION:               ${AWS_DEFAULT_REGION}
        OPSW_STACK_DI:                    ${OPSW_STACK_ID}
        OPSW_LAYER_NAME:                  ${OPSW_LAYER_NAME}
        OPSW_LAYER_TYPE:                  ${OPSW_LAYER_TYPE}
        OPSW_LAYER_SHORTNAME:             ${OPSW_LAYER_SHORTNAME}
        OPSW_CUSTOM_SG_GROUP_IDS:         ${OPSW_CUSTOM_SG_GROUP_IDS}
        OPSW_LAYER_CUSTOM_RECIPES_STRING: ${OPSW_LAYER_CUSTOM_RECIPES_STRING}
        OPSW_LAYER_LIFECYCLE_STRING:      ${OPSW_LAYER_LIFECYCLE_STRING}

ETX
```

```bash:コマンド:
aws opsworks create-layer \
        --stack-id ${OPSW_STACK_ID} \
        --name "${OPSW_LAYER_NAME}" \
        --type ${OPSW_LAYER_TYPE} \
        --shortname ${OPSW_LAYER_SHORTNAME} \
        --custom-security-group-ids ${OPSW_CUSTOM_SG_GROUP_IDS} \
        --custom-recipes ${OPSW_LAYER_CUSTOM_RECIPES_STRING} \
        --no-auto-assign-elastic-ips \
        --enable-auto-healing \
        --auto-assign-public-ips \
        --no-use-ebs-optimized-instances \
        --lifecycle-event-configuration ${OPSW_LAYER_LIFECYCLE_STRING}
```

結果(例):

      {
        "LayerId": "eb94325c-8b81-4ee8-87e0-8e9df3a4e4ee"
      }


2.2. レイヤIDの取得
-------------------

```bash:変数の設定:
OPSW_LAYER_ID=$( \
        aws opsworks describe-layers \
          --stack-id $OPSW_STACK_ID \
          --query "Layers[?Name == \`${OPSW_LAYER_NAME}\`].LayerId" \
          --output text \
) \
        && echo ${OPSW_LAYER_ID}
```


3. 事後作業
===========

```bash:変数の設定:
ARRAY_OPSW_LAYER_IDS="${OPSW_LAYER_ID}" \
        && echo ${ARRAY_OPSW_LAYER_IDS}
```

```bash:コマンド:
aws opsworks describe-layers \
        --layer-ids ${ARRAY_OPSW_LAYER_IDS}
```


完了
====