Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (Change log group references to ILogGroup, don't create a second log group in aws-*-step-function) #211

Merged
merged 5 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .viperlightignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ source/patterns/@aws-solutions-constructs/aws-apigateway-lambda/test/integ.exist
source/patterns/@aws-solutions-constructs/aws-cloudfront-apigateway/test/integ.no-arguments.expected.json:204
CODE_OF_CONDUCT.md:4
CONTRIBUTING.md:236
source/patterns/@aws-solutions-constructs/core/test/step-function-helper.test.ts:118
source/patterns/@aws-solutions-constructs/core/test/step-function-helper.test.ts:114
source/patterns/@aws-solutions-constructs/core/test/step-function-helper.test.ts:124
source/patterns/@aws-solutions-constructs/aws-kinesisstreams-gluejob/test/test.kinesisstream-gluejob.test.ts:126
source/patterns/@aws-solutions-constructs/aws-lambda-sqs/test/integ.deployFunction.expected.json:112
source/patterns/@aws-solutions-constructs/aws-lambda-sqs/test/integ.existingFunction.expected.json:112
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ _Parameters_
|:-------------|:----------------|-----------------|
|eventsRule|[`events.Rule`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-events.Rule.html)|Returns an instance of events.Rule created by the construct|
|stateMachine|[`sfn.StateMachine`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-stepfunctions.StateMachine.html)|Returns an instance of sfn.StateMachine created by the construct|
|stateMachineLogGroup|[`logs.LogGroup`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-logs.LogGroup.html)|Returns an instance of the LogGroup created by the construct for StateMachine|
|stateMachineLogGroup|[`logs.ILogGroup`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-logs.ILogGroup.html)|Returns an instance of the ILogGroup created by the construct for StateMachine|
|cloudwatchAlarms?|[`cloudwatch.Alarm[]`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cloudwatch.Alarm.html)|Returns a list of cloudwatch.Alarm created by the construct|

## Default settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface EventsRuleToStepFunctionProps {

export class EventsRuleToStepFunction extends Construct {
public readonly stateMachine: sfn.StateMachine;
public readonly stateMachineLogGroup: logs.LogGroup;
public readonly stateMachineLogGroup: logs.ILogGroup;
public readonly eventsRule: events.Rule;
public readonly cloudwatchAlarms?: cloudwatch.Alarm[];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ _Parameters_
|:-------------|:----------------|-----------------|
|lambdaFunction|[`lambda.Function`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.Function.html)|Returns an instance of the Lambda function created by the pattern.|
|stateMachine|[`sfn.StateMachine`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-stepfunctions.StateMachine.html)|Returns an instance of StateMachine created by the construct.|
|stateMachineLogGroup|[`logs.LogGroup`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-logs.LogGroup.html)|Returns an instance of the LogGroup created by the construct for StateMachine|
|stateMachineLogGroup|[`logs.ILogGroup`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-logs.ILogGroup.html)|Returns an instance of the ILogGroup created by the construct for StateMachine|
|cloudwatchAlarms?|[`cloudwatch.Alarm[]`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cloudwatch.Alarm.html)|Returns a list of alarms created by the construct.

## Default settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface LambdaToStepFunctionProps {
export class LambdaToStepFunction extends Construct {
public readonly lambdaFunction: lambda.Function;
public readonly stateMachine: sfn.StateMachine;
public readonly stateMachineLogGroup: logs.LogGroup;
public readonly stateMachineLogGroup: logs.ILogGroup;
public readonly cloudwatchAlarms?: cloudwatch.Alarm[];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ _Parameters_
| **Name** | **Type** | **Description** |
|:-------------|:----------------|-----------------|
|stateMachine|[`sfn.StateMachine`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-stepfunctions.StateMachine.html)|Returns an instance of sfn.StateMachine created by the construct|
|stateMachineLogGroup|[`logs.LogGroup`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-logs.LogGroup.html)|Returns an instance of the LogGroup created by the construct for StateMachine|
|stateMachineLogGroup|[`logs.ILogGroup`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-logs.ILogGroup.html)|Returns an instance of the ILogGroup created by the construct for StateMachine|
|cloudwatchAlarms?|[`cloudwatch.Alarm[]`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cloudwatch.Alarm.html)|Returns a list of cloudwatch.Alarm created by the construct|
|s3Bucket?|[`s3.Bucket`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html)|Returns an instance of the s3.Bucket created by the construct|
|s3LoggingBucket?|[`s3.Bucket`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html)|Returns an instance of s3.Bucket created by the construct as the logging bucket for the primary bucket.|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export interface S3ToStepFunctionProps {

export class S3ToStepFunction extends Construct {
public readonly stateMachine: sfn.StateMachine;
public readonly stateMachineLogGroup: logs.LogGroup;
public readonly stateMachineLogGroup: logs.ILogGroup;
public readonly s3Bucket?: s3.Bucket;
public readonly s3LoggingBucket?: s3.Bucket;
public readonly cloudwatchAlarms?: cloudwatch.Alarm[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
*/

import * as sfn from '@aws-cdk/aws-stepfunctions';
import { LogGroup } from '@aws-cdk/aws-logs';
import { ILogGroup } from '@aws-cdk/aws-logs';

export function DefaultStateMachineProps(_logGroup: LogGroup): sfn.StateMachineProps | any {
export function DefaultStateMachineProps(_logGroup: ILogGroup): sfn.StateMachineProps | any {

const stateMachineProps: sfn.StateMachineProps | any = {
logs: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,45 @@ import { buildLogGroup } from './cloudwatch-log-group-helper';
* @param stateMachineProps - user-specified properties to override the default properties.
*/
export function buildStateMachine(scope: cdk.Construct, stateMachineProps: sfn.StateMachineProps,
logGroupProps?: logs.LogGroupProps): [sfn.StateMachine, logs.LogGroup] {
logGroupProps?: logs.LogGroupProps): [sfn.StateMachine, logs.ILogGroup] {

let logGroup: logs.ILogGroup;
let _smProps;

// If they sent a logGroup in stateMachineProps
if (stateMachineProps.logs?.destination) {
logGroup = stateMachineProps.logs?.destination;
_smProps = stateMachineProps;
} else {
// Three possibilities
// 1) logGroupProps not provided - create logGroupProps with just logGroupName
// 2) logGroupProps provided with no logGroupName - override logGroupProps.logGroupName
// 3) logGroupProps provided with logGroupName - pass unaltered logGroupProps
let consolidatedLogGroupProps = logGroupProps;

if (!consolidatedLogGroupProps) {
consolidatedLogGroupProps = {};
}
if (!consolidatedLogGroupProps?.logGroupName) {
const logGroupPrefix = '/aws/vendedlogs/states/';
const maxResourceNameLength = 255 - logGroupPrefix.length;
const nameParts: string[] = [
cdk.Stack.of(scope).stackName, // Name of the stack
scope.node.id, // Construct ID
'StateMachineLog' // Literal string for log group name portion
];

const logGroupName = logGroupPrefix + generateResourceName(nameParts, maxResourceNameLength);
consolidatedLogGroupProps = overrideProps(consolidatedLogGroupProps, { logGroupName });
}

let consolidatedLogGroupProps = logGroupProps;
// Create new Cloudwatch log group for Step function State Machine
logGroup = buildLogGroup(scope, 'StateMachineLogGroup', consolidatedLogGroupProps);

// Three possibilities
// 1) logGroupProps not provided - create logGroupProps with just logGroupName
// 2) logGroupProps provided with no logGroupName - override logGroupProps.logGroupName
// 3) logGroupProps provided with logGroupName - pass unaltered logGroupProps
if (!consolidatedLogGroupProps) {
consolidatedLogGroupProps = {};
}
if (!consolidatedLogGroupProps?.logGroupName) {
const logGroupPrefix = '/aws/vendedlogs/states/';
const maxResourceNameLength = 255 - logGroupPrefix.length;
const nameParts: string[] = [
cdk.Stack.of(scope).stackName, // Name of the stack
scope.node.id, // Construct ID
'StateMachineLog' // Literal string for log group name portion
];

const logGroupName = logGroupPrefix + generateResourceName(nameParts, maxResourceNameLength);
consolidatedLogGroupProps = overrideProps(consolidatedLogGroupProps, { logGroupName });
// Override the defaults with the user provided props
_smProps = overrideProps(smDefaults.DefaultStateMachineProps(logGroup), stateMachineProps);
}

// Configure Cloudwatch log group for Step function State Machine
const logGroup = buildLogGroup(scope, 'StateMachineLogGroup', consolidatedLogGroupProps);

// Override the defaults with the user provided props
const _smProps = overrideProps(smDefaults.DefaultStateMachineProps(logGroup), stateMachineProps);

// Override the Cloudwatch permissions to make it more fine grained
const _sm = new sfn.StateMachine(scope, 'StateMachine', _smProps);
const role = _sm.node.findChild('Role') as iam.Role;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import * as defaults from '../';
import { SynthUtils } from '@aws-cdk/assert';
import '@aws-cdk/assert/jest';
import * as sfn from '@aws-cdk/aws-stepfunctions';
import { LogGroup } from "@aws-cdk/aws-logs";
import { buildLogGroup } from '../lib/cloudwatch-log-group-helper';

// --------------------------------------------------------------
// Test minimal deployment with no properties
Expand Down Expand Up @@ -49,6 +49,8 @@ test('Test deployment w/ custom properties', () => {
stateMachineName: 'myStateMachine'
});
// Assertion
expect(stack).toCountResources("AWS::Logs::LogGroup", 1);

expect(stack).toHaveResource("AWS::StepFunctions::StateMachine", {
StateMachineName: "myStateMachine"
});
Expand All @@ -63,7 +65,9 @@ test('Test deployment w/ logging enabled', () => {
// Step function definition
const startState = new sfn.Pass(stack, 'StartState');
// Log group
const logGroup = new LogGroup(stack, 'myLogGroup', defaults.buildLogGroup(stack));
// const logGroup = new LogGroup(stack, 'myLogGroup', defaults.buildLogGroup(stack));
const logGroup = buildLogGroup(stack, 'StateMachineLogGroup');

// Build state machine
defaults.buildStateMachine(stack, {
definition: startState,
Expand All @@ -73,13 +77,15 @@ test('Test deployment w/ logging enabled', () => {
}
});
// Assertion
expect(stack).toCountResources("AWS::Logs::LogGroup", 1);

expect(stack).toHaveResource("AWS::StepFunctions::StateMachine", {
LoggingConfiguration: {
Destinations: [{
CloudWatchLogsLogGroup: {
LogGroupArn: {
"Fn::GetAtt": [
"myLogGroup46524CAB",
"StateMachineLogGroup15B91BCB",
"Arn"
]
}
Expand All @@ -91,9 +97,9 @@ test('Test deployment w/ logging enabled', () => {
});

// --------------------------------------------------------------
// Check default Cloudwatch perissions
// Check default Cloudwatch permissions
// --------------------------------------------------------------
test('Test deployment w/ logging enabled', () => {
test('Check default Cloudwatch permissions', () => {
// Stack
const stack = new Stack();
// Step function definition
Expand Down