Tutorial: Test instance stop and start using AWS FIS
You can use AWS Fault Injection Service (AWS FIS) to test how your applications handle instance stop and
start. Use this tutorial to create an experiment template that uses the AWS FIS
aws:ec2:stop-instances
action to stop one instance and then a second
instance.
Prerequisites
To complete this tutorial, ensure that you do the following:
-
Launch two test EC2 instances in your account. After you launch your instances, note the IDs of both instances.
-
Create an IAM role that enables the AWS FIS service to perform the
aws:ec2:stop-instances
action on your behalf. For more information, see IAM roles for AWS FIS experiments. -
Ensure that you have access to AWS FIS. For more information, see AWS FIS policy examples.
Step 1: Create an experiment template
Create the experiment template using the AWS FIS console. In the template, you specify two actions that will run sequentially for three minutes each. The first action stops one of the test instances, which AWS FIS chooses randomly. The second action stops both test instances.
To create an experiment template
Open the AWS FIS console at https://console.aws.amazon.com/fis/
. -
In the navigation pane, choose Experiment templates.
-
Choose Create experiment template.
-
For Step 1, Specify template details, do the following:
-
For Description and name, enter a description for the template, such as
Amazon S3 Network Disrupt Connectivity
. -
Choose Next, and move to Step 2, Specify actions and targets.
-
-
For Actions, do the following:
-
Choose Add action.
-
Enter a name for the action. For example, enter
stopOneInstance
. -
For Action type, choose aws:ec2:stop-instances.
-
For Target keep the target that AWS FIS creates for you.
-
For Action parameters, Start instances after duration, specify 3 minutes (PT3M).
Choose Save.
-
-
For Targets, do the following:
-
Choose Edit for the target that AWS FIS automatically created for you in the previous step.
-
Replace the default name with a more descriptive name. For example, enter
oneRandomInstance
. -
Verify that Resource type is aws:ec2:instance.
-
For Target method, choose Resource IDs, and then choose the IDs of the two test instances.
-
For Selection mode, choose Count. For Number of resources, enter
1
. Choose Save.
-
-
Choose Add target and do the following:
-
Enter a name for the target. For example, enter
bothInstances
. -
For Resource type, choose aws:ec2:instance.
-
For Target method, choose Resource IDs, and then choose the IDs of the two test instances.
-
For Selection mode, choose All.
Choose Save.
-
-
From the Actions section, choose Add action. Do the following:
-
For Name, enter a name for the action. For example, enter
stopBothInstances
. -
For Action type, choose aws:ec2:stop-instances.
For Start after, choose the first action that you added (
stopOneInstance
).-
For Target, choose the second target that you added (
bothInstances
). -
For Action parameters, Start instances after duration, specify 3 minutes (PT3M).
Choose Save.
-
-
Choose Next to move to Step 3, Configure service access.
-
For Service Access, choose Use an existing IAM role, and then choose the IAM role that you created as described in the prerequisites for this tutorial. If your role is not displayed, verify that it has the required trust relationship. For more information, see IAM roles for AWS FIS experiments.
-
Choose Next to move to Step 4, Configure optional settings.
-
(Optional) For Tags, choose Add new tag and specify a tag key and tag value. The tags that you add are applied to your experiment template, not the experiments that are run using the template.
-
Choose Next to move to Step 5, Review and create.
-
Review the template and choose Create experiment template. When prompted for confirmation, enter
create
, Then choose Create experiment template.
(Optional) To view the experiment template JSON
Choose the Export tab. The following is an example of the JSON created by the preceding console procedure.
{ "description": "Test instance stop and start", "targets": { "bothInstances": { "resourceType": "aws:ec2:instance", "resourceArns": [ "arn:aws:ec2:
region
:123456789012
:instance/instance_id_1
", "arn:aws:ec2:region
:123456789012
:instance/instance_id_2
" ], "selectionMode": "ALL" }, "oneRandomInstance": { "resourceType": "aws:ec2:instance", "resourceArns": [ "arn:aws:ec2:region
:123456789012
:instance/instance_id_1
", "arn:aws:ec2:region
:123456789012
:instance/instance_id_2
" ], "selectionMode": "COUNT(1)" } }, "actions": { "stopBothInstances": { "actionId": "aws:ec2:stop-instances", "parameters": { "startInstancesAfterDuration": "PT3M" }, "targets": { "Instances": "bothInstances" }, "startAfter": [ "stopOneInstance" ] }, "stopOneInstance": { "actionId": "aws:ec2:stop-instances", "parameters": { "startInstancesAfterDuration": "PT3M" }, "targets": { "Instances": "oneRandomInstance" } } }, "stopConditions": [ { "source": "none" } ], "roleArn": "arn:aws:iam::123456789012
:role/AllowFISEC2Actions
", "tags": {} }
Step 2: Start the experiment
When you have finished creating your experiment template, you can use it to start an experiment.
To start an experiment
-
You should be on the details page for the experiment template that you just created. Otherwise, choose Experiment templates and then select the ID of the experiment template to open the details page.
-
Choose Start experiment.
-
(Optional) To add a tag to your experiment, choose Add new tag and enter a tag key and a tag value.
-
Choose Start experiment. When prompted for confirmation, enter
start
and choose Start experiment.
Step 3: Track the experiment progress
You can track the progress of a running experiment until the experiment is completed, stopped, or failed.
To track the progress of an experiment
-
You should be on the details page for the experiment that you just started. Otherwise, choose Experiments and then select the ID of the experiment to open the details page.
-
To view the state of the experiment, check State in the Details pane. For more information, see experiment states.
-
When the state of the experiment is Running, go to the next step.
Step 4: Verify the experiment result
You can verify that the instances were stopped and started by the experiment as expected.
To verify the result of the experiment
-
Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/
in a new browser tab or window. This allows you to continue to track the progress of the experiment in the AWS FIS console while viewing the result of the experiment in the Amazon EC2 console. -
In the navigation pane, choose Instances.
-
When the state of the first action changes from Pending to Running (AWS FIS console), the state of one of the target instances changes from Running to Stopped (Amazon EC2 console).
-
After three minutes, the state of the first action changes to Completed, the state of the second action changes to Running, and the state of the other target instance changes to Stopped.
-
After three minutes, the state of the second action changes to Completed, the state of the target instances changes to Running, and the state of the experiment changes to Completed.
Step 5: Clean up
If you no longer need the test EC2 instances that you created for this experiment, you can terminate them.
To terminate the instances
-
Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/
. -
In the navigation pane, choose Instances.
-
Select both test instances and choose Instance state, Terminate instance.
-
When prompted for confirmation, choose Terminate.
If you no longer need the experiment template, you can delete it.
To delete an experiment template using the AWS FIS console
Open the AWS FIS console at https://console.aws.amazon.com/fis/
. -
In the navigation pane, choose Experiment templates.
-
Select the experiment template, and choose Actions, Delete experiment template.
-
When prompted for confirmation, enter
delete
and then choose Delete experiment template.