Skip to content

Commit 1e754d7

Browse files
committed
Adding Repo
1 parent 862513f commit 1e754d7

9 files changed

Lines changed: 353 additions & 0 deletions
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Amazon RDS to Amazon SNS
2+
3+
RDS Event Subscriptions allow users to configure notifications for RDS Events (provided through an SNS topic). This template configures an event subscription for failure, low storage, and availability event categories for RDS Instances.
4+
5+
Learn more about this pattern at Serverless Land Patterns:https://serverlessland.com/patterns/rds-sns-event-notification-cdk-ts
6+
7+
Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
8+
9+
## Requirements
10+
11+
* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
12+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
13+
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
14+
* [AWS CDK](https://docs.aws.amazon.com/cdk/latest/guide/cli.html) installed and configured
15+
* [Create an RDS Instance and copy Name of RDS Instance somewhere in notes. You will need it during template deployment](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_CreateDBInstance.html#USER_CreateDBInstance.Creating)
16+
17+
## Deployment Instructions
18+
19+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
20+
```bash
21+
git clone https://github.com/aws-samples/serverless-patterns
22+
```
23+
2. Change directory to the pattern directory:
24+
```bash
25+
cd serverless-patterns/rds-sns-event-notification-cdk-ts
26+
```
27+
3. Install the required dependencies.
28+
```bash
29+
npm install
30+
```
31+
4. To deploy the application:
32+
- In SNSEndpoint, Provide your email address to receive notification from Amazon SNS
33+
- In RDSInstanceName, Provide name of RDS Instance you created during Requirements
34+
```bash
35+
cdk deploy --parameters SNSEndpoint=EmailID --parameters RDSInstanceName=RDSInstanceName
36+
```
37+
38+
## How it works
39+
40+
RDS Event Subscriptions allow users to configure notifications for RDS Events (provided through an SNS topic). This template configures an event subscription for failure, low storage, and availability event categories for RDS Instances.
41+
42+
## Testing
43+
44+
Once the CDK deployment is successful, first thing to do is to confirm the Email subscription. You will receive an email to confirm it. Then go to RDS console. Select the RDS Instance you have created. Stop the Instance and Restart it again. You will receive a notification related to it on your Email Address. Moving forward, you will receive failure, low storage, and availability events that happen on your RDS Instance.
45+
46+
## Cleanup
47+
48+
* Delete the stack
49+
```bash
50+
cdk destroy
51+
```
52+
----
53+
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
54+
55+
SPDX-License-Identifier: MIT-0
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env node
2+
import 'source-map-support/register';
3+
import * as cdk from 'aws-cdk-lib';
4+
import { RdsSnsEventNotificationCdkTsStack } from '../lib/rds-sns-event-notification-cdk-ts-stack';
5+
6+
const app = new cdk.App();
7+
new RdsSnsEventNotificationCdkTsStack(app, 'RdsSnsEventNotificationCdkTsStack', {
8+
/* If you don't specify 'env', this stack will be environment-agnostic.
9+
* Account/Region-dependent features and context lookups will not work,
10+
* but a single synthesized template can be deployed anywhere. */
11+
12+
/* Uncomment the next line to specialize this stack for the AWS Account
13+
* and Region that are implied by the current CLI configuration. */
14+
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
15+
16+
/* Uncomment the next line if you know exactly what Account and Region you
17+
* want to deploy the stack to. */
18+
// env: { account: '123456789012', region: 'us-east-1' },
19+
20+
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
21+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"app": "npx ts-node --prefer-ts-exts bin/rds-sns-event-notification-cdk-ts.ts",
3+
"watch": {
4+
"include": [
5+
"**"
6+
],
7+
"exclude": [
8+
"README.md",
9+
"cdk*.json",
10+
"**/*.d.ts",
11+
"**/*.js",
12+
"tsconfig.json",
13+
"package*.json",
14+
"yarn.lock",
15+
"node_modules",
16+
"test"
17+
]
18+
},
19+
"context": {
20+
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
21+
"@aws-cdk/core:checkSecretUsage": true,
22+
"@aws-cdk/core:target-partitions": [
23+
"aws",
24+
"aws-cn"
25+
],
26+
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
27+
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
28+
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
29+
"@aws-cdk/aws-iam:minimizePolicies": true,
30+
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
31+
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
32+
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
33+
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
34+
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
35+
"@aws-cdk/core:enablePartitionLiterals": true,
36+
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
37+
"@aws-cdk/aws-iam:standardizedServicePrincipals": true,
38+
"@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true,
39+
"@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true,
40+
"@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true,
41+
"@aws-cdk/aws-route53-patters:useCertificate": true,
42+
"@aws-cdk/customresources:installLatestAwsSdkDefault": false,
43+
"@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true,
44+
"@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true,
45+
"@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true,
46+
"@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true,
47+
"@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true,
48+
"@aws-cdk/aws-redshift:columnId": true,
49+
"@aws-cdk/aws-stepfunctions-tasks:enableEmrServicePolicyV2": true,
50+
"@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true,
51+
"@aws-cdk/aws-apigateway:requestValidatorUniqueId": true,
52+
"@aws-cdk/aws-kms:aliasNameRef": true,
53+
"@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true,
54+
"@aws-cdk/core:includePrefixInUniqueNameGeneration": true,
55+
"@aws-cdk/aws-efs:denyAnonymousAccess": true,
56+
"@aws-cdk/aws-opensearchservice:enableOpensearchMultiAzWithStandby": true,
57+
"@aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion": true,
58+
"@aws-cdk/aws-efs:mountTargetOrderInsensitiveLogicalId": true
59+
}
60+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"title": "Amazon RDS instance event notification to Amazon SNS",
3+
"description": "Amazon RDS event subscriptions allow users to configure notifications for RDS events (provided through an SNS topic).",
4+
"language": "Typescript",
5+
"level": "200",
6+
"framework": "CDK",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"Amazon RDS event subscriptions allow users to configure notifications for RDS events (provided through an SNS topic).",
11+
"This template configures an event subscription for failure, low storage, and availability event categories for RDS Instances."
12+
]
13+
},
14+
"gitHub": {
15+
"template": {
16+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/rds-sns-event-notification-cdk-ts",
17+
"templateURL": "serverless-patterns/rds-sns-event-notification-cdk-ts",
18+
"projectFolder": "rds-sns-event-notification-cdk-ts",
19+
"templateFile": "lib/rds-sns-event-notification-cdk-ts-stack.ts"
20+
}
21+
},
22+
"resources": {
23+
"bullets": [
24+
{
25+
"text": "Working with Amazon RDS event notification",
26+
"link": "https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_Events.html"
27+
},
28+
{
29+
"text": "Creating an Amazon RDS DB instance",
30+
"link": "https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_CreateDBInstance.html#USER_CreateDBInstance.Creating"
31+
}
32+
]
33+
},
34+
"deploy": {
35+
"text": [
36+
"cdk deploy --parameters SNSEndpoint=EmailID --parameters RDSInstanceName=RDSInstanceName"
37+
]
38+
},
39+
"testing": {
40+
"text": [
41+
"See the GitHub repo for detailed testing instructions."
42+
]
43+
},
44+
"cleanup": {
45+
"text": [
46+
"cdk destroy"
47+
]
48+
},
49+
"authors": [
50+
{
51+
"name": "Tejendra Khatri",
52+
"image": "https://drive.google.com/file/d/1FR9bL3aJz2YRYkIsmfR_zL3RvE65oR0b/view",
53+
"bio": "Cloud Support Engineer @ AWS",
54+
"linkedin": "https://www.linkedin.com/in/tejendrakhatri"
55+
}
56+
]
57+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
roots: ['<rootDir>/test'],
4+
testMatch: ['**/*.test.ts'],
5+
transform: {
6+
'^.+\\.tsx?$': 'ts-jest'
7+
}
8+
};
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import * as cdk from 'aws-cdk-lib';
2+
import * as sns from 'aws-cdk-lib/aws-sns';
3+
import * as subscriptions from 'aws-cdk-lib/aws-sns-subscriptions';
4+
import * as events from 'aws-cdk-lib/aws-events';
5+
import * as rds from 'aws-cdk-lib/aws-rds';
6+
import * as iam from 'aws-cdk-lib/aws-iam';
7+
8+
import { Construct } from 'constructs';
9+
10+
export class RdsSnsEventNotificationCdkTsStack extends cdk.Stack {
11+
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
12+
super(scope, id, props);
13+
// Parameters
14+
const snsEndpoint = new cdk.CfnParameter(this, 'SNSEndpoint', {
15+
type: 'String',
16+
description: 'Provide your email address to receive notification from SNS'
17+
});
18+
const rdsInstanceName = new cdk.CfnParameter(this, 'RDSInstanceName', {
19+
type: 'String',
20+
description: 'Provide name of your existing RDS Instance for which you want to receive event notifications'
21+
});
22+
// SNS Topic for RDS Event Subscription
23+
const topic = new sns.Topic(this, 'SnsForRdsEventSubscription', {
24+
displayName: 'rds-subscription-topic'
25+
});
26+
const awsAccountId = cdk.Fn.ref('AWS::AccountId');
27+
// SNS Topic Policy
28+
const topicPolicy = new sns.TopicPolicy(this, 'SnsTopicPolicyEventRule', {
29+
topics: [topic]
30+
});
31+
topicPolicy.document.addStatements(
32+
new iam.PolicyStatement({
33+
actions: [
34+
"SNS:GetTopicAttributes",
35+
"SNS:SetTopicAttributes",
36+
"SNS:AddPermission",
37+
"SNS:RemovePermission",
38+
"SNS:DeleteTopic",
39+
"SNS:Subscribe",
40+
"SNS:ListSubscriptionsByTopic",
41+
"SNS:Publish",
42+
"SNS:Receive"
43+
],
44+
principals: [new iam.AccountPrincipal(awsAccountId)],
45+
resources: [topic.topicArn],
46+
conditions: {
47+
StringEquals: {
48+
"aws:SourceOwner": awsAccountId
49+
}
50+
}
51+
}),
52+
new iam.PolicyStatement({
53+
effect: iam.Effect.ALLOW,
54+
actions: ["sns:Publish"],
55+
resources: [topic.topicArn],
56+
principals: [new iam.ServicePrincipal("events.rds.amazonaws.com")]
57+
})
58+
);
59+
topic.addSubscription(new subscriptions.EmailSubscription(snsEndpoint.valueAsString, { json: true }));
60+
// RDS Event Subscription
61+
new rds.CfnEventSubscription(this, 'RdsEventSubscription', {
62+
enabled: true,
63+
snsTopicArn: topic.topicArn,
64+
sourceIds: [rdsInstanceName.valueAsString],
65+
sourceType: 'db-instance',
66+
eventCategories: ['failure', 'low storage', 'availability']
67+
});
68+
// Outputs
69+
new cdk.CfnOutput(this, 'MySnsTopicName', {
70+
value: topic.topicName,
71+
description: 'SNS topic name'
72+
});
73+
new cdk.CfnOutput(this, 'RDSInstanceNames', {
74+
value: rdsInstanceName.valueAsString
75+
});
76+
}
77+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "rds-sns-event-notification-cdk-ts",
3+
"version": "0.1.0",
4+
"bin": {
5+
"rds-sns-event-notification-cdk-ts": "bin/rds-sns-event-notification-cdk-ts.js"
6+
},
7+
"scripts": {
8+
"build": "tsc",
9+
"watch": "tsc -w",
10+
"test": "jest",
11+
"cdk": "cdk"
12+
},
13+
"devDependencies": {
14+
"@types/jest": "^29.5.4",
15+
"@types/node": "20.5.3",
16+
"jest": "^29.6.3",
17+
"ts-jest": "^29.1.1",
18+
"aws-cdk": "2.93.0",
19+
"ts-node": "^10.9.1",
20+
"typescript": "~5.1.6"
21+
},
22+
"dependencies": {
23+
"aws-cdk-lib": "2.93.0",
24+
"constructs": "^10.0.0",
25+
"source-map-support": "^0.5.21"
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// import * as cdk from 'aws-cdk-lib';
2+
// import { Template } from 'aws-cdk-lib/assertions';
3+
// import * as RdsSnsEventNotificationCdkTs from '../lib/rds-sns-event-notification-cdk-ts-stack';
4+
5+
// example test. To run these tests, uncomment this file along with the
6+
// example resource in lib/rds-sns-event-notification-cdk-ts-stack.ts
7+
test('SQS Queue Created', () => {
8+
// const app = new cdk.App();
9+
// // WHEN
10+
// const stack = new RdsSnsEventNotificationCdkTs.RdsSnsEventNotificationCdkTsStack(app, 'MyTestStack');
11+
// // THEN
12+
// const template = Template.fromStack(stack);
13+
14+
// template.hasResourceProperties('AWS::SQS::Queue', {
15+
// VisibilityTimeout: 300
16+
// });
17+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"module": "commonjs",
5+
"lib": [
6+
"es2020",
7+
"dom"
8+
],
9+
"declaration": true,
10+
"strict": true,
11+
"noImplicitAny": true,
12+
"strictNullChecks": true,
13+
"noImplicitThis": true,
14+
"alwaysStrict": true,
15+
"noUnusedLocals": false,
16+
"noUnusedParameters": false,
17+
"noImplicitReturns": true,
18+
"noFallthroughCasesInSwitch": false,
19+
"inlineSourceMap": true,
20+
"inlineSources": true,
21+
"experimentalDecorators": true,
22+
"strictPropertyInitialization": false,
23+
"typeRoots": [
24+
"./node_modules/@types"
25+
]
26+
},
27+
"exclude": [
28+
"node_modules",
29+
"cdk.out"
30+
]
31+
}

0 commit comments

Comments
 (0)