0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Ansible で Amazon Aurora の起動・停止

Last updated at Posted at 2021-09-26

はじめに

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

ansible.cfg
[defaults]
interpreter_python = /usr/bin/python3

変数

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"

タスク定義

roles/aws/rds/tasks/main.yml
---
- import_tasks: rds_start.yml
  tags:
    - start

- import_tasks: rds_stop.yml
  tags:
    - stop

依存モジュールのインストール

roles/aws/rds/tasks/dependency_install.yml
---
- name: Install dependences.
  pip:
    name: "{{ dependencies }}"
    executable: "{{ pip_executable }}"

Amazon Aurora の起動

※ 実行は --tagsstart を指定

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 の停止

※ 実行は --tagsstop を指定

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

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?