EC2でRundeck起動するまで

# EC2作成
今回はCloudFormationで作成する．
- セキュリティグループにssh(20)とRundeck(4440)を設定
- EC2のインスタンスタイプはt3.large

<details><summary>CloudFormationテンプレート</summary>

```yml
AWSTemplateFormatVersion: 2010-09-09
Description: create ec2 for Rundeck

Parameters: 
  projectName:
    Type: String
    Default: rundeck

# ------------------------------------------------------------#
# VPC, パブリックサブネット, IGW，ルートテーブル，セキュリティグループを作成する
# ------------------------------------------------------------#
Resources: 
  myVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 192.168.0.0/21
      Tags:
        - Key: Name
          Value: !Sub ${projectName}-vpc
  subnet:
    Type: AWS::EC2::Subnet
    Properties:
      AvailabilityZone: ap-northeast-1a
      VpcId: !Ref myVPC
      CidrBlock: 192.168.0.0/24
      Tags:
        - Key: Name
          Value: !Sub ${projectName}-subnet
  igw:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: !Sub ${projectName}-igw
  AttachGateway:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref myVPC
      InternetGatewayId: !Ref igw
  routeTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref myVPC
      Tags:
        - Key: Name
          Value: !Sub ${projectName}-routeTable
  route:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref routeTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref igw
  subnetRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref subnet
      RouteTableId: !Ref routeTable
  securityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Enable SSH access via port 22
      VpcId: !Ref myVPC
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: '22'
          ToPort: '22'
          CidrIp: 0.0.0.0/0
        - IpProtocol: tcp
          FromPort: '4440'
          ToPort: '4440'
          CidrIp: 0.0.0.0/0

# ------------------------------------------------------------#
# EC2作成（キーペアは「ec2-test.pemを作成しておく」）
# ------------------------------------------------------------#
  myEC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      KeyName: ec2-test
      ImageId: ami-01d078c099c7f1261
      InstanceType: t3.large
      NetworkInterfaces:
        - AssociatePublicIpAddress: 'true'
          DeviceIndex: '0'
          SubnetId: !Ref subnet
          GroupSet:
            - !Ref securityGroup
      Tags:
        - Key: Name
          Value: !Sub ${projectName}-ec2
```
</details>

# EC2にssh接続
```
ssh -i "~/.ssh/ec2-test.pem" ec2-user@{パブリックIP}
```

# javaをインストール
```
sudo amazon-linux-extras install java-openjdk11
```

# Rundeck ymuリポジトリを追加
```
curl https://raw.githubusercontent.com/rundeck/packaging/main/scripts/rpm-setup.sh 2> /dev/null | sudo bash -s rundeck
```

# Rundeckをインストール
```
sudo yum install rundeck
```

# 設定ファイルの変更
設定ファイル内のlocalhostをEC2インスタンスのパブリックIPアドレスに置換する
```
sudo sed -i "s/localhost/$(curl -s http://checkip.amazonaws.com)/g" /etc/rundeck/rundeck-config.properties
sudo sed -i "s/localhost/$(curl -s http://checkip.amazonaws.com)/g" /etc/rundeck/framework.properties
```

# Rundeckサービスを起動
```
sudo service rundeckd start
```
ログを確認
```
tail -f /var/log/rundeck/service.log
```
以下が表示されたら成功（数分かかる）
```
Grails application running at http://localhost:4440 in environment: production
```

# ブラウザからアクセス
`http://{EC2のIPアドレス}:4440`

ユーザー名とPWは`admin`でログイン
![ログイン画面](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/931058/80cb14ab-d3fc-cc1d-5298-c22dd85ade22.png)

![ログイン後画面](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/931058/93874dcf-e6ba-f674-8bc2-34cd8a23a2c4.png)

# Rundeckサービスを停止
```
sudo service rundeckd stop
```

# 参考
https://docs.rundeck.com/docs/administration/install/linux-rpm.html

https://docs.rundeck.com/docs/learning/howto/how2-terra-rd-aws.html#files