[AWS]ElasticBeanstalkをコマンドラインから起動してみた。

# [AWS]ElasticBeanstalkをコマンドラインから起動してみた。

## AWS elasticbeanstalkにアプリケーション追加
<pre>
$ aws --region ap-northeast-1 elasticbeanstalk create-application --application-name appname
{
    "Application": {
        "ApplicationName": "appname",
        "ConfigurationTemplates": [],
        "DateUpdated": "2016-07-11T07:06:00.557Z",
        "DateCreated": "2016-07-11T07:06:00.557Z"
    }
}
</pre>

## 環境構築用のJsonファイル取得
<pre>
$ aws --region ap-northeast-1 elasticbeanstalk create-environment --generate-cli-skeleton > create-environment.json
$ cat create-environment.json
{
    "ApplicationName": "",
    "EnvironmentName": "",
    "GroupName": "",
    "Description": "",
    "CNAMEPrefix": "",
    "Tier": {
        "Name": "",
        "Type": "",
        "Version": ""
    },
    "Tags": [
        {
            "Key": "",
            "Value": ""
        }
    ],
    "VersionLabel": "",
    "TemplateName": "",
    "SolutionStackName": "",
    "OptionSettings": [
        {
            "ResourceName": "",
            "Namespace": "",
            "OptionName": "",
            "Value": ""
        }
    ],
    "OptionsToRemove": [
        {
            "ResourceName": "",
            "Namespace": "",
            "OptionName": ""
        }
    ]
}
</pre>

## 各項目について箇条書き
[create-environment — AWS CLI 1.10.47 Command Reference](http://docs.aws.amazon.com/cli/latest/reference/elasticbeanstalk/create-environment.html)
ApplicationName: アプリケーション名
GroupName: グループ名
Description: 説明
CNAMEPrefix: CNAMEのPrefix（xxx.ap-northeast-1.elasticbeanstalk.comのxxx）
Tier: Worker環境を作成する際に利用する？
Tags: タグ
VersionLabel: デプロイするアプリケーションのバージョン
TemplateName: 設定ファイルのテンプレート名
SolutionStackName: ソリューションスタック名（tomcatのバージョンいくつかなど）
[サポートされているプラットフォーム - AWS Elastic Beanstalk](http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/concepts.platforms.html)
OptionSettings: オプションの配列
[すべての環境に対する汎用オプション - AWS Elastic Beanstalk](http://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/command-options-general.html#command-options-general-ec2vpc)
OptionsToRemove: 削除したいオプション？

## 設定ファイル書き換え
VPC利用・シングルaz・スティッキーセッション
<pre>
$ cat create-environment.json
{
    "ApplicationName": "appname",
    "EnvironmentName": "envname",
    "Description": "",
    "Tags": [
        {
            "Key": "RSC",
            "Value": "xxxxxxxx"
        }
    ],
    "SolutionStackName": "64bit Amazon Linux 2016.03 v2.1.3 running Tomcat 8 Java 8",
    "OptionSettings": [
        {
            "Namespace": "aws:autoscaling:launchconfiguration",
            "OptionName": "EC2KeyName",
            "Value": "xxxxxxxx"
        },
        {
            "Namespace": "aws:autoscaling:launchconfiguration",
            "OptionName": "IamInstanceProfile",
            "Value": "Role-xxxxxxxx"
        },
        {
            "Namespace": "aws:autoscaling:launchconfiguration",
            "OptionName": "InstanceType",
            "Value": "t2.micro"
        },
        {
            "Namespace": "aws:autoscaling:launchconfiguration",
            "OptionName": "SecurityGroups",
            "Value": "sg-xxxxxxxx"
        },
        {
            "Namespace": "aws:ec2:vpc",
            "OptionName": "VPCId",
            "Value": "vpc-xxxxxxxx"
        },
        {
            "Namespace": "aws:ec2:vpc",
            "OptionName": "Subnets",
            "Value": "subnet-xxxxxxxx"
        },
        {
            "Namespace": "aws:ec2:vpc",
            "OptionName": "ELBSubnets",
            "Value": "subnet-xxxxxxxx"
        },
        {
            "Namespace": "aws:ec2:vpc",
            "OptionName": "ELBScheme",
            "Value": "public"
        },
        {
            "Namespace": "aws:ec2:vpc",
            "OptionName": "AssociatePublicIpAddress",
            "Value": "true"
        },
        {
            "Namespace": "aws:elasticbeanstalk:environment",
            "OptionName": "ServiceRole",
            "Value": "aws-elasticbeanstalk-service-role"
        },
        {
            "Namespace": "aws:elasticbeanstalk:healthreporting:system",
            "OptionName": "SystemType",
            "Value": "enhanced"
        },
        {
            "Namespace": "aws:elasticbeanstalk:hostmanager",
            "OptionName": "LogPublicationControl",
            "Value": "true"
        },
        {
            "Namespace": "aws:elasticbeanstalk:managedactions",
            "OptionName": "ManagedActionsEnabled",
            "Value": "true"
        },
        {
            "Namespace": "aws:elasticbeanstalk:managedactions",
            "OptionName": "PreferredStartTime",
            "Value": "Fri:17:00"
        },
        {
            "Namespace": "aws:elasticbeanstalk:managedactions:platformupdate",
            "OptionName": "UpdateLevel",
            "Value": "minor"
        },
        {
            "Namespace": "aws:elb:policies",
            "OptionName": "Stickiness Policy",
            "Value": "true"
        },
        {
            "Namespace": "aws:elb:policies",
            "OptionName": "Stickiness Cookie Expiration",
            "Value": "1800"
        }
    ],
    "OptionsToRemove": [
    ]
}
</pre>

## 作成
<pre>
$ aws --region ap-northeast-1 elasticbeanstalk create-environment --cli-input-json file:///tmp/create-environment.json
{
    "ApplicationName": "appname",
    "EnvironmentName": "envname",
    "Status": "Launching",
    "Description": "",
    "EnvironmentId": "e-xxxxxxxx",
    "SolutionStackName": "64bit Amazon Linux 2016.03 v2.1.3 running Tomcat 8 Java 8",
    "Health": "Grey",
    "Tier": {
        "Version": " ",
        "Type": "Standard",
        "Name": "WebServer"
    },
    "DateUpdated": "2016-07-11T08:47:49.751Z",
    "DateCreated": "2016-07-11T08:47:49.751Z"
}
</pre>

## エラー
<pre>
Environment health has transitioned from Pending to Warning. Initialization in progress (running for 2 minutes). Unable to assume role "arn:aws:iam::xxxxxxxxxxxxx:role/arn:aws:iam::xxxxxxxxxxxx:role/aws-elasticbeanstalk-service-role". Verify that the role exists and is configured correctly.
</pre>
iamのarnをフルパスで指定していたが、ServiceRoleはarn:aws:iam::xxxxxxxxxxxxx:role/以下のみ記載すれば良い