[JAWS-UG CLI] OpsWorks #1 スタックの作成


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

### 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}
        AWS_DEFAULT_REGION:               (0.2) ${AWS_DEFAULT_REGION}
        IAM_ROLE_ARN:                     (0.3) ${IAM_ROLE_ARN}
        IAM_INSTANCE_PROFILE_ARN:         (0.4) ${IAM_INSTANCE_PROFILE_ARN}
        VPC_ID:                           (0.5) ${VPC_ID}
        VPC_SUBNET_ID:                    (0.6) ${VPC_SUBNET_ID}
        OPSW_STACK_NAME:                  (0.8) ${OPSW_STACK_NAME}
        OPSW_ATTRIBUTE:                   (0.9) ${OPSW_ATTRIBUTE}
        OPSW_CONF_MANAGER_NAME            (0.10) ${OPSW_CONF_MANAGER_NAME}
        OPSW_CONF_MANAGER_VERSION         (0.10) ${OPSW_CONF_MANAGER_VERSION}
        OPSW_CUSTOM_COOKBOOK_SOURCE_TYPE: (0.11) ${OPSW_CUSTOM_COOKBOOK_SOURCE_TYPE}
        OPSW_CUSTOM_COOKBOOK_SOURCE_URL:  (0.11) ${OPSW_CUSTOM_COOKBOOK_SOURCE_URL}
        OPSW_DEFAULT_ROOT_DEVICE_TYPE:    (0.12) ${OPSW_DEFAULT_ROOT_DEVICE_TYPE}
        OPSW_DEFAULT_OS:                  (0.13) ${OPSW_DEFAULT_OS}

ETX
```

結果(例):

      AWS_DEFAULT_PROFILE:              (0.1) opsworksFull-prjZ-mbp13
      AWS_DEFAULT_REGION:               (0.2) ap-northeast-1
      IAM_ROLE_ARN:                     (0.3) arn:aws:iam::XXXXXXXXXXXX:role/aws-opsworks-service-role
      IAM_INSTANCE_PROFILE_ARN:         (0.4) arn:aws:iam::XXXXXXXXXXXX:instance-profile/aws-opsworks-ec2-role
      VPC_ID:                           (0.5) <スタックを起動するVPC>
      VPC_SUBNET_ID:                    (0.6) <スタックを起動するサブネット>
      OPSW_STACK_NAME:                  (0.8) My Sample Stack (Linux)
      OPSW_ATTRIBUTE:                   (0.9) { "Color": "rgb(45, 114, 184)" }
      OPSW_CONF_MANAGER_NAME            (0.10) Chef
      OPSW_CONF_MANAGER_VERSION         (0.10) 12
      OPSW_CUSTOM_COOKBOOK_SOURCE_TYPE: (0.11) archive
      OPSW_CUSTOM_COOKBOOK_SOURCE_URL:  (0.11) https://s3.amazonaws.com/opsworks-demo-assets/opsworks-linux-demo-cookbooks-nodejs.tar.gz
      OPSW_DEFAULT_ROOT_DEVICE_TYPE:    (0.12) ebs
      OPSW_DEFAULT_OS:                  (0.13) Amazon Linux 2017.03

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


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:変数の設定:
export AWS_DEFAULT_REGION='ap-northeast-1'
```


0.3. IAMロールの指定
--------------------

```bash:変数の設定:
IAM_ROLE_NAME='aws-opsworks-service-role'
```

```bash:コマンド:
IAM_ROLE_ARN=$( \
        aws iam get-role \
          --role-name ${IAM_ROLE_NAME} \
          --query 'Role.Arn' \
          --output text \
) \
        && echo ${IAM_ROLE_ARN}
```

結果(例):

      arn:aws:iam::XXXXXXXXXXXX:role/aws-opsworks-service-role


0.4. インスタンスプロファイルの指定
-----------------------------------

```bash:変数の設定:
IAM_INSTANCE_PROFILE_NAME='aws-opsworks-ec2-role'
```

```bash:コマンド:
IAM_INSTANCE_PROFILE_ARN=$( \
        aws iam get-instance-profile \
          --instance-profile-name ${IAM_INSTANCE_PROFILE_NAME} \
          --query 'InstanceProfile.Arn' \
          --output text \
) \
        && echo ${IAM_INSTANCE_PROFILE_ARN}
```

結果(例):

      arn:aws:iam::XXXXXXXXXXXX:instance-profile/aws-opsworks-ec2-role


0.5. VPC IDの指定
-----------------

```bash:変数の設定:
VPC_ID=
```

```bash:コマンド:
VPC_ID=$( \
        aws ec2 describe-vpcs \
          --filters Name=cidr,Values=${VPC_CIDR} \
          --query 'Vpcs[].VpcId' \
          --output text \
) \
        && echo ${VPC_ID}
```

結果(例):

      vpc-xxxxxxxx


0.6. デフォルトサブネットIDの指定
---------------------------------

```bash:変数の設定:
VPC_SUBNET_ID=
```

```bash:コマンド:
VPC_SUBNET_ID=$( \
        aws ec2 describe-subnets \
          --filters Name=cidrBlock,Values=${VPC_SUBNET_CIDR} \
          --query 'Subnets[].SubnetId' \
          --output text \
) \
        && echo ${VPC_SUBNET_ID}
```

結果(例):

      subnet-xxxxxxxx




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

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


0.9. スタックのアトリビュートの指定
-----------------------------------

```bash:変数の設定:
OPSW_ATTRIBUTE='{ "Color": "rgb(45, 114, 184)" }'
```


0.10. 設定マネージャの指定
--------------------------

```bash:変数の設定:
OPSW_CONF_MANAGER_NAME='Chef'
```

```bash:変数の設定:
OPSW_CONF_MANAGER_VERSION='12'
```


0.11. カスタムクックブックの指定
--------------------------------

```bash:変数の設定:
OPSW_CUSTOM_COOKBOOK_SOURCE_TYPE='archive'
```

```bash:変数の設定:
OPSW_CUSTOM_COOKBOOK_SOURCE_URL='https://s3.amazonaws.com/opsworks-demo-assets/opsworks-linux-demo-cookbooks-nodejs.tar.gz'
```


0.12. インスタンスのルートデバイスの指定
----------------------------------------

```bash:変数の設定:
OPSW_DEFAULT_ROOT_DEVICE_TYPE='ebs'
```


0.13. デフォルトOSの指定
------------------------

```bash:変数の設定:
OPSW_DEFAULT_OS='Amazon Linux 2017.03'
```


再確認
------

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

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

        AWS_DEFAULT_PROFILE:              (0.1) ${AWS_DEFAULT_PROFILE}
        AWS_DEFAULT_REGION:               (0.2) ${AWS_DEFAULT_REGION}
        IAM_ROLE_ARN:                     (0.3) ${IAM_ROLE_ARN}
        IAM_INSTANCE_PROFILE_ARN:         (0.4) ${IAM_INSTANCE_PROFILE_ARN}
        VPC_ID:                           (0.5) ${VPC_ID}
        VPC_SUBNET_ID:                    (0.6) ${VPC_SUBNET_ID}
        OPSW_STACK_NAME:                  (0.8) ${OPSW_STACK_NAME}
        OPSW_ATTRIBUTE:                   (0.9) ${OPSW_ATTRIBUTE}
        OPSW_CONF_MANAGER_NAME            (0.10) ${OPSW_CONF_MANAGER_NAME}
        OPSW_CONF_MANAGER_VERSION         (0.10) ${OPSW_CONF_MANAGER_VERSION}
        OPSW_CUSTOM_COOKBOOK_SOURCE_TYPE: (0.11) ${OPSW_CUSTOM_COOKBOOK_SOURCE_TYPE}
        OPSW_CUSTOM_COOKBOOK_SOURCE_URL:  (0.11) ${OPSW_CUSTOM_COOKBOOK_SOURCE_URL}
        OPSW_DEFAULT_ROOT_DEVICE_TYPE:    (0.12) ${OPSW_DEFAULT_ROOT_DEVICE_TYPE}
        OPSW_DEFAULT_OS:                  (0.13) ${OPSW_DEFAULT_OS}

ETX
```

結果(例):

      AWS_DEFAULT_PROFILE:              (0.1) opsworksFull-prjZ-mbp13
      AWS_DEFAULT_REGION:               (0.2) ap-northeast-1
      IAM_ROLE_ARN:                     (0.3) arn:aws:iam::XXXXXXXXXXXX:role/aws-opsworks-service-role
      IAM_INSTANCE_PROFILE_ARN:         (0.4) arn:aws:iam::XXXXXXXXXXXX:instance-profile/aws-opsworks-ec2-role
      VPC_ID:                           (0.5) <スタックを起動するVPC>
      VPC_SUBNET_ID:                    (0.6) <スタックを起動するサブネット>
      OPSW_STACK_NAME:                  (0.8) My Sample Stack (Linux)
      OPSW_ATTRIBUTE:                   (0.9) { "Color": "rgb(45, 114, 184)" }
      OPSW_CONF_MANAGER_NAME            (0.10) Chef
      OPSW_CONF_MANAGER_VERSION         (0.10) 12
      OPSW_CUSTOM_COOKBOOK_SOURCE_TYPE: (0.11) archive
      OPSW_CUSTOM_COOKBOOK_SOURCE_URL:  (0.11) https://s3.amazonaws.com/opsworks-demo-assets/opsworks-linux-demo-cookbooks-nodejs.tar.gz
      OPSW_DEFAULT_ROOT_DEVICE_TYPE:    (0.12) ebs
      OPSW_DEFAULT_OS:                  (0.13) Amazon Linux 2017.03


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



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


作成
----

```bash:変数の設定:
OPSW_CONF_MANAGER_STRING="Name=${OPSW_CONF_MANAGER_NAME},Version=${OPSW_CONF_MANAGER_VERSION}" \
        && echo ${OPSW_CONF_MANAGER_STRING}
```

```bash:変数の設定:
OPSW_CUSTOM_COOKBOOK_SOURECE_STRING="Type=${OPSW_CUSTOM_COOKBOOK_SOURCE_TYPE},Url=${OPSW_CUSTOM_COOKBOOK_SOURCE_URL}" \
        && echo ${OPSW_CUSTOM_COOKBOOK_SOURECE_STRING}
```

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

        AWS_DEFAULT_REGION:                  ${AWS_DEFAULT_REGION}
        IAM_ROLE_ARN:                        ${IAM_ROLE_ARN}
        IAM_INSTANCE_PROFILE_ARN:            ${IAM_INSTANCE_PROFILE_ARN}
        VPC_ID:                              ${VPC_ID}
        VPC_SUBNET_ID:                       ${VPC_SUBNET_ID}
        OPSW_STACK_NAME:                     ${OPSW_STACK_NAME}
        OPSW_ATTRIBUTE:                      ${OPSW_ATTRIBUTE}
        OPSW_CONF_MANAGER_STRING             ${OPSW_CONF_MANAGER_STRING}
        OPSW_CUSTOM_COOKBOOK_SOURECE_STRING: ${OPSW_CUSTOM_COOKBOOK_SOURECE_STRING}
        OPSW_DEFAULT_ROOT_DEVICE_TYPE:       ${OPSW_DEFAULT_ROOT_DEVICE_TYPE}
        OPSW_DEFAULT_OS:                     ${OPSW_DEFAULT_OS}

ETX
```

```bash:コマンド:
aws opsworks create-stack \
  --name "${OPSW_STACK_NAME}" \
  --attributes "${OPSW_ATTRIBUTE}" \
  --service-role-arn ${IAM_ROLE_ARN} \
  --stack-region ${AWS_DEFAULT_REGION} \
  --default-instance-profile-arn ${IAM_INSTANCE_PROFILE_ARN} \
  --default-os "${OPSW_DEFAULT_OS}" \
  --vpc-id ${VPC_ID} \
  --default-subnet-id ${VPC_SUBNET_ID} \
  --use-custom-cookbooks \
  --use-opsworks-security-groups \
  --configuration-manager ${OPSW_CONF_MANAGER_STRING} \
  --custom-cookbooks-source ${OPSW_CUSTOM_COOKBOOK_SOURECE_STRING} \
  --default-root-device-type ${OPSW_DEFAULT_ROOT_DEVICE_TYPE}
```

結果(例):

      {
        "StackId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
      }


2.2. スタックIDの取得
---------------------

```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


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


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

```bash:コマンド:
aws opsworks describe-stacks \
        --stack-ids ${ARRAY_OPSW_STACK_IDS}
```

結果(例):

      {
        "Stacks": [
          {
              "StackId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
              "ServiceRoleArn": "arn:aws:iam::XXXXXXXXXXXX:role/aws-opsworks-service-role",
              "VpcId": "vpc-xxxxxxxx",
              "DefaultRootDeviceType": "ebs",
              "Name": "My Sample Stack (Linux)",
              "HostnameTheme": "Layer_Dependent",
              "UseCustomCookbooks": true,
              "UseOpsworksSecurityGroups": true,
              "Region": "ap-northeast-1",
              "DefaultAvailabilityZone": "ap-northeast-1a",
              "CreatedAt": "2017-06-26T01:23:45+00:00",
              "CustomCookbooksSource": {
                  "Url": "https://s3.amazonaws.com/opsworks-demo-assets/opsworks-linux-demo-cookbooks-nodejs.tar.gz",
                  "Type": "archive"
              },
              "ConfigurationManager": {
                  "Version": "12",
                  "Name": "Chef"
              },
              "ChefConfiguration": {},
              "DefaultSubnetId": "subnet-xxxxxxxx",
              "DefaultInstanceProfileArn": "arn:aws:iam::XXXXXXXXXXXX:instance-profile/aws-opsworks-ec2-role",
              "Attributes": {
                  "Color": "rgb(45, 114, 184)"
              },
              "DefaultOs": "Amazon Linux 2017.03",
              "Arn": "arn:aws:opsworks:ap-northeast-1:XXXXXXXXXXXX:stack/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/",
              "AgentVersion": "4023-20170402215230"
          }
        ]
      }


完了
====