|
| 1 | +#!/bin/bash |
| 2 | +WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/fis.log") 2>&1 |
| 3 | +REGION=${AWS_DEFAULT_REGION:-$(aws configure get region 2>/dev/null)}; [ -z "$REGION" ] && echo "ERROR: No region" && exit 1; export AWS_DEFAULT_REGION="$REGION"; echo "Region: $REGION" |
| 4 | +RANDOM_ID=$(openssl rand -hex 4); ROLE_NAME="fis-tut-role-${RANDOM_ID}" |
| 5 | +handle_error() { echo "ERROR on line $1"; trap - ERR; cleanup; exit 1; }; trap 'handle_error $LINENO' ERR |
| 6 | +cleanup() { echo ""; echo "Cleaning up..."; [ -n "$TEMPLATE_ID" ] && aws fis delete-experiment-template --id "$TEMPLATE_ID" > /dev/null 2>&1 && echo " Deleted template"; aws iam delete-role-policy --role-name "$ROLE_NAME" --policy-name fis-policy 2>/dev/null; aws iam delete-role --role-name "$ROLE_NAME" 2>/dev/null && echo " Deleted role"; rm -rf "$WORK_DIR"; echo "Done."; } |
| 7 | +echo "Step 1: Creating IAM role" |
| 8 | +ROLE_ARN=$(aws iam create-role --role-name "$ROLE_NAME" --assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"fis.amazonaws.com"},"Action":"sts:AssumeRole"}]}' --query 'Role.Arn' --output text) |
| 9 | +aws iam put-role-policy --role-name "$ROLE_NAME" --policy-name fis-policy --policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["ec2:DescribeInstances","ec2:StopInstances","ec2:StartInstances"],"Resource":"*"}]}' |
| 10 | +echo " Role: $ROLE_ARN"; sleep 10 |
| 11 | +echo "Step 2: Listing available actions" |
| 12 | +aws fis list-actions --query 'actions[:5].{Id:id,Description:description}' --output table |
| 13 | +echo "Step 3: Creating experiment template" |
| 14 | +TEMPLATE_ID=$(aws fis create-experiment-template --description "Tutorial: stop EC2 instance" --role-arn "$ROLE_ARN" --stop-conditions '[{"source":"none"}]' --actions '{"stopInstances":{"actionId":"aws:ec2:stop-instances","parameters":{"startInstancesAfterDuration":"PT1M"},"targets":{"Instances":"tutorialInstances"}}}' --targets '{"tutorialInstances":{"resourceType":"aws:ec2:instance","selectionMode":"COUNT(1)","resourceTags":{"tutorial":"fis-test"}}}' --query 'experimentTemplate.id' --output text) |
| 15 | +echo " Template ID: $TEMPLATE_ID" |
| 16 | +echo "Step 4: Describing template" |
| 17 | +aws fis get-experiment-template --id "$TEMPLATE_ID" --query 'experimentTemplate.{Id:id,Description:description,Actions:actions|length(@),Targets:targets|length(@)}' --output table |
| 18 | +echo "Step 5: Listing templates" |
| 19 | +aws fis list-experiment-templates --query 'experimentTemplates[?starts_with(id, `EXT`)].{Id:id,Description:description}' --output table |
| 20 | +echo " (Not starting experiment — would require tagged EC2 instances)" |
| 21 | +echo ""; echo "Tutorial complete." |
| 22 | +echo "Do you want to clean up? (y/n): "; read -r CHOICE; [[ "$CHOICE" =~ ^[Yy]$ ]] && cleanup |
0 commit comments