From b59dec0fa81fe930c552874709e70054f0c0b218 Mon Sep 17 00:00:00 2001 From: yicr Date: Thu, 13 Mar 2025 18:43:26 +0900 Subject: [PATCH 1/3] feat: add machine logging option --- src/index.ts | 27 +++++++++++++++++++++++++++ test/stack.specific.test.ts | 5 ++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 359b2d3..c5995ea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,16 @@ import { Stack } from 'aws-cdk-lib'; import * as iam from 'aws-cdk-lib/aws-iam'; +import * as logs from 'aws-cdk-lib/aws-logs'; import * as scheduler from 'aws-cdk-lib/aws-scheduler'; import * as sns from 'aws-cdk-lib/aws-sns'; +import { LogLevel as EC2InstanceRunningScheduleStackMachineLogLevel } from 'aws-cdk-lib/aws-stepfunctions'; import { Construct } from 'constructs'; import { RunningControlStateMachine } from './resources/running-control-state-machine'; +export { + EC2InstanceRunningScheduleStackMachineLogLevel, +}; + export interface TargetResource { readonly tagKey: string; readonly tagValues: string[]; @@ -22,11 +28,16 @@ export interface Notifications { // readonly slack?: Slack; } +export interface LogOption { + readonly machineLogLevel?: EC2InstanceRunningScheduleStackMachineLogLevel; +} + export interface EC2InstanceRunningScheduleStackProps { readonly targetResource: TargetResource; readonly stopSchedule?: Schedule; readonly startSchedule?: Schedule; readonly notifications?: Notifications; + readonly logOption?: LogOption; } export class EC2InstanceRunningScheduleStack extends Stack { @@ -51,6 +62,22 @@ export class EC2InstanceRunningScheduleStack extends Stack { const machine = new RunningControlStateMachine(this, 'StateMachine', { stateMachineName: undefined, notificationTopic: topic, + logs: (() => { + if (props.logOption?.machineLogLevel) { + return { + destination: new logs.LogGroup(this, 'StateMachineLogGroup', { + logGroupName: (() => { + // if (names.stateMachineName) { + // return `/aws/states/${names.stateMachineName}`; + // } + return undefined; + })(), + }), + level: props.logOption.machineLogLevel, + }; + } + return undefined; + })(), }); // 👇 EventBridge Scheduler IAM Role diff --git a/test/stack.specific.test.ts b/test/stack.specific.test.ts index f6445a5..0faaaae 100644 --- a/test/stack.specific.test.ts +++ b/test/stack.specific.test.ts @@ -1,6 +1,6 @@ import { App } from 'aws-cdk-lib'; import { Template } from 'aws-cdk-lib/assertions'; -import { EC2InstanceRunningScheduleStack } from '../src'; +import { EC2InstanceRunningScheduleStack, EC2InstanceRunningScheduleStackMachineLogLevel } from '../src'; describe('Ec2InstanceRunningScheduleStack specific Testing', () => { @@ -29,6 +29,9 @@ describe('Ec2InstanceRunningScheduleStack specific Testing', () => { 'bar@example.net', ], }, + logOption: { + machineLogLevel: EC2InstanceRunningScheduleStackMachineLogLevel.ALL, + }, }); const template = Template.fromStack(stack); From c83f9a1027550afd36e0f414d9e09dd95bd55148 Mon Sep 17 00:00:00 2001 From: yicr Date: Thu, 13 Mar 2025 18:44:28 +0900 Subject: [PATCH 2/3] chore(test): update snapshots --- .../__snapshots__/stack.specific.test.ts.snap | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/__snapshots__/stack.specific.test.ts.snap b/test/__snapshots__/stack.specific.test.ts.snap index 2800b32..bc9d45b 100644 --- a/test/__snapshots__/stack.specific.test.ts.snap +++ b/test/__snapshots__/stack.specific.test.ts.snap @@ -223,6 +223,21 @@ exports[`Ec2InstanceRunningScheduleStack specific Testing Should match snapshot: ], ], }, + "LoggingConfiguration": { + "Destinations": [ + { + "CloudWatchLogsLogGroup": { + "LogGroupArn": { + "Fn::GetAtt": [ + "StateMachineLogGroup15B91BCB", + "Arn", + ], + }, + }, + }, + ], + "Level": "ALL", + }, "RoleArn": { "Fn::GetAtt": [ "StateMachineRoleB840431D", @@ -233,6 +248,14 @@ exports[`Ec2InstanceRunningScheduleStack specific Testing Should match snapshot: "Type": "AWS::StepFunctions::StateMachine", "UpdateReplacePolicy": "Delete", }, + "StateMachineLogGroup15B91BCB": { + "DeletionPolicy": "Retain", + "Properties": { + "RetentionInDays": 731, + }, + "Type": "AWS::Logs::LogGroup", + "UpdateReplacePolicy": "Retain", + }, "StateMachineRoleB840431D": { "Properties": { "AssumeRolePolicyDocument": { @@ -262,6 +285,20 @@ exports[`Ec2InstanceRunningScheduleStack specific Testing Should match snapshot: "Properties": { "PolicyDocument": { "Statement": [ + { + "Action": [ + "logs:CreateLogDelivery", + "logs:GetLogDelivery", + "logs:UpdateLogDelivery", + "logs:DeleteLogDelivery", + "logs:ListLogDeliveries", + "logs:PutResourcePolicy", + "logs:DescribeResourcePolicies", + "logs:DescribeLogGroups", + ], + "Effect": "Allow", + "Resource": "*", + }, { "Action": "tag:GetResources", "Effect": "Allow", From 60cf2e00391762bae59270258d506d23b4f6b195 Mon Sep 17 00:00:00 2001 From: yicr Date: Thu, 13 Mar 2025 18:44:41 +0900 Subject: [PATCH 3/3] chore(docs): update auto generated api document --- API.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/API.md b/API.md index fc3f559..e665190 100644 --- a/API.md +++ b/API.md @@ -894,6 +894,7 @@ const eC2InstanceRunningScheduleStackProps: EC2InstanceRunningScheduleStackProps | **Name** | **Type** | **Description** | | --- | --- | --- | | targetResource | TargetResource | *No description.* | +| logOption | LogOption | *No description.* | | notifications | Notifications | *No description.* | | startSchedule | Schedule | *No description.* | | stopSchedule | Schedule | *No description.* | @@ -910,6 +911,16 @@ public readonly targetResource: TargetResource; --- +##### `logOption`Optional + +```typescript +public readonly logOption: LogOption; +``` + +- *Type:* LogOption + +--- + ##### `notifications`Optional ```typescript @@ -940,6 +951,34 @@ public readonly stopSchedule: Schedule; --- +### LogOption + +#### Initializer + +```typescript +import { LogOption } from '@gammarers/aws-ec2-instance-running-schedule-stack' + +const logOption: LogOption = { ... } +``` + +#### Properties + +| **Name** | **Type** | **Description** | +| --- | --- | --- | +| machineLogLevel | aws-cdk-lib.aws_stepfunctions.LogLevel | *No description.* | + +--- + +##### `machineLogLevel`Optional + +```typescript +public readonly machineLogLevel: LogLevel; +``` + +- *Type:* aws-cdk-lib.aws_stepfunctions.LogLevel + +--- + ### Notifications #### Initializer