Ansible で Amazon Aurora の起動・停止

# はじめに
Ansible で Amazon Aurora をスナップショットを利用して起動・停止するサンプルです。
・Amazon Linux 2 にて検証

# 構成
```
playbook
|-- ansible.cfg
|-- roles
    |-- aws
        |-- rds
            |-- defaults
                |-- main.yml
            |-- tasks
                |-- main.yml
                |-- dependency_install.yml
                |-- rds_start.yml
                |-- rds_stop.yml
```

# ansible.cfg
```INI:ansible.cfg
[defaults]
interpreter_python = /usr/bin/python3
```

# 変数
```YAML:roles/aws/rds/defaults/main.yml
---
# pip 実行可能ファイル
pip_executable: pip3

# 依存モジュール
dependencies:
  - boto
  - boto3
  - botocore

# AWS のリージョン
aws_region: ap-northeast-1

# RDS DB インスタンス ID
rds_db_instance_id: sample

# RDS DB クラスター ID
rds_db_cluster_id: "{{ rds_db_instance_id }}-cluster"

# RDS DB サブネットグループ
rds_db_subnet_group: sample

# RDS VPC セキュリティグループ
rds_vpc_security_group_ids:
  - sg-xxxxxxxx
  - sg-yyyyyyyy

# RDS DB クラスターパラメータグループ
rds_db_cluster_parameter_group: sample-cluster

# RDS DB アベイラビリティーゾーン
rds_db_availability_zone: "{{ aws_region }}a"

# RDS DB パラメータグループ
rds_db_parameter_group: sample

# RDS DB エンジン
rds_db_engine: aurora-mysql

# RDS DB インスタンスクラス
rds_db_instance_class: db.t3.small

# RDS スナップショット ID
rds_snapshot_id: "{{ rds_db_cluster_id }}-snapshot"
```

# タスク定義
```YAML:roles/aws/rds/tasks/main.yml
---
- import_tasks: rds_start.yml
  tags:
    - start

- import_tasks: rds_stop.yml
  tags:
    - stop
```

# 依存モジュールのインストール
```YAML:roles/aws/rds/tasks/dependency_install.yml
---
- name: Install dependences.
  pip:
    name: "{{ dependencies }}"
    executable: "{{ pip_executable }}"
```

# Amazon Aurora の起動
※ 実行は `--tags` で `start` を指定

```YAML:roles/aws/rds/tasks/rds_start.yml
---
- import_tasks: dependency_install.yml

- name: Get Aurora DB cluster information.
  ansible.builtin.command: >
    aws rds describe-db-clusters
    --query 'DBClusters[?DBClusterIdentifier == `{{ rds_db_cluster_id }}`]'
    --output json
    --region {{ aws_region }}
  register: rds_db_cluster

- name: Restore Aurora DB cluster.
  ansible.builtin.command: >
    aws rds restore-db-cluster-from-snapshot
    --db-cluster-identifier {{ rds_db_cluster_id }}
    --snapshot-identifier {{ rds_snapshot_id }}
    --db-subnet-group-name {{ rds_db_subnet_group }}
    --vpc-security-group-ids {{ rds_vpc_security_group_ids | join(' ') }}
    --db-cluster-parameter-group-name {{ rds_db_cluster_parameter_group }}
    --engine {{ rds_db_engine }}
    --region {{ aws_region }}
  when: rds_db_cluster.stdout | from_json | length == 0

- name: Wait for Aurora DB cluster to be available.
  ansible.builtin.command: >
    aws rds wait db-cluster-available
    --db-cluster-identifier {{ rds_db_cluster_id }}
    --region {{ aws_region }}

- name: Create DB instance.
  community.aws.rds_instance:
    db_cluster_identifier: "{{ rds_db_cluster_id }}"
    db_instance_identifier: "{{ rds_db_instance_id }}"
    db_instance_class: "{{ rds_db_instance_class }}"
    availability_zone: "{{ rds_db_availability_zone }}"
    db_subnet_group_name: "{{ rds_db_subnet_group }}"
    db_parameter_group_name: "{{ rds_db_parameter_group }}"
    publicly_accessible: no
    copy_tags_to_snapshot: yes
    engine: "{{ rds_db_engine }}"
    region: "{{ aws_region }}"
```

# Amazon Aurora の停止
※ 実行は `--tags` で `stop` を指定

```YAML:roles/aws/rds/tasks/rds_stop.yml
---
- import_tasks: dependency_install.yml

- name: Delete DB instance.
  community.aws.rds_instance:
    db_instance_identifier: "{{ rds_db_instance_id }}"
    state: absent
    skip_final_snapshot: yes
    region: "{{ aws_region }}"

- name: Get Aurora DB cluster information.
  ansible.builtin.command: >
    aws rds describe-db-clusters
    --query 'DBClusters[?DBClusterIdentifier == `{{ rds_db_cluster_id }}`]'
    --output json
    --region {{ aws_region }}
  register: rds_db_cluster

- block:
    - name: Get Aurora DB cluster snapshot information.
      community.aws.rds_snapshot_info:
        db_cluster_snapshot_identifier: "{{ rds_snapshot_id }}"
        region: "{{ aws_region }}"
      register: rds_db_cluster_snapshot

    - block:
        - name: Delete Aurora DB cluster snapshot.
          ansible.builtin.command: >
            aws rds delete-db-cluster-snapshot
            --db-cluster-snapshot-identifier {{ rds_snapshot_id }}
            --region {{ aws_region }}

        - name: Wait for Aurora DB cluster snapshot to be deleted.
          ansible.builtin.command: >
            aws rds wait db-cluster-snapshot-deleted
            --db-cluster-snapshot-identifier {{ rds_snapshot_id }}
            --region {{ aws_region }}
      when: rds_db_cluster_snapshot | length > 0

    - name: Create Aurora DB cluster snapshot.
      ansible.builtin.command: >
        aws rds create-db-cluster-snapshot
        --db-cluster-identifier {{ rds_db_cluster_id }}
        --db-cluster-snapshot-identifier {{ rds_snapshot_id }}
        --region {{ aws_region }}

    - name: Wait for Aurora DB cluster snapshot to be available.
      ansible.builtin.command: >
        aws rds wait db-cluster-snapshot-available
        --db-cluster-snapshot-identifier {{ rds_snapshot_id }}
        --region {{ aws_region }}

    - name: Delete Aurora DB cluster.
      ansible.builtin.command: >
        aws rds delete-db-cluster
        --db-cluster-identifier {{ rds_db_cluster_id }}
        --skip-final-snapshot
        --region {{ aws_region }}

    - name: Wait for Aurora DB cluster to be deleted.
      ansible.builtin.command: >
        aws rds wait db-cluster-deleted
        --db-cluster-identifier {{ rds_db_cluster_id }}
        --region {{ aws_region }}
  when: rds_db_cluster.stdout | from_json | length > 0
```

# 参照
[AWS CLIを利用したRDSの起動停止スクリプト(検証環境用2017年1月版) | DevelopersIO](https://dev.classmethod.jp/articles/rds-oracle-aurora-start-stop-by-aws-cli/)