LoginSignup
0
0

More than 5 years have passed since last update.

[JAWS-UG CLI] EC2 # インスタンスの作成 (Public: Redmine AMI)

Last updated at Posted at 2017-02-13

前提条件

EC2への権限

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

AWS CLIのバージョン

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

  • AWS CLI 1.11.14
コマンド
aws --version

結果(例):

  aws-cli/1.11.34 Python/2.7.10 Darwin/15.6.0 botocore/1.4.91

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

コマンド
sudo -H pip install -U awscli

AWSアカウントの属性

AWSアカウントがEC2-Classicに対応していないこと。

コマンド
AWS_SUPPORT_PLATFORMS=$( \
         aws ec2 describe-account-attributes \
           --query 'AccountAttributes[?AttributeName == `supported-platforms`].AttributeValues[].AttributeValue' \
           --output text \
) \
        && echo ${AWS_SUPPORT_PLATFORMS}

結果:

  VPC

注釈: 'VPC'の他に'EC2'が表示される場合、別のアカウントを作成もしくは
利用し てください。

0. 準備

0.1. リージョンの決定

変数の設定
export AWS_DEFAULT_REGION='ap-northeast-1'

0.2. プロファイルの確認

プロファイルが想定のものになっていることを確認します。

変数の確認
aws configure list

結果(例):

        Name                    Value             Type    Location
        ----                    -----             ----    --------
     profile       ec2full-prjZ-mbp13        env    AWS_DEFAULT_PROFILE
  access_key     ****************XXXX shared-credentials-file
  secret_key     ****************XXXX shared-credentials-file
      region        ap-northeast-1        env    AWS_DEFAULT_REGION

0.3. VPCの指定

既存のVPCに割り当てられているCIDRブロックを確認します。

コマンド
aws ec2 describe-vpcs \
        --query 'Vpcs[].CidrBlock'

結果(例):

  [
    "172.31.0.0/16",
    "172.18.0.0/16"
  ]

ここでは、172.18.0.0/16を範囲とするVPCを選択します。

変数の設定
VPC_CIDR='172.18.0.0/16'

VPC IDを取得します。

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

結果(例):

  vpc-xxxxxxxx

0.4. サブネットの指定

変数の設定
VPC_SUBNET_CIDR='172.18.16.0/24'
コマンド
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

1. 事前作業

1.1. セキュリティグループの決定

セキュリティグループの一覧を確認します。

コマンド
aws ec2 describe-security-groups \
        --query "SecurityGroups[?VpcId == \` ${VPC_ID}\`].GroupName"

結果(例):

  [
    "default",
    "ec2-ssh-global-inbound"
  ]

対象となるセキュリティグループ名を指定します。

変数の設定
VPC_SG_NAME='ec2-ssh-global-inbound'

セキュリティグループIDを取得します。

コマンド
VPC_SG_ID=$( \
        aws ec2 describe-security-groups \
          --filter Name=group-name,Values=${VPC_SG_NAME} \
          --query "SecurityGroups[?VpcId == \` ${VPC_ID}\`].GroupId" \
          --output text \
) \
        && echo ${VPC_SG_ID}

結果(例):

  sg-xxxxxxxx

セキュリティグループの内容を確認しましょう。

コマンド
aws ec2 describe-security-groups \
        --group-ids ${VPC_SG_ID}

セキュリティグループを配列に入れておきます。

変数の設定
ARRAY_VPC_SG_IDS="${VPC_SG_ID}" \
        && echo ${ARRAY_VPC_SG_IDS}

1.2. イメージIDの決定

AMIを選択します。

変数の設定
EC2_IMAGE_NAME='redmine-prototype-20170213'
コマンド
EC2_IMAGE_ID=$( \
        aws ec2 describe-images \
          --filters Name=name,Values="${EC2_IMAGE_NAME}" \
          --query 'Images[].ImageId' \
          --output text \
) \
        && echo ${EC2_IMAGE_ID}

結果(例):

  ami-XXXXXXXX

1.3. インスタンスタイプの決定

変数の設定
EC2_INSTANCE_TYPE='t2.micro'

1.4. 稼動インスタンスを確認

同一でインスタンスが起動していないことを確認します。

コマンド
EC2_INSTANCE_STATUS='running'
コマンド
aws ec2 describe-instances \
        --filters Name=instance-state-name,Values=${EC2_INSTANCE_STATUS}

結果:

  {
    "Reservations": []
  }

2. インスタンス起動

2.1. インスタンス起動

変数の確認
cat << ETX

        EC2_IMAGE_ID:              ${EC2_IMAGE_ID}
        EC2_INSTANCE_TYPE:         ${EC2_INSTANCE_TYPE}
        ARRAY_VPC_SG_IDS:          ${ARRAY_VPC_SG_IDS}
        VPC_SUBNET_ID              ${VPC_SUBNET_ID}

ETX

コマンド

  aws ec2 run-instances \
    --image-id ${EC2_IMAGE_ID} \
    --instance-type ${EC2_INSTANCE_TYPE} \
    --security-group-ids ${ARRAY_VPC_SG_IDS} \
    --subnet-id ${VPC_SUBNET_ID} \
    --associate-public-ip-address

結果(例):

  {
    "OwnerId": "XXXXXXXXXXXX",
    "ReservationId": "r-0175dfcc93e29ff43",
    "Groups": [],
    "Instances": [
      {
          "Monitoring": {
              "State": "disabled"
          },
          "PublicDnsName": "",
          "RootDeviceType": "ebs",
          "State": {
              "Code": 0,
              "Name": "pending"
          },
          "EbsOptimized": false,
          "LaunchTime": "2017-02-13T01:23:45.000Z",
          "PrivateIpAddress": "xxx.xxx.xxx.xxx",
          "ProductCodes": [],
          "VpcId": "vpc-xxxxxxxx",
          "StateTransitionReason": "",
          "InstanceId": "i-xxxxxxxxxxxxxxxxx",
          "ImageId": "ami-00c08367",
          "PrivateDnsName": "ip-xxx-xxx-xxx-xxx.ap-northeast-1.compute.internal",
          "SecurityGroups": [
              {
                  "GroupName": "ec2-ssh-global-inbound",
                  "GroupId": "sg-xxxxxxxx"
              }
          ],
          "ClientToken": "",
          "SubnetId": "subnet-xxxxxxxx",
          "InstanceType": "t2.micro",
          "NetworkInterfaces": [
              {
                  "Status": "in-use",
                  "MacAddress": "xx:xx:xx:xx:xx:xx",
                  "SourceDestCheck": true,
                  "VpcId": "vpc-xxxxxxxx",
                  "Description": "",
                  "NetworkInterfaceId": "eni-xxxxxxxx",
                  "PrivateIpAddresses": [
                      {
                          "Primary": true,
                          "PrivateIpAddress": "xxx.xxx.xxx.xxx"
                      }
                  ],
                  "Ipv6Addresses": [],
                  "Attachment": {
                      "Status": "attaching",
                      "DeviceIndex": 0,
                      "DeleteOnTermination": true,
                      "AttachmentId": "eni-attach-xxxxxxxx",
                      "AttachTime": "2017-02-13T01:23:45.000Z"
                  },
                  "Groups": [
                      {
                          "GroupName": "ec2-ssh-global-inbound",
                          "GroupId": "sg-xxxxxxxx"
                      }
                  ],
                  "SubnetId": "subnet-xxxxxxxx",
                  "OwnerId": "XXXXXXXXXXXX",
                  "PrivateIpAddress": "xxx.xxx.xxx.xxx"
              }
          ],
          "SourceDestCheck": true,
          "Placement": {
              "Tenancy": "default",
              "GroupName": "",
              "AvailabilityZone": "ap-northeast-1a"
          },
          "Hypervisor": "xen",
          "BlockDeviceMappings": [],
          "Architecture": "x86_64",
          "StateReason": {
              "Message": "pending",
              "Code": "pending"
          },
          "RootDeviceName": "/dev/xvda",
          "VirtualizationType": "hvm",
          "AmiLaunchIndex": 0
      }
    ]
  }

2.2. インスタンスIDの取得

起動中のインスタンスからインスタンスIDを取得します。

変数の設定
EC2_INSTANCE_STATUS='pending'
コマンド
ARRAY_EC2_INSTANCE_ID=$( \
        aws ec2 describe-instances \
          --filters Name=instance-state-name,Values=${EC2_INSTANCE_STATUS} \
          --query 'Reservations[].Instances[].InstanceId' \
          --output text \
) \
        && echo ${ARRAY_EC2_INSTANCE_ID}

結果(例):

  i-xxxxxxxx
変数の設定
EC2_INSTANCE_ID="${ARRAY_EC2_INSTANCE_ID}" \
        && echo ${EC2_INSTANCE_ID}

3. 事後作業

インスタンスのステータス確認

コマンド
EC2_INSTANCE_STATE=$( \
        aws ec2 describe-instances \
        --instance-ids ${EC2_INSTANCE_ID} \
        --query 'Reservations[].Instances[].State.Name' \
        --output text \
) \
        && echo ${EC2_INSTANCE_STATE}

結果(例):

  running
コマンド
EC2_PUBLIC_IP=$( \
        aws ec2 describe-instances \
          --instance-id ${EC2_INSTANCE_ID} \
          --query "Reservations[].Instances[].PublicIpAddress" \
          --output text \
) \
        && echo ${EC2_PUBLIC_IP}

結果(例):

  xxx.xxx.xxx.xxx

完了

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0