
cdkのsolution constructが便利だったのでブログに書いてみます。
solution constructとは
AWSの公式ドキュメントに説明がありました。
aws.amazon.com
AWS Solutions Constructs は、2 つ以上の CDK のリソースを組み合わせ、ロギングや暗号化などのベストプラクティスを実装する複数サービスのパターンを提供します。
要はよくあるパターンをAWSが準備してくれているということですね。
本記事で利用したもの
今回はaws-events-rule-snsを利用してみました。
docs.aws.amazon.com
こちらのイメージ図は下記です。
よくある構成です。

動かしてみる
下記のようにソースをサンプルで書いてみました。
enableEncryptionWithCustomerManagedKeyをfalseにしておくと
kmsをデフォルトキーで利用してくれます。
(私は最初こちらを指定していなくて無駄なkmsを作成してしまいました)
フォルダ構成イメージ
lib └── aws-events-rule-sns-stack.ts patterns ├── SecurityhubNotify.ts └── index.ts
aws-events-rule-sns-stack.ts
import * as cdk from '@aws-cdk/core';
import { EventsRuleToSnsProps, EventsRuleToSns } from "@aws-solutions-constructs/aws-events-rule-sns";
import * as patterns from '../patterns/index'
export class AwsEventsRuleSnsStack extends cdk.Stack {
public readonly constructStack: EventsRuleToSns;
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const patternToNotify = patterns[patterns.patternName]
const envcode = new cdk.CfnParameter(this, "envcode", {
type: "String",
description: "envcode",
default:"test"
});
const constructStack = new EventsRuleToSns(this, 'eveSns', {
eventRuleProps: {
eventPattern: patternToNotify,
ruleName: `${envcode.valueAsString}-${patterns.patternName}`
},
topicProps:{
topicName:`${envcode.valueAsString}-${patterns.patternName}`
},
enableEncryptionWithCustomerManagedKey: false
});
this.constructStack=constructStack
}
}
index.ts
export { SecurityhubNotify } from "./SecurityhubNotify";
export const patternName='SecurityhubNotify' ;
SecurityhubNotify.ts
export const SecurityhubNotify={
"source": ["aws.securityhub"],
"detail-type": ["Security Hub Findings - Imported"],
"detail": {
"findings": {
"Compliance": {
"Status": ["FAILED", "WARNING", "NOT_AVAILABLE"]
}
}
}
}
package.json(関連部分のみ抜粋しています)
"dependencies": {
"@aws-cdk/core": "^1.114.0",
"@aws-solutions-constructs/aws-events-rule-sns": "^1.114.0",
"source-map-support": "^0.5.19"
},
deployとリソース確認
deployします
cdk deploy --parameters envcode=test
events+snsとsnsポリシーが作成されています。
ポリシーも良い感じでeventsへの許可が記載されています。
{
"Sid": "2",
"Effect": "Allow",
"Principal": {
"Service": "events.amazonaws.com"
},
"Action": "sns:Publish",
"Resource": "arn:aws:sns:ap-northeast-1:xxxxxx:test-SecurityhubNotify"
}
触ってみて
感覚的にはeventsのフィルター部分だけ書けば、
events + snsを作成してくれるので便利でした。
他にもlambdaやs3等を絡めたパターンもあるようなので
機会があれば触ってみようと思います。