Skip to content

Commit

Permalink
Merge pull request #2 from gammarers/feature/add-handling-message
Browse files Browse the repository at this point in the history
feat: add handling message
  • Loading branch information
yicr authored Jun 3, 2024
2 parents f583ca7 + c87c010 commit a72445a
Show file tree
Hide file tree
Showing 2 changed files with 349 additions and 19 deletions.
110 changes: 106 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import * as crypto from 'crypto';
import * as cdk from 'aws-cdk-lib';
import * as events from 'aws-cdk-lib/aws-events';
import * as targets from 'aws-cdk-lib/aws-events-targets';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as sns from 'aws-cdk-lib/aws-sns';
import * as subscriptions from 'aws-cdk-lib/aws-sns-subscriptions';
//import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
//import * as tasks from 'aws-cdk-lib/aws-stepfunctions-tasks';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
import * as tasks from 'aws-cdk-lib/aws-stepfunctions-tasks';
import { Construct } from 'constructs';

export interface NotificationsProperty {
Expand Down Expand Up @@ -34,7 +35,89 @@ export class LambdaFunctionInvokeErrorNotificationStack extends cdk.Stack {
topic.addSubscription(new subscriptions.EmailSubscription(email));
}

// EventBridgeルールの作成
// Prepare Message
const prepareMessage: sfn.Pass = new sfn.Pass(this, 'PrepareMessage', {
parameters: {
Subject: sfn.JsonPath.format('😵 [Failur] AWS Lambda Function Invocation Failur Notification [{}][{}]',
sfn.JsonPath.stringAt('$.account'),
sfn.JsonPath.stringAt('$.region'),
),
Message: sfn.JsonPath.format('Account : {}\nRegion : {}\nFunction : {}\nErrorMessage : {}\nTrace : \n{}',
sfn.JsonPath.stringAt('$.account'),
sfn.JsonPath.stringAt('$.region'),
sfn.JsonPath.stringAt('$.detail.requestContext.functionArn'),
sfn.JsonPath.stringAt('$.detail.responsePayload.errorMessage'),
sfn.JsonPath.stringAt('$.Prepare.Concatenated.Trace'),
),
},
resultPath: '$.Prepare.Sns.Topic',
});

const init: sfn.Pass = new sfn.Pass(this, 'Init', {
result: sfn.Result.fromString(''),
resultPath: '$.Prepare.Concatenated.Trace',
});

const traceLinces: sfn.Pass = new sfn.Pass(this, 'TraceLinces', {
parameters: {
Lines: sfn.JsonPath.stringAt('$.detail.responsePayload.trace'),
},
resultPath: '$.TempTrace',
});

init.next(traceLinces);

const getTraceLine = new sfn.Pass(this, 'GetTraceLine', {
parameters: {
Line: sfn.JsonPath.arrayGetItem(sfn.JsonPath.stringAt('$.TempTrace.Lines'), 0),
},
resultPath: '$.Temp.GetTrace',
});

const checkUntreatedTranceLinesExist: sfn.Choice = new sfn.Choice(this, 'CheckUntreatedTranceLinesExist')
.when(sfn.Condition.isPresent('$.TempTrace.Lines[0]'), getTraceLine)
.otherwise(prepareMessage);

traceLinces.next(checkUntreatedTranceLinesExist);

const concatenateValue: sfn.Pass = new sfn.Pass(this, 'Concatenate', {
parameters: {
Trace: sfn.JsonPath.format('{}{}\n', sfn.JsonPath.stringAt('$.Prepare.Concatenated.Trace'), sfn.JsonPath.stringAt('$.Temp.GetTrace.Line')),
},
resultPath: '$.Prepare.Concatenated',
});

getTraceLine.next(concatenateValue);

const untreatedTranceLines: sfn.Pass = new sfn.Pass(this, 'UntreatedTranceLines', {
parameters: {
Lines: sfn.JsonPath.stringAt('$.TempTrace.Lines[1:]'),
},
resultPath: '$.TempTrace',
});

concatenateValue.next(untreatedTranceLines);

untreatedTranceLines.next(checkUntreatedTranceLinesExist);

const sendNotification: tasks.SnsPublish = new tasks.SnsPublish(this, 'SendNotification', {
topic: topic,
inputPath: '$.Prepare.Sns.Topic',
subject: sfn.JsonPath.stringAt('$.Subject'),
message: sfn.TaskInput.fromJsonPathAt('$.Message'),
resultPath: '$.Result.Sns.Topic',
});

prepareMessage.next(sendNotification);

// Step Functions State Machine
const stateMachine: sfn.StateMachine = new sfn.StateMachine(this, 'StateMachine', {
stateMachineName: `lambda-func-invoke-error-notification-${random}-state-machine`,
timeout: cdk.Duration.minutes(5),
definitionBody: sfn.DefinitionBody.fromChainable(init),
});

// EventBridge Rule
new events.Rule(this, 'LambdaFunctionInvokeErrorCatchRule', {
ruleName: `lambda-func-invoke-error-catch-${random}-rule`,
eventPattern: {
Expand All @@ -49,7 +132,26 @@ export class LambdaFunctionInvokeErrorNotificationStack extends cdk.Stack {
},
},
targets: [
new targets.SnsTopic(topic),
new targets.SfnStateMachine(stateMachine, {
role: new iam.Role(this, 'StartExecMachineRole', {
roleName: `lambda-func-invoke-error-start-exec-machine-${random}-role`,
description: 'lambda func invoke error start exec machine (send notification).',
assumedBy: new iam.ServicePrincipal('events.amazonaws.com'),
inlinePolicies: {
'states-start-execution-policy': new iam.PolicyDocument({
statements: [
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'states:StartExecution',
],
resources: ['*'],
}),
],
}),
},
}),
}),
],
});
}
Expand Down
Loading

0 comments on commit a72445a

Please sign in to comment.