Skip to content

ExperimentTemplate

Source: src/AWS/FIS/ExperimentTemplate.ts

An AWS Fault Injection Service (FIS) experiment template — a reusable definition of a chaos-engineering experiment: the targets to disrupt, the fault actions to run against them, and the stop conditions that abort a runaway experiment.

Creating a template is free and does not disrupt any resources — faults are only injected when an experiment is explicitly started from the template.

Stop EC2 instances selected by tag

import * as AWS from "alchemy/AWS";
const role = yield* AWS.IAM.Role("FisRole", {
assumeRolePolicyDocument: {
Version: "2012-10-17",
Statement: [{
Effect: "Allow",
Principal: { Service: "fis.amazonaws.com" },
Action: ["sts:AssumeRole"],
}],
},
managedPolicyArns: [
"arn:aws:iam::aws:policy/service-role/AWSFaultInjectionSimulatorEC2Access",
],
});
const template = yield* AWS.FIS.ExperimentTemplate("StopInstances", {
description: "Stop one tagged instance for two minutes",
roleArn: role.roleArn,
targets: {
Instances: {
resourceType: "aws:ec2:instance",
resourceTags: { ChaosReady: "true" },
selectionMode: "COUNT(1)",
},
},
actions: {
StopInstances: {
actionId: "aws:ec2:stop-instances",
parameters: { startInstancesAfterDuration: "PT2M" },
targets: { Instances: "Instances" },
},
},
});

Stop condition backed by a CloudWatch alarm

const template = yield* AWS.FIS.ExperimentTemplate("GuardedExperiment", {
roleArn: role.roleArn,
targets: {
Instances: {
resourceType: "aws:ec2:instance",
resourceTags: { ChaosReady: "true" },
selectionMode: "ALL",
},
},
actions: {
StopInstances: {
actionId: "aws:ec2:stop-instances",
targets: { Instances: "Instances" },
},
},
stopConditions: [
{
source: "aws:cloudwatch:alarm",
value: alarmArn,
},
],
});

Wait action sequenced after a fault

const template = yield* AWS.FIS.ExperimentTemplate("SequencedExperiment", {
roleArn: role.roleArn,
actions: {
Wait: {
actionId: "aws:fis:wait",
parameters: { duration: "PT1M" },
},
WaitAgain: {
actionId: "aws:fis:wait",
parameters: { duration: "PT1M" },
startAfter: ["Wait"],
},
},
});