Skip to content

Commit

Permalink
Merge pull request #1070 from aligent/feature/MICRO-196-Slack-Alerts
Browse files Browse the repository at this point in the history
MICRO-196: Slack Alerts
  • Loading branch information
AdamJHall authored Sep 17, 2023
2 parents eea05b0 + 18a10f3 commit 1906669
Show file tree
Hide file tree
Showing 8 changed files with 2,038 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16.2.0
v18.17.0
1,968 changes: 1,956 additions & 12 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
]
},
"devDependencies": {
"@types/aws-lambda": "^8.10.121",
"@types/jest": "^26.0.10",
"@types/node": "16.9.4",
"@types/node": "^18.15.3",
"jest": "^26.4.2",
"source-map-support": "^0.5.19",
"ts-jest": "^26.5.6",
Expand All @@ -21,6 +22,7 @@
"esbuild": "^0.12.15"
},
"dependencies": {
"@aws-sdk/client-sns": "^3.413.0",
"aws-cdk-lib": "^2.90.0",
"constructs": "10.1.56"
},
Expand Down
5 changes: 4 additions & 1 deletion packages/graphql-mesh-server/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Prerender in Fargate
# GraphQL Mesh in Fargate
A construct host [GraphQL Mesh](https://the-guild.dev/graphql/mesh) server in Fargate.

## Deployment notifications
If notificationArn is set this construct creates a CodeStar notification rule, SNS topic and Lambda function to receive notifications for codepipeline executions and forward them to another SNS topic. This is so that you can setup AWS Chatbot either in this account OR another account and forward the notifications there.
## Props
- `vpc?`: VPC to attach Redis and Fargate instances to (default: create a vpc)
- `vpcName?`: If no VPC is provided create one with this name (default: 'graphql-server-vpc')
Expand All @@ -13,3 +15,4 @@ A construct host [GraphQL Mesh](https://the-guild.dev/graphql/mesh) server in Fa
- `memory?`: Amount of memory per Fargate instance (default: 1024)
- `redis?`: Redis instance to use for mesh caching
- `secrets?`: SSM values to pass through to the container as secrets
- `notificationArn?`: SNS Topic ARN to publish deployment notifications to
20 changes: 20 additions & 0 deletions packages/graphql-mesh-server/assets/notify-sns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { PublishCommand, SNSClient } from '@aws-sdk/client-sns';
import { SNSEvent } from 'aws-lambda';

const client = new SNSClient({ region: process.env.AWS_REGION });

export const handler = async (event: SNSEvent): Promise<void> => {
const record = event.Records[0];
const message = record.Sns.Message;

const command = new PublishCommand({
TopicArn: process.env.SNS_TOPIC,
Message: message,
});

try {
await client.send(command);
} catch (e) {
console.log(e);
}
};
5 changes: 5 additions & 0 deletions packages/graphql-mesh-server/lib/graphql-mesh-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export type MeshHostingProps = {
* SSM values to pass through to the container as secrets
*/
secrets?: {[key: string]: ssm.IStringParameter | ssm.IStringListParameter};
/**
* ARN of the SNS Topic to send deployment notifications to
*/
notificationArn?: string;
};

export class MeshHosting extends Construct {
Expand Down Expand Up @@ -90,6 +94,7 @@ export class MeshHosting extends Construct {
new CodePipelineService(this, 'pipeline', {
repository: this.repository,
service: this.service,
notificationArn: props.notificationArn
});
}
}
47 changes: 47 additions & 0 deletions packages/graphql-mesh-server/lib/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import { Construct } from 'constructs';
import * as fs from 'fs';
import * as path from 'path';
import * as YAML from 'yaml';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { Runtime } from 'aws-cdk-lib/aws-lambda';
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { Topic } from 'aws-cdk-lib/aws-sns';
import { LambdaSubscription } from 'aws-cdk-lib/aws-sns-subscriptions';
import { DetailType, NotificationRule } from 'aws-cdk-lib/aws-codestarnotifications';

export interface CodePipelineServiceProps {
/**
Expand All @@ -24,6 +30,11 @@ export interface CodePipelineServiceProps {
* Path to buildspec.yml (default: '../assets/buildspec.yml')
*/
buildspecPath?: string;

/**
* ARN of the SNS Topic to send deployment notifications to
*/
notificationArn?: string;
}

export class CodePipelineService extends Construct {
Expand Down Expand Up @@ -87,5 +98,41 @@ export class CodePipelineService extends Construct {
}),
],
});

if (props.notificationArn) {
const notifier = new NodejsFunction(this, 'NotifierLambda', {
entry: path.resolve(__dirname, '../assets/notify-sns.ts'),
description: 'Lambda function to forward SNS messages to another account.',
runtime: Runtime.NODEJS_18_X,
handler: 'index.handler',
timeout: Duration.seconds(10),
environment: {
SNS_TOPIC: props.notificationArn
}
});

notifier.addToRolePolicy(new PolicyStatement({
actions: ['sns:publish'],
resources: [props.notificationArn],
effect: Effect.ALLOW
}));

const topic = new Topic(this, 'NotifierTopic');
topic.addSubscription(new LambdaSubscription(notifier));

const rule = new NotificationRule(this, 'CodeStarNotificationRule', {
detailType: DetailType.FULL,
events: [
'codepipeline-pipeline-pipeline-execution-failed',
'codepipeline-pipeline-pipeline-execution-canceled',
'codepipeline-pipeline-pipeline-execution-started',
'codepipeline-pipeline-pipeline-execution-resumed',
'codepipeline-pipeline-pipeline-execution-succeeded',
'codepipeline-pipeline-pipeline-execution-superseded',
],
targets: [topic],
source: this.pipeline,
});
}
}
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"compilerOptions": {
"target": "ES2018",
"target": "ES2022",
"module": "commonjs",
"lib": ["es2018"],
"lib": ["es2022"],
"declaration": true,
"strict": true,
"noImplicitAny": true,
Expand Down

0 comments on commit 1906669

Please sign in to comment.