Skip to content

Commit

Permalink
[Step Function] 1. Set up skeleton code (#144)
Browse files Browse the repository at this point in the history
* [Step Function] 1. Set up skeleton code
  • Loading branch information
lym953 authored Jan 6, 2025
1 parent c77956b commit 6dceff1
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 5 deletions.
8 changes: 3 additions & 5 deletions serverless/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getConfigFromCfnMappings, getConfigFromCfnParams, validateParameters, Configuration } from "./lambda/env";
import { instrumentLambdas } from "./lambda/lambda";
import { InputEvent, OutputEvent, SUCCESS, FAILURE } from "./types";
import { instrumentStateMachines } from "./step_function/step_function";
import log from "loglevel";

export const handler = async (event: InputEvent, _: any): Promise<OutputEvent> => {
Expand Down Expand Up @@ -37,11 +38,8 @@ export const handler = async (event: InputEvent, _: any): Promise<OutputEvent> =
return lambdaOutput;
}

return {
requestId: event.requestId,
status: SUCCESS,
fragment,
};
const stepFunctionOutput = await instrumentStateMachines(event);
return stepFunctionOutput;
} catch (error: any) {
return {
requestId: event.requestId,
Expand Down
43 changes: 43 additions & 0 deletions serverless/src/step_function/step_function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { InputEvent, OutputEvent, SUCCESS, Resources } from "../types";
import log from "loglevel";
import { StateMachine, StateMachineProperties } from "step_function/types";

const STATE_MACHINE_RESOURCE_TYPE = "AWS::StepFunctions::StateMachine";

export async function instrumentStateMachines(event: InputEvent): Promise<OutputEvent> {
const fragment = event.fragment;
const resources = fragment.Resources;

const stateMachines = findStateMachines(resources);
for (const stateMachine of stateMachines) {
instrumentStateMachine(resources, stateMachine);
}

return {
requestId: event.requestId,
status: SUCCESS,
fragment,
};
}

function instrumentStateMachine(resources: Resources, stateMachine: StateMachine): void {
log.debug(`Instrumenting State Machine ${stateMachine.resourceKey}`);
}

export function findStateMachines(resources: Resources): StateMachine[] {
return Object.entries(resources)
.map(([key, resource]) => {
if (resource.Type !== STATE_MACHINE_RESOURCE_TYPE) {
log.debug(`Resource ${key} is not a State Machine, skipping...`);
return;
}

const properties: StateMachineProperties = resource.Properties;

return {
properties: properties,
resourceKey: key,
} as StateMachine;
})
.filter((resource) => resource !== undefined) as StateMachine[];
}
33 changes: 33 additions & 0 deletions serverless/src/step_function/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Parsed state machine info from CloudFormation template. For internal use.
// Not aimed to match any CloudFormation type.
export interface StateMachine {
properties: StateMachineProperties;
resourceKey: string;
}

// Necessary fields from AWS::StepFunctions::StateMachine's Properties field
export interface StateMachineProperties {
LoggingConfiguration?: LoggingConfiguration;
RoleArn?: string | { [key: string]: any };
}

// Matches AWS::StepFunctions::StateMachine LoggingConfiguration
export interface LoggingConfiguration {
Destinations?: LogDestination[];
IncludeExecutionData?: boolean;
Level?: string;
}

// Matches AWS::StepFunctions::StateMachine LogDestination
export interface LogDestination {
CloudWatchLogsLogGroup: CloudWatchLogsLogGroup;
}

// Matches AWS::StepFunctions::StateMachine CloudWatchLogsLogGroup
export interface CloudWatchLogsLogGroup {
LogGroupArn:
| string
| {
"Fn::GetAtt": string[];
};
}
41 changes: 41 additions & 0 deletions serverless/test/step_function/step_function.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { findStateMachines } from "../../src/step_function/step_function";

describe("findStateMachines", () => {
it("returns an empty array when no state machines are present", () => {
const resources = {
SomeOtherResource: {
Type: "AWS::Lambda::Function",
Properties: {},
},
};

const result = findStateMachines(resources);
expect(result).toEqual([]);
});

it("returns an array with state machines when they are present", () => {
const resources = {
FirstStateMachine: {
Type: "AWS::StepFunctions::StateMachine",
Properties: {
DefinitionUri: "state_machine/first.asl.json",
},
},
SecondStateMachine: {
Type: "AWS::StepFunctions::StateMachine",
Properties: {
DefinitionUri: "state_machine/second.asl.json",
},
},
SomeOtherResource: {
Type: "AWS::Lambda::Function",
Properties: {},
},
};

const result = findStateMachines(resources);
expect(result).toHaveLength(2);
expect(result[0].resourceKey).toBe("FirstStateMachine");
expect(result[1].resourceKey).toBe("SecondStateMachine");
});
});

0 comments on commit 6dceff1

Please sign in to comment.