export async function handler()

in src/getTargetNode.ts [8:47]


export async function handler(event: StateMachineInput): Promise<AsgDiscoveryResponse> {
    const runningExecutionsPromise = totalRunningExecutions(event.stepFunctionArn)
    const runningExecutions = await runningExecutionsPromise

    if (runningExecutions !== 1) {
        console.log(`Expected to find one running execution (this one!) but there were ${runningExecutions}.`);
        return { skipRotation: true };
    }

    const eligibleASGs = (
      await getASGsByTag(event.autoScalingGroupDiscoveryTagKey, "true")
    ).map(asg => asg.AutoScalingGroupName);

    const eligibleInstances = (
      // TODO it would be nice to not need the Tags on the instances as well, but currently used in the ElasticsearchAdminSsmPolicy IAM policy in cloudformation.yaml
      await getInstancesByTag(event.autoScalingGroupDiscoveryTagKey, "true")
    ).filter(i => eligibleASGs.includes(i.autoScalingGroupName));

    // We can manually run rotation against a particular instance if needed
    if(event.targetInstanceId) {
        const targetInstance = eligibleInstances.find(i => i.id === event.targetInstanceId);
        if(!targetInstance){
            throw Error(
              `The specified 'targetInstanceId' doesn't belong to an ASG with the 
              '${event.autoScalingGroupDiscoveryTagKey}' Tag set to 'true', 
              or the instance itself doesn't have that tag set to true.`
            );
        }
        const asgName = targetInstance.autoScalingGroupName;
        const targetInstanceId = targetInstance.id;
        const elasticsearchClient = new Elasticsearch(targetInstanceId);
        const targetElasticSearchNode = await elasticsearchClient.getElasticsearchNode(targetInstance);
        console.log(`Instance ${targetInstanceId} (ASG: ${asgName}) specified as input. Moving on...`);
        return { asgName, targetElasticSearchNode, skipRotation: false };
    }

    console.log(`Found ${eligibleInstances.length} instances with tag ${event.autoScalingGroupDiscoveryTagKey}`);
    eligibleInstances.forEach(instance => {
        console.log(`${instance.id} (${instance.autoScalingGroupName}) launched at ${instance.launchTime.toISOString()}}`);
    });