Cloud9の実行環境を構築する

AWS CDKの学習環境としてCloud9の構築を実施したのでメモ

## ネットワークの構築  
Cloud9のインスタンスを動かすためのネットワークを構築します。
今回は以下のリソースをCloudFormationで作成するため、テンプレートファイルを作成します。

- VPC(10.0.0.0/16)
- PublicSubnet(10.0.0.0/24)
- PrivateSubnet(10.0.1.0/24)
- PublicRouteTable
- PrivateRouteTable
- InternetGateway
- ElasticIP
- NATGateway

```networkForCloud9.yaml
AWSTemplateFormatVersion: 2010-09-09
Description: Deploy Cloud9 VPC

Resources:
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsHostnames: true
      Tags:
      - Key: Name
        Value: Cloud9 VPC

  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
      - Key: Name
        Value: Cloud9 Internet Gateway

  AttachGateway:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref VPC
      InternetGatewayId: !Ref InternetGateway

  PublicSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      CidrBlock: 10.0.0.0/24
      AvailabilityZone: !Select 
        - '0'
        - !GetAZs ''
      Tags:
        - Key: Name
          Value: Public Subnet 1

  PrivateSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      CidrBlock: 10.0.1.0/24
      AvailabilityZone: !Select 
        - '0'
        - !GetAZs ''
      Tags:
        - Key: Name
          Value: Private Subnet 1

  PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: Public Route Table

  PublicRoute:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PublicRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway

  PublicSubnetRouteTableAssociation1:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PublicSubnet1
      RouteTableId: !Ref PublicRouteTable

  NATGatewayEIP:
    Type: AWS::EC2::EIP
    Properties: 
      Domain: vpc

  NATGateway:
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt NATGatewayEIP.AllocationId
      ConnectivityType: public
      SubnetId: !Ref PublicSubnet1
      Tags:
      - Key: Name
        Value: Cloud9 NATGateway

  PrivateRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
      - Key: Name
        Value: Private Route Table

  PrivateRoute:
    DependsOn: NATGateway
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PrivateRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref NATGateway

  PrivateSubnetRouteTableAssociation1:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PrivateSubnet1
      RouteTableId: !Ref PrivateRouteTable
```

CloudFormationでテンプレートファイルをアップロードして環境を構築します。

![Cloud9-1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/386901/71158c57-4eb6-fb46-68e0-6f7f67b7c46c.png)

![Cloud9-2.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/386901/3719bf81-9478-17a8-48be-c7c240f13d9e.png)

## Cloud9の構築
AWS コンソール上でCloud9を構築します。
VPCとサブネットには、先ほどCloudFormationで構築したVPCとPrivateSubnetを指定します。


![Cloud9-3.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/386901/992efa38-82ff-670c-b52b-0e46b9c479f1.png)
![Cloud9-4.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/386901/bdc06bb6-8a76-dfe7-7ded-3eaee7d21525.png)


しばらくしたあとに、環境が構築されたことを確認します。
コンソールからCloud9に接続します。

![Cloud9-5.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/386901/15ec187e-5e21-a382-085b-d06e3f1b62d3.png)
![Cloud9-6.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/386901/a5279448-31e8-56fc-b343-066b83139abe.png)


## 参考
https://docs.aws.amazon.com/ja_jp/cloud9/latest/user-guide/create-environment.html