Runbook

Creating an AWS System Manager Automation Runbook for MySQL Failover

Accessing System Manager Automation Console

  1. Log into the AWS System Manager Automation console.
  2. Select “Create Runbook”.

Creating a Workflow

In the visual design experience, workflows are represented as graphical runbooks on the workspace. Use the visual design experience to define, configure, and review individual Runbook actions.

Defining Runbook Details

Next to the Design and Code toggle switch, select the pencil icon and enter a name for your Runbook. For this guide, enter “MySQL Failover Runbook”.

Adding Action: Describe Primary MySQL Node

  1. In the AWS API browser, type “Describe_PrimaryNode_Instance” in the search bar.
  2. Drag the “Amazon EC2 – DescribeInstances” Action into the workspace.
  3. For Step name, enter a value (e.g., “Describe_PrimaryNode_Instance”).
  4. Select the Inputs tab.
  5. Expand the Additional inputs dropdown and enter the Instance ID of the primary node.
  6. In the Outputs tab, select “Add an output” and enter “InstanceState” in the Name field.
  7. In the Selector field, enter $.Reservations[0].Instances[0].State.Name.
  8. In the Type dropdown, select “String”.

Create VPC

Adding Action: Branch on Instance State

  1. Drag the Branch Action from the Action browser and drop it below the Describe_PrimaryNode_Instance step.
  2. For Step name, enter a value (e.g., BranchOnPrimaryNodeState).
  3. To define branch logic: 4. Select the Branch state on the canvas. 5. Under Inputs and Choices, select the pencil icon to edit Rule #1. 6. Select Add conditions.

Create VPC

  1. In the Conditions dialog for Rule #1, select the step output GetInstanceState.InstanceState from the Variable dropdown.
  2. For Operator, choose is equal to.
  3. For Value, select String from the dropdown and enter stopped.
  4. Select Save conditions.
  5. Choose Add new choice rule.
  6. Select Add conditions for Rule #2.
  7. In the Conditions dialog for Rule #2, select the step output GetInstanceState.InstanceState from the Variable dropdown.
  8. For Operator, choose is equal to.
  9. For Value, select String from the dropdown and enter stopping.
  10. Select Save conditions.
  11. Choose Add new choice rule.
  12. For Rule #3, select Add conditions.
  13. In the Conditions dialog for Rule #3, select the step output GetInstanceState.InstanceState from the Variable dropdown.
  14. For Operator, choose is equal to.
  15. For Value, select String from the dropdown and enter running.
  16. For the Default Rule, select Go to end for the Default step.

Adding Action: Start MySQL on Secondary Node

  1. Drag the Run command on instances action to the Drag action here box under the condition {{ GetInstanceState.InstanceState }} == "stopped".
  2. Name the step StartMySQL_On_SecondaryNode.
  3. In the Inputs tab, enter AWS-RunShellScript for the Document Name parameter.
  4. In Parameters, under Commands, enter sudo systemctl start mysqld.
  5. For InstanceIds, select the secondary node’s instance ID from the dropdown. Note: You may copy and paste the secondary instance ID if it’s not listed in the dropdown.

Adding Action: Wait for Primary Node to Stop

  1. Drag the Wait on AWS resource action to the Drag action here box under the condition {{ GetInstanceState.InstanceState }} == "stopping".
  2. Name the step, e.g., WaitForInstanceStop_PrimaryNode.
  3. For the Service field, select Amazon EC2.
  4. For the API field, select DescribeInstances.
  5. For the Property selector field, enter $.Reservations[0].Instances[0].State.Name.
  6. For Desired values, enter [**stopped**].
  7. In the Configuration tab of the WaitForInstanceStop_PrimaryNode action, select StartMySQL_On_SecondaryNode from the Next step dropdown.

Adding Action: Stop Primary Node

  1. Drag the Change an instance state action to the Drag action here box under the condition {{ GetInstanceState.InstanceState }} == "running".
  2. Name the step StopInstance_PrimaryNode.
  3. Select the Inputs tab.
  4. Expand the Additional inputs dropdown and enter the primary node’s instance ID. Note: You may copy and paste the primary instance ID if it’s not listed in the dropdown.
  5. For Desired state, specify stopped.
  6. In the Configuration tab of the StopInstance_PrimaryNode action, select StartMySQL_On_SecondaryNode from the Next step dropdown.

Reviewing and Creating the Runbook

Review the completed runbook on the canvas and select Create runbook to save your practice runbook.

Create VPC