AWS

AWS CLIとEB CLIでElasticBeanstalk環境の作成

 

EB CLIを操作する為に、今回はクライアントのEC2から操作します。

 

AWS設定

$ aws configure

AWS Access Key ID [None]: xxxxxxxxxxxxxxxx
AWS Secret Access Key [None]: xxxxxxxxxxxxxxxx
Default region name [None]: ap-northeast-1
Default output format [None]: json

 

 

$ cat ~/.aws/config

[default]
output = json
region = ap-northeast-1

 

アプリのルートフォルダの作成

$ mkdir ebappTest
$ cd ebappTest

gitコマンドのインストールとアプリのダウンロード
$ sudo yum install git
$ sudo git clone https://github.com/hoge/ebappTest.git

 

git初期化

$ sudo git init .
Reinitialized existing Git repository in /home/ec2-user/ebappTest/.git/

 

プロフィール設定

$ git config --global user.name "yuu"
$ git config --global user.email yuu@example.net

 

$ git add test.txt
$ git commit -m "initial check-in"

 

外部にプッシュする場合

リモートリポジトリ登録
$ git remote add origin https://github.com/hoge/ebappTest.git

push
$ git push -u origin master

 

 

eb環境の初期化

[ec2-user@ip-172-31-33-180 ebappTest]$ eb init

リージョンの選択
Select a default region
1) us-east-1 : US East (N. Virginia)
2) us-west-1 : US West (N. California)
3) us-west-2 : US West (Oregon)
4) eu-west-1 : EU (Ireland)
5) eu-central-1 : EU (Frankfurt)
6) ap-south-1 : Asia Pacific (Mumbai)
7) ap-southeast-1 : Asia Pacific (Singapore)
8) ap-southeast-2 : Asia Pacific (Sydney)
9) ap-northeast-1 : Asia Pacific (Tokyo)
10) ap-northeast-2 : Asia Pacific (Seoul)
11) sa-east-1 : South America (Sao Paulo)
12) cn-north-1 : China (Beijing)
13) cn-northwest-1 : China (Ningxia)
14) us-east-2 : US East (Ohio)
15) ca-central-1 : Canada (Central)
16) eu-west-2 : EU (London)
17) eu-west-3 : EU (Paris)
18) eu-north-1 : EU (Stockholm)
(default is 3): 9

アプリケーション名がある場合
Enter Application Name
(default is "ebappTest"): ebappTest
Application ebappTest has been created.

It appears you are using PHP. Is this correct?
(Y/n): Y

Select a platform version.
1) PHP 7.2
2) PHP 7.1
3) PHP 7.0
4) PHP 5.6
5) PHP 5.5
6) PHP 5.4
7) PHP 5.3
(default is 1): 1
Do you wish to continue with CodeCommit? (y/N) (default is n): n
Do you want to set up SSH for your instances?
(Y/n): Y

キーの選択
Select a keypair.
1) TEST-1217
2) TEST-21081203
3) TESTSG-20181212
4) [ Create new KeyPair ]
(default is 3): 1

 

 

$ ls -laht
total 16K
drwx------ 7 ec2-user ec2-user 182 Dec 20 06:10 ..
drwxr-xr-x 5 ec2-user ec2-user 139 Dec 20 06:10 .
-rw-rw-r-- 1 ec2-user ec2-user 108 Dec 20 06:10 .gitignore
drwxrwxr-x 2 ec2-user ec2-user  24 Dec 20 06:06 .elasticbeanstalk
drwxr-xr-x 8 ec2-user ec2-user 163 Dec 20 03:26 .git
drwxr-xr-x 2 ec2-user ec2-user  27 Dec 20 03:25 .ebextensions
-rw-r--r-- 1 ec2-user ec2-user  60 Dec 20 03:25 healthcheck.php
-rw-r--r-- 1 ec2-user ec2-user  29 Dec 20 03:25 index.php
-rw-r--r-- 1 ec2-user ec2-user  11 Dec 20 03:25 README.md

ヘルスチェックファイルを作っておく

 

確認

$ cat /home/ec2-user/ebappTest/.gitignore

# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml

 

確認

$ cat /home/ec2-user/ebappTest/.elasticbeanstalk/config.yml

branch-defaults:
  master:
    environment: null
    group_suffix: null
global:
  application_name: ebappTest
  branch: null
  default_ec2_keyname: TEST-1217
  default_platform: PHP 7.2
  default_region: ap-northeast-1
  include_git_submodules: true
  instance_profile: null
  platform_name: null
  platform_version: null
  profile: null
  repository: null
  sc: git
  workspace_type: Application

 

 

VPC作成

$ aws ec2 create-vpc --cidr-block 10.0.0.0/16

{
    "Vpc": {
        "VpcId": "vpc-01f38b4afc33e2d60",
        "InstanceTenancy": "default",
        "Tags": [],
        "CidrBlockAssociationSet": [
            {
                "AssociationId": "vpc-cidr-assoc-058a5560870cc112e",
                "CidrBlock": "10.0.0.0/16",
                "CidrBlockState": {
                    "State": "associated"
                }
            }
        ],
        "Ipv6CidrBlockAssociationSet": [],
        "State": "pending",
        "DhcpOptionsId": "dopt-e5271280",
        "CidrBlock": "10.0.0.0/16",
        "IsDefault": false
    }
}

 

 

VPCにName タグを追加

$ aws ec2 create-tags --resources vpc-01f38b4afc33e2d60 --tags Key=Name,Value="vpc test"

 

VPCのルートテーブルの確認

$ aws ec2 describe-route-tables --filters "Name=vpc-id,Values=vpc-01f38b4afc33e2d60"

{
    "RouteTables": [
        {
            "Associations": [
                {
                    "RouteTableAssociationId": "rtbassoc-014a2dbc35615739d",
                    "Main": true,
                    "RouteTableId": "rtb-0cafa4febb22d37bd"
                }
            ],
            "RouteTableId": "rtb-0cafa4febb22d37bd",
            "VpcId": "vpc-01f38b4afc33e2d60",
            "PropagatingVgws": [],
            "Tags": [],
            "Routes": [
                {
                    "GatewayId": "local",
                    "DestinationCidrBlock": "10.0.0.0/16",
                    "State": "active",
                    "Origin": "CreateRouteTable"
                }
            ]
        }
    ]
}

 

 

 

ルートテーブルにName タグを追加

$ aws ec2 create-tags --resources rtb-0cafa4febb22d37bd --tags Key=Name, Value="rtb test main"

{
    "NetworkAcls": [
        {
            "Associations": [],
            "NetworkAclId": "acl-0492173698a8ebb1c",
            "VpcId": "vpc-01f38b4afc33e2d60",
            "Tags": [],
            "Entries": [
                {
                    "RuleNumber": 100,
                    "Protocol": "-1",
                    "Egress": true,
                    "CidrBlock": "0.0.0.0/0",
                    "RuleAction": "allow"
                },
                {
                    "RuleNumber": 32767,
                    "Protocol": "-1",
                    "Egress": true,
                    "CidrBlock": "0.0.0.0/0",
                    "RuleAction": "deny"
                },
                {
                    "RuleNumber": 100,
                    "Protocol": "-1",
                    "Egress": false,
                    "CidrBlock": "0.0.0.0/0",
                    "RuleAction": "allow"
                },
                {
                    "RuleNumber": 32767,
                    "Protocol": "-1",
                    "Egress": false,
                    "CidrBlock": "0.0.0.0/0",
                    "RuleAction": "deny"
                }
            ],
            "IsDefault": true
        }
    ]
}

 

 

ACLにNAMEタグを追加

$ aws ec2 create-tags --resources acl-0492173698a8ebb1c --tags Key=Name,Value="acl test"

 

VPCのデフォルトセキュリティグループの設定を確認

$ aws ec2 describe-security-groups --filters "Name=vpc-id,Values=vpc-01f38b4afc33e2d60"
{
    "SecurityGroups": [
        {
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "PrefixListIds": [],
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "UserIdGroupPairs": [],
                    "Ipv6Ranges": []
                }
            ],
            "Description": "default VPC security group",
            "IpPermissions": [
                {
                    "IpProtocol": "-1",
                    "PrefixListIds": [],
                    "IpRanges": [],
                    "UserIdGroupPairs": [
                        {
                            "UserId": "840155334575",
                            "GroupId": "sg-0a33660dc363996ce"
                        }
                    ],
                    "Ipv6Ranges": []
                }
            ],
            "GroupName": "default",
            "VpcId": "vpc-01f38b4afc33e2d60",
            "OwnerId": "840155334575",
            "GroupId": "sg-0a33660dc363996ce"
        }
    ]
}

 

 

デフォルトセキュリティグループにNAMEタグを追加

$ aws ec2 create-tags --resources sg-0a33660dc363996ce --tags Key=Name,Value="sg test default"

 

VPCのDNSホスト名を追加する ※RDSにエンドポイントでアクセスする為

$ aws ec2 modify-vpc-attribute --vpc-id vpc-01f38b4afc33e2d60 --enable-dns-hostnames

 

サブネットの作成

$ aws ec2 create-subnet --vpc-id vpc-01f38b4afc33e2d60 --cidr-block 10.0.1.0/24 --availability-zone ap-northeast-1a

{
    "Subnet": {
        "AvailabilityZone": "ap-northeast-1a",
        "AvailableIpAddressCount": 251,
        "DefaultForAz": false,
        "Ipv6CidrBlockAssociationSet": [],
        "VpcId": "vpc-01f38b4afc33e2d60",
        "State": "pending",
        "MapPublicIpOnLaunch": false,
        "SubnetId": "subnet-0b2e2a26f07afa705",
        "CidrBlock": "10.0.1.0/24",
        "AssignIpv6AddressOnCreation": false
    }
}

 

 

 

$ aws ec2 create-subnet --vpc-id vpc-01f38b4afc33e2d60 --cidr-block 10.0.2.0/24 --availability-zone ap-northeast-1c

{
    "Subnet": {
        "AvailabilityZone": "ap-northeast-1c",
        "AvailableIpAddressCount": 251,
        "DefaultForAz": false,
        "Ipv6CidrBlockAssociationSet": [],
        "VpcId": "vpc-01f38b4afc33e2d60",
        "State": "pending",
        "MapPublicIpOnLaunch": false,
        "SubnetId": "subnet-03083b0694cb2695b",
        "CidrBlock": "10.0.2.0/24",
        "AssignIpv6AddressOnCreation": false
    }
}

 

 

$ aws ec2 create-subnet --vpc-id vpc-01f38b4afc33e2d60 --cidr-block 10.0.3.0/24 --availability-zone ap-northeast-1d

{
    "Subnet": {
        "AvailabilityZone": "ap-northeast-1d",
        "AvailableIpAddressCount": 251,
        "DefaultForAz": false,
        "Ipv6CidrBlockAssociationSet": [],
        "VpcId": "vpc-01f38b4afc33e2d60",
        "State": "pending",
        "MapPublicIpOnLaunch": false,
        "SubnetId": "subnet-0664682d62aa9e8fc",
        "CidrBlock": "10.0.3.0/24",
        "AssignIpv6AddressOnCreation": false
    }
}

 

 

IGW(インターネットゲートウェイ)の作成

$ aws ec2 create-internet-gateway

{
    "InternetGateway": {
        "Tags": [],
        "Attachments": [],
        "InternetGatewayId": "igw-0cc9f024d44204888"
    }
}

 

 

IGWにNAMEタグを追加する

$ aws ec2 create-tags --resources igw-0cc9f024d44204888 --tags Key=Name,Value="igw test"

 

VPCにIGWをアタッチする

$ aws ec2 attach-internet-gateway --internet-gateway-id igw-0cc9f024d44204888 --vpc-id vpc-01f38b4afc33e2d60

 

確認

$ aws ec2 describe-internet-gateways --internet-gateway-id igw-0cc9f024d44204888

{
    "InternetGateways": [
        {
            "Tags": [
                {
                    "Value": "igw test",
                    "Key": "Name"
                }
            ],
            "Attachments": [
                {
                    "State": "available",
                    "VpcId": "vpc-01f38b4afc33e2d60"
                }
            ],
            "InternetGatewayId": "igw-0cc9f024d44204888"
        }
    ]
}

 

 

 

パブリックサブネット用のルートテーブル作成

 

$ aws ec2 create-route-table --vpc-id vpc-01f38b4afc33e2d60

{
    "RouteTable": {
        "Associations": [],
        "RouteTableId": "rtb-0d0c22b6949e0adfc",
        "VpcId": "vpc-01f38b4afc33e2d60",
        "PropagatingVgws": [],
        "Tags": [],
        "Routes": [
            {
                "GatewayId": "local",
                "DestinationCidrBlock": "10.0.0.0/16",
                "State": "active",
                "Origin": "CreateRouteTable"
            }
        ]
    }
}

 

ルートテーブルに Name タグを追加する。

$ aws ec2 create-tags --resources rtb-0d0c22b6949e0adfc --tags Key=Name,Value="rtb test public"

 

パブリックサブネット用ルートテーブルにインターネットゲートウェイを関連付ける

$ aws ec2 create-route --route-table-id rtb-0d0c22b6949e0adfc --destination-cidr-block 0.0.0.0/0 --gateway-id igw-0cc9f024d44204888

{
    "Return": true
}

 

$ aws ec2 associate-route-table --route-table-id rtb-0d0c22b6949e0adfc --subnet-id subnet-0b2e2a26f07afa705

{
    "AssociationId": "rtbassoc-0f0c45f90f70d8fdb"
}

 

aws ec2 associate-route-table --route-table-id rtb-0d0c22b6949e0adfc --subnet-id subnet-03083b0694cb2695b

{
    "AssociationId": "rtbassoc-04dafe1f5d8a32a03"
}

 

$ aws ec2 associate-route-table --route-table-id rtb-0d0c22b6949e0adfc --subnet-id subnet-0664682d62aa9e8fc

{
    "AssociationId": "rtbassoc-0e573c428282f8092"
}

 

 

ElasticBeanstalk アプリ環境の作成

  •  –vpc.publicip ・・・EC2にpublicIPを付与
  • –vpc.elbpublic・・・ELBをpublicに、付与しないとinternalになる

 

$ eb create --vpc.id vpc-01f38b4afc33e2d60 --vpc.elbsubnets subnet-0b2e2a26f07afa705,subnet-03083b0694cb2695b --vpc.ec2subnets subnet-0b2e2a26f07afa705 --vpc.securitygroup sg-0a33660dc363996ce --vpc.publicip --vpc.elbpublic --instance_type t2.nano


Enter Environment Name
(default is App-TEST1-dev):
Enter DNS CNAME prefix
(default is App-TEST1-dev):

Select a load balancer type
1) classic
2) application
3) network
(default is 2):
Creating application version archive "app-eb1e-181221_065649".
Uploading App-TEST1/app-eb1e-181221_065649.zip to S3. This may take a while.
Upload Complete.
Application App-TEST1 has been created.

 

デプロイ

$ eb deploy

 

ElasticBeanstalkのCNANEでアクセス出来るようにする

$ eb open

 

ステータスの確認

$ eb status

Environment details for: App-TEST1-dev
  Application name: App-TEST1
  Region: ap-northeast-1
  Deployed Version: app-8927-181221_072031
  Environment ID: e-fj3rdn4mms
  Platform: arn:aws:elasticbeanstalk:ap-northeast-1::platform/PHP 7.2 running on 64bit Amazon Linux/2.8.5
  Tier: WebServer-Standard-1.0
  CNAME: SG-App-TEST1-dev.ap-northeast-1.elasticbeanstalk.com
  Updated: 2018-12-21 07:20:55.025000+00:00
  Status: Ready ←●Ready確認
  Health: Green

 

 

.ebextensionsによる拡張

 

# vi /home/ec2-user/ebappTest/.ebextensions/server.config


option_settings:
  - namespace:  aws:autoscaling:launchconfiguration
    option_name:  InstanceType
    value: t2.nano
  - namespace:  aws:autoscaling:asg
    option_name:  MaxSize
    value: '3'
  - namespace:  aws:autoscaling:asg
    option_name:  MinSize
    value: '1'

 

デプロイ

$ git add -A
$ git commit -m "20181225-0225"
$ eb deploy

 

options_settingsについて

 

オプションの名前空間やプロパティは既存環境から確認出来る

$ eb config

exitは【CTRL+X】

 

CloudFormationのプロパティのすべてが使えるわけではないので注意。

 

AWS ドキュメント ≫ AWS Elastic Beanstalk ≫ 開発者ガイド ≫ AWS Elastic Beanstalk の詳細な環境設定 ≫ 設定オプション
https://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/command-options.html

AWS::AutoScaling::LaunchConfiguration
https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-properties-as-launchconfig.html

 

 

 

 

Amazonおすすめ

iPad 9世代 2021年最新作

iPad 9世代出たから買い替え。安いぞ!🐱 初めてならiPad。Kindleを外で見るならiPad mini。ほとんどの人には通常のiPadをおすすめします><

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)