Skip to content

Commit

Permalink
feat(aws-constructs-factories): add cloudwatch alarms to factory outp…
Browse files Browse the repository at this point in the history
…ut (#1201)

* Remove bind statement

* Add CW Alarms to State Machine Factory

* Update documentation
  • Loading branch information
biffgaut authored Sep 16, 2024
1 parent a50aedc commit 5e49430
Show file tree
Hide file tree
Showing 23 changed files with 635 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,16 @@ stateMachineFactory(id: string, props: StateMachineFactoryProps): StateMachineFa
|:-------------|:----------------|-----------------|
|stateMachineProps|[`sfn.StateMachineProps`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.StateMachineProps.html)|The CDK properties that define the state machine. This property is required and must include a definitionBody or definition (definition is deprecated)|
|logGroup?|[]`logs.LogGroup`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_logs.LogGroup.html)|An existing LogGroup to which the new state machine will write log entries. Default: none, the construct will create a new log group.|
|createCloudWatchAlarms?|boolean|Whether to create recommended CloudWatch alarms for the State Machine. Default: the alarms are created|
|cloudWatchAlarmsPrefix?|string|Creating multiple State Machines with one Factories construct will result in name collisions as the cloudwatch alarms originally had fixed resource ids. This value was added to avoid collisions while not making changes that would be destructive for existing stacks. Unless you are creating multiple State Machines using factories you can ignore it|

# StateMachineFactoryResponse

| **Name** | **Type** | **Description** |
|:-------------|:----------------|-----------------|
|stateMachineProps|[`sfn.StateMachineProps`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.StateMachineProps.html)||
|logGroup|[]`logs.LogGroupProps`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_logs.LogGroupProps.html)||
|cloudwatchAlarms?|[`cloudwatch.Alarm[]`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudwatch.Alarm.html)|The alarms created by the factory (ExecutionFailed, ExecutionThrottled, ExecutionAborted)|

# Default settings

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as sqs from 'aws-cdk-lib/aws-sqs';
import * as kms from 'aws-cdk-lib/aws-kms';
import * as logs from 'aws-cdk-lib/aws-logs';
import * as defaults from '@aws-solutions-constructs/core';
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';

export interface S3BucketFactoryProps {
readonly bucketProps?: s3.BucketProps | any,
Expand All @@ -44,6 +45,22 @@ export interface StateMachineFactoryProps {
* Default: none, the construct will create a new log group.
*/
readonly logGroupProps?: logs.LogGroupProps
/**
* Whether to create recommended CloudWatch alarms
*
* @default - Alarms are created
*/
readonly createCloudWatchAlarms?: boolean,
/**
* Creating multiple State Machines in 1 stack with constructs will
* result in name collisions as the cloudwatch alarms originally had fixed resource ids.
* This value was added to avoid collisions while not making changes that would be
* destructive for existing stacks. Unless you are creating multiple State Machines using constructs
* you can ignore it.
*
* @default - undefined
*/
readonly cloudWatchAlarmsPrefix?: string
}

// Create a response specifically for the interface to avoid coupling client with internal implementation
Expand All @@ -57,6 +74,10 @@ export interface StateMachineFactoryResponse {
* The log group that will receive log messages from the state maching
*/
readonly logGroup: logs.ILogGroup
/*
* The alarms created by the factory (ExecutionFailed, ExecutionThrottled, ExecutionAborted)
*/
readonly cloudwatchAlarms?: cloudwatch.Alarm[];
}

export interface SqsQueueFactoryProps {
Expand Down Expand Up @@ -135,10 +156,16 @@ export class ConstructsFactories extends Construct {
}

public stateMachineFactory(id: string, props: StateMachineFactoryProps): StateMachineFactoryResponse {
const buildStateMachineResponse = defaults.buildStateMachine(this, id, props.stateMachineProps, props.logGroupProps);
const buildStateMachineResponse = defaults.buildStateMachine(this, id, {
stateMachineProps: props.stateMachineProps,
logGroupProps: props.logGroupProps,
createCloudWatchAlarms: props.createCloudWatchAlarms,
cloudWatchAlarmsPrefix: props.cloudWatchAlarmsPrefix
});
return {
stateMachine: buildStateMachineResponse.stateMachine,
logGroup: buildStateMachineResponse.logGroup
logGroup: buildStateMachineResponse.logGroup,
cloudwatchAlarms: buildStateMachineResponse.cloudWatchAlarms
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"36.0.0"}
{"version":"36.0.25"}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "36.0.0",
"version": "36.0.25",
"files": {
"c7ecd4e8eaccb1e7f7416826559ce7fa1c58bb3a5fb8298e571ac6695d1d50b7": {
"c609c27e6e7fc30d831454d0ca67932fbbb0f7d66f09883e3fc1e4a0afe294b6": {
"source": {
"path": "facstp-defaults.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "c7ecd4e8eaccb1e7f7416826559ce7fa1c58bb3a5fb8298e571ac6695d1d50b7.json",
"objectKey": "c609c27e6e7fc30d831454d0ca67932fbbb0f7d66f09883e3fc1e4a0afe294b6.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,69 @@
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
"targetExecutionFailedAlarm97DD1630": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"AlarmDescription": "Alarm for the number of executions that failed exceeded the threshold of 1. ",
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"Dimensions": [
{
"Name": "StateMachineArn",
"Value": {
"Ref": "targetStateMachinetestsm59897C57"
}
}
],
"EvaluationPeriods": 1,
"MetricName": "ExecutionsFailed",
"Namespace": "AWS/States",
"Period": 300,
"Statistic": "Sum",
"Threshold": 1
}
},
"targetExecutionThrottledAlarmCAA85FD5": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"AlarmDescription": "Alarm for the number of executions that throttled exceeded the threshold of 1. ",
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"Dimensions": [
{
"Name": "StateMachineArn",
"Value": {
"Ref": "targetStateMachinetestsm59897C57"
}
}
],
"EvaluationPeriods": 1,
"MetricName": "ExecutionThrottled",
"Namespace": "AWS/States",
"Period": 300,
"Statistic": "Sum",
"Threshold": 1
}
},
"targetExecutionAbortedAlarm70BA06C6": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"AlarmDescription": "Alarm for the number of executions that aborted exceeded the threshold of 1. ",
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"Dimensions": [
{
"Name": "StateMachineArn",
"Value": {
"Ref": "targetStateMachinetestsm59897C57"
}
}
],
"EvaluationPeriods": 1,
"MetricName": "ExecutionsAborted",
"Namespace": "AWS/States",
"Period": 300,
"Statistic": "Maximum",
"Threshold": 1
}
},
"lambdatestServiceRoleF3BDB8FC": {
"Type": "AWS::IAM::Role",
"Properties": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "36.0.0",
"version": "36.0.25",
"files": {
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
"source": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "36.0.0",
"version": "36.0.25",
"testCases": {
"facstp-defaults/Integ/DefaultTest": {
"stacks": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "36.0.0",
"version": "36.0.24",
"artifacts": {
"facstpdefaultsIntegDefaultTestDeployAssertFD717592.assets": {
"type": "cdk:asset-manifest",
Expand Down Expand Up @@ -66,7 +66,7 @@
"validateOnSynth": false,
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/c7ecd4e8eaccb1e7f7416826559ce7fa1c58bb3a5fb8298e571ac6695d1d50b7.json",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/c609c27e6e7fc30d831454d0ca67932fbbb0f7d66f09883e3fc1e4a0afe294b6.json",
"requiresBootstrapStackVersion": 6,
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
"additionalDependencies": [
Expand Down Expand Up @@ -106,6 +106,24 @@
"data": "targetStateMachinetestsm59897C57"
}
],
"/facstp-defaults/target/ExecutionFailedAlarm/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "targetExecutionFailedAlarm97DD1630"
}
],
"/facstp-defaults/target/ExecutionThrottledAlarm/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "targetExecutionThrottledAlarmCAA85FD5"
}
],
"/facstp-defaults/target/ExecutionAbortedAlarm/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "targetExecutionAbortedAlarm70BA06C6"
}
],
"/facstp-defaults/lambdatest/ServiceRole/Resource": [
{
"type": "aws:cdk:logicalId",
Expand Down
Loading

0 comments on commit 5e49430

Please sign in to comment.