Skip to content

Commit

Permalink
fix(StepFunctions): Address LogGroup behavior problems (#922)
Browse files Browse the repository at this point in the history
* Implementation

* Clean up some code cruft
  • Loading branch information
biffgaut authored Mar 17, 2023
1 parent 7321f86 commit 84e581c
Show file tree
Hide file tree
Showing 33 changed files with 302 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Duration } from 'aws-cdk-lib';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
import '@aws-cdk/assert/jest';
import * as cdk from 'aws-cdk-lib';
import { Template } from 'aws-cdk-lib/assertions';

function deployNewStateMachine(stack: cdk.Stack) {

Expand Down Expand Up @@ -192,4 +193,28 @@ test('check custom event bus resource with props when deploy:true', () => {
expect(stack).toHaveResource('AWS::Events::EventBus', {
Name: 'testcustomeventbus'
});
});
});

test('check LogGroup name', () => {
const stack = new cdk.Stack();

deployNewStateMachine(stack);

// Perform some fancy stuff to examine the specifics of the LogGroupName
const expectedPrefix = '/aws/vendedlogs/states/constructs/';
const lengthOfDatetimeSuffix = 13;

const LogGroup = Template.fromStack(stack).findResources("AWS::Logs::LogGroup");

const logName = LogGroup.testeventbridgestepfunctionsStateMachineLogGroup826A5B74.Properties.LogGroupName;
const suffix = logName.slice(-lengthOfDatetimeSuffix);

// Look for the expected Prefix and the 13 digit time suffix
expect(logName.slice(0, expectedPrefix.length)).toEqual(expectedPrefix);
expect(IsWholeNumber(suffix)).not.toBe(false);
});

function IsWholeNumber(target: string): boolean {
const numberPattern = /[0-9]{13}/;
return target.match(numberPattern) !== null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
"testeventbridgestepfunctionsneweventbusconstructStateMachineLogGroup6DC6AD59": {
"Type": "AWS::Logs::LogGroup",
"Properties": {
"LogGroupName": "/aws/vendedlogs/states/eventbridgestepfunctionsexistingeventbustesteventbridgestepfunctionsneweventbusconstructstatemachinelogac9442d7e2fa"
"LogGroupName": "integ-test-existing-eventbus"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ const props: EventbridgeToStepfunctionsProps = {
},
existingEventBusInterface: existingEventBus,
logGroupProps: {
removalPolicy: RemovalPolicy.DESTROY
removalPolicy: RemovalPolicy.DESTROY,
logGroupName: "integ-test-existing-eventbus"
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"testeventbridgestepfunctionsneweventbusconstructStateMachineLogGroup6DC6AD59": {
"Type": "AWS::Logs::LogGroup",
"Properties": {
"LogGroupName": "/aws/vendedlogs/states/eventbridgestepfunctionsneweventbustesteventbridgestepfunctionsneweventbusconstructstatemachinelog651032919fdf"
"LogGroupName": "integ-test-new-eventbus"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ const props: EventbridgeToStepfunctionsProps = {
},
eventBusProps: { eventBusName: 'test' },
logGroupProps: {
removalPolicy: RemovalPolicy.DESTROY
removalPolicy: RemovalPolicy.DESTROY,
logGroupName: "integ-test-new-eventbus"
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"testeventbridgestepfunctionsconstructStateMachineLogGroup3098B32C": {
"Type": "AWS::Logs::LogGroup",
"Properties": {
"LogGroupName": "/aws/vendedlogs/states/eventbridgestepfunctionsnoargumenttesteventbridgestepfunctionsconstructstatemachinelog56559569213c"
"LogGroupName": "integ-test-no-arguments"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const props: EventbridgeToStepfunctionsProps = {
schedule: events.Schedule.rate(Duration.minutes(5))
},
logGroupProps: {
removalPolicy: RemovalPolicy.DESTROY
removalPolicy: RemovalPolicy.DESTROY,
logGroupName: "integ-test-no-arguments"
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"testeventbridgestepfunctionsandlambdaconstructStateMachineLogGroup7C2D036A": {
"Type": "AWS::Logs::LogGroup",
"Properties": {
"LogGroupName": "/aws/vendedlogs/states/eventbridgestepfunctionswithlambdatesteventbridgestepfunctionsandlambdaconstructstatemachinelog1db1bc901e42"
"LogGroupName": "with-lambda"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ const props: EventbridgeToStepfunctionsProps = {
schedule: events.Schedule.rate(Duration.minutes(5))
},
logGroupProps: {
removalPolicy: RemovalPolicy.DESTROY
removalPolicy: RemovalPolicy.DESTROY,
logGroupName: "with-lambda"
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { FargateToStepfunctions } from "../lib";
import * as stepfunctions from 'aws-cdk-lib/aws-stepfunctions';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { Template } from 'aws-cdk-lib/assertions';

const clusterName = "custom-cluster-name";
const containerName = "custom-container-name";
Expand Down Expand Up @@ -311,4 +312,29 @@ function testStateMachineProps(stack: cdk.Stack, userProps?: stepfunctions.State
const defaultTestProp = { definition: new stepfunctions.Pass(stack, 'StartState') };

return defaults.consolidateProps(defaultTestProp, userProps);
}
}

test('check LogGroup name', () => {
const stack = new cdk.Stack();
const publicApi = true;

createFargateConstructWithNewResources(stack, publicApi);

// Perform some fancy stuff to examine the specifics of the LogGroupName
const expectedPrefix = '/aws/vendedlogs/states/constructs/';
const lengthOfDatetimeSuffix = 13;

const LogGroup = Template.fromStack(stack).findResources("AWS::Logs::LogGroup");

const logName = LogGroup.testconstructStateMachineLogGroup2EB4F48B.Properties.LogGroupName;
const suffix = logName.slice(-lengthOfDatetimeSuffix);

// Look for the expected Prefix and the 13 digit time suffix
expect(logName.slice(0, expectedPrefix.length)).toEqual(expectedPrefix);
expect(IsWholeNumber(suffix)).not.toBe(false);
});

function IsWholeNumber(target: string): boolean {
const numberPattern = /[0-9]{13}/;
return target.match(numberPattern) !== null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@
"testconstructStateMachineLogGroup2EB4F48B": {
"Type": "AWS::Logs::LogGroup",
"Properties": {
"LogGroupName": "/aws/vendedlogs/states/newresourcestestconstructstatemachinelog63b3cb15f80b"
"LogGroupName": "with-lambda"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ const constructProps: FargateToStepfunctionsProps = {
existingFargateServiceObject: createFargateServiceResponse.service,
stateMachineEnvironmentVariableName: 'CUSTOM_NAME',
logGroupProps: {
removalPolicy: RemovalPolicy.DESTROY
removalPolicy: RemovalPolicy.DESTROY,
logGroupName: "with-lambda"
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@
"testconstructStateMachineLogGroup2EB4F48B": {
"Type": "AWS::Logs::LogGroup",
"Properties": {
"LogGroupName": "/aws/vendedlogs/states/nocloudwatchalarmstestconstructstatemachinelogdbb9902b27ea"
"LogGroupName": "with-lambda"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ const constructProps: FargateToStepfunctionsProps = {
existingFargateServiceObject: createFargateServiceResponse.service,
createCloudWatchAlarms: false,
logGroupProps: {
removalPolicy: RemovalPolicy.DESTROY
removalPolicy: RemovalPolicy.DESTROY,
logGroupName: "with-lambda"
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"testlambdastepfunctionsconstructStateMachineLogGroup1FD4C0D4": {
"Type": "AWS::Logs::LogGroup",
"Properties": {
"LogGroupName": "/aws/vendedlogs/states/deploylambdatestlambdastepfunctionsconstructstatemachinelogb258d52dbe27"
"LogGroupName": "with-lambda"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ const props: LambdaToStepfunctionsProps = {
definition: startState
},
logGroupProps: {
removalPolicy: RemovalPolicy.DESTROY
},
removalPolicy: RemovalPolicy.DESTROY,
logGroupName: "with-lambda"
}
};

// Add the pattern
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"testlambdastepfunctionsStateMachineLogGroupD3F22A89": {
"Type": "AWS::Logs::LogGroup",
"Properties": {
"LogGroupName": "/aws/vendedlogs/states/deployfunctionwithvpctestlambdastepfunctionsstatemachinelog97398dca7a29"
"LogGroupName": "with-lambda"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ const props: LambdaToStepfunctionsProps = {
stateMachineProps: {
definition: startState
},
logGroupProps: {
removalPolicy: RemovalPolicy.DESTROY
},
deployVpc: true,
logGroupProps: {
removalPolicy: RemovalPolicy.DESTROY,
logGroupName: "with-lambda"
}
};

new LambdaToStepfunctions(stack, "test-lambda-stepfunctions", props);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
"testlambdastepfunctionsconstructStateMachineLogGroup1FD4C0D4": {
"Type": "AWS::Logs::LogGroup",
"Properties": {
"LogGroupName": "/aws/vendedlogs/states/existingfunctiontestlambdastepfunctionsconstructstatemachinelog9e0bca5b4cd6"
"LogGroupName": "with-lambda"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ const props: LambdaToStepfunctionsProps = {
definition: startState
},
logGroupProps: {
removalPolicy: RemovalPolicy.DESTROY
},
removalPolicy: RemovalPolicy.DESTROY,
logGroupName: "with-lambda"
}
};

// Add the pattern
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as stepfunctions from 'aws-cdk-lib/aws-stepfunctions';
import * as ec2 from "aws-cdk-lib/aws-ec2";
import { LambdaToStepfunctions } from '../lib';
import '@aws-cdk/assert/jest';
import { Template } from "aws-cdk-lib/assertions";

// --------------------------------------------------------------
// Test deployment with new Lambda function
Expand Down Expand Up @@ -461,4 +462,41 @@ test("Test bad call with existingVpc and deployVpc", () => {
};
// Assertion
expect(app).toThrowError();
});
});

test('check LogGroup name', () => {
// Stack
const stack = new Stack();
// Helper declaration
const startState = new stepfunctions.Pass(stack, 'StartState');
new LambdaToStepfunctions(stack, 'lambda-to-step-function-stack', {
lambdaFunctionProps: {
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'index.handler',
code: lambda.Code.fromAsset(`${__dirname}/lambda`),
environment: {
LAMBDA_NAME: 'existing-function'
}
},
stateMachineProps: {
definition: startState
}
});
// Perform some fancy stuff to examine the specifics of the LogGroupName
const expectedPrefix = '/aws/vendedlogs/states/constructs/';
const lengthOfDatetimeSuffix = 13;

const LogGroup = Template.fromStack(stack).findResources("AWS::Logs::LogGroup");

const logName = LogGroup.lambdatostepfunctionstackStateMachineLogGroupEAD4854E.Properties.LogGroupName;
const suffix = logName.slice(-lengthOfDatetimeSuffix);

// Look for the expected Prefix and the 13 digit time suffix
expect(logName.slice(0, expectedPrefix.length)).toEqual(expectedPrefix);
expect(IsWholeNumber(suffix)).not.toBe(false);
});

function IsWholeNumber(target: string): boolean {
const numberPattern = /[0-9]{13}/;
return target.match(numberPattern) !== null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
"tests3stepfunctionstests3stepfunctionseventrulestepfunctionconstructStateMachineLogGroupB4555776": {
"Type": "AWS::Logs::LogGroup",
"Properties": {
"LogGroupName": "/aws/vendedlogs/states/customloggingbuckettests3stepfunctionseventrulestepfunctionconstructstatemachineloga4e9bc58c9e9"
"LogGroupName": "with-lambda"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ new S3ToStepfunctions(stack, 'test-s3-stepfunctions', {
versioned: true
},
logGroupProps: {
removalPolicy: RemovalPolicy.DESTROY
removalPolicy: RemovalPolicy.DESTROY,
logGroupName: "with-lambda"
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
"tests3stepfunctionspreexistingbucketconstructtests3stepfunctionspreexistingbucketconstructeventrulestepfunctionconstructStateMachineLogGroup9D5E3E4D": {
"Type": "AWS::Logs::LogGroup",
"Properties": {
"LogGroupName": "/aws/vendedlogs/states/preexistingbuckettests3stepfunctionspreexistingbucketconstructeventrulestepfunctstatemachineloga29d0790019e"
"LogGroupName": "with-lambda"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const props: S3ToStepfunctionsProps = {
definition: startState
},
logGroupProps: {
removalPolicy: RemovalPolicy.DESTROY
removalPolicy: RemovalPolicy.DESTROY,
logGroupName: "with-lambda"
},
logS3AccessLogs: false
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
"tests3stepfunctionsconstructtests3stepfunctionsconstructeventrulestepfunctionconstructStateMachineLogGroupE86C2CF5": {
"Type": "AWS::Logs::LogGroup",
"Properties": {
"LogGroupName": "/aws/vendedlogs/states/s3stepfunctionsnoargumenttests3stepfunctionsconstructeventrulestepfunctionconstructstatemachinelogf07752992857"
"LogGroupName": "with-lambda"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const props: S3ToStepfunctionsProps = {
removalPolicy: RemovalPolicy.DESTROY,
},
logGroupProps: {
removalPolicy: RemovalPolicy.DESTROY
removalPolicy: RemovalPolicy.DESTROY,
logGroupName: "with-lambda"
},
logS3AccessLogs: false
};
Expand Down
Loading

0 comments on commit 84e581c

Please sign in to comment.