|
| 1 | +import * as c from '@aws-accelerator/common-config'; |
| 2 | +import { AccountStacks } from '../../common/account-stacks'; |
| 3 | +import * as sns from '@aws-cdk/aws-sns'; |
| 4 | +import { createSnsTopicName } from '@aws-accelerator/cdk-accelerator/src/core/accelerator-name-generator'; |
| 5 | +import { SNS_NOTIFICATION_TYPES } from '@aws-accelerator/common/src/util/constants'; |
| 6 | +import * as path from 'path'; |
| 7 | +import * as lambda from '@aws-cdk/aws-lambda'; |
| 8 | +import * as cdk from '@aws-cdk/core'; |
| 9 | +import { IamRoleOutputFinder } from '@aws-accelerator/common-outputs/src/iam-role'; |
| 10 | +import { StackOutput } from '@aws-accelerator/common-outputs/src/stack-output'; |
| 11 | +import * as iam from '@aws-cdk/aws-iam'; |
| 12 | + |
| 13 | +export interface SnsStep1Props { |
| 14 | + accountStacks: AccountStacks; |
| 15 | + config: c.AcceleratorConfig; |
| 16 | + outputs: StackOutput[]; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * |
| 21 | + * Create SNS Topics High, Medium, Low, Ignore |
| 22 | + * in Central-Log-Services Account |
| 23 | + */ |
| 24 | +export async function step1(props: SnsStep1Props) { |
| 25 | + const { accountStacks, config, outputs } = props; |
| 26 | + const globalOptions = config['global-options']; |
| 27 | + const centralLogServices = globalOptions['central-log-services']; |
| 28 | + const supportedRegions = globalOptions['supported-regions']; |
| 29 | + const excludeRegions = centralLogServices['sns-excl-regions']; |
| 30 | + const regions = supportedRegions.filter(r => !excludeRegions?.includes(r)); |
| 31 | + if (!regions.includes(centralLogServices.region)) { |
| 32 | + regions.push(centralLogServices.region); |
| 33 | + } |
| 34 | + const subscribeEmails = centralLogServices['sns-subscription-emails']; |
| 35 | + const snsSubscriberLambdaRoleOutput = IamRoleOutputFinder.tryFindOneByName({ |
| 36 | + outputs, |
| 37 | + accountKey: centralLogServices.account, |
| 38 | + roleKey: 'SnsSubscriberLambda', |
| 39 | + }); |
| 40 | + if (!snsSubscriberLambdaRoleOutput) { |
| 41 | + console.error(`Role required for SNS Subscription Lambda is not created in ${centralLogServices.account}`); |
| 42 | + return; |
| 43 | + } |
| 44 | + for (const region of regions) { |
| 45 | + const accountStack = accountStacks.tryGetOrCreateAccountStack(centralLogServices.account, region); |
| 46 | + if (!accountStack) { |
| 47 | + console.error(`Cannot find account stack ${centralLogServices.account}: ${region}, while deploying SNS`); |
| 48 | + continue; |
| 49 | + } |
| 50 | + const lambdaPath = require.resolve('@aws-accelerator/deployments-runtime'); |
| 51 | + const lambdaDir = path.dirname(lambdaPath); |
| 52 | + const lambdaCode = lambda.Code.fromAsset(lambdaDir); |
| 53 | + const role = iam.Role.fromRoleArn(accountStack, `SnsSubscriberLambdaRole`, snsSubscriberLambdaRoleOutput.roleArn); |
| 54 | + let snsSubscriberFunc: lambda.Function | undefined; |
| 55 | + if (region !== centralLogServices.region) { |
| 56 | + snsSubscriberFunc = new lambda.Function(accountStack, `SnsSubscriberLambda`, { |
| 57 | + runtime: lambda.Runtime.NODEJS_12_X, |
| 58 | + handler: 'index.createSnsPublishToCentralRegion', |
| 59 | + code: lambdaCode, |
| 60 | + role, |
| 61 | + environment: { |
| 62 | + CENTRAL_LOG_SERVICES_REGION: centralLogServices.region, |
| 63 | + }, |
| 64 | + timeout: cdk.Duration.minutes(15), |
| 65 | + }); |
| 66 | + |
| 67 | + snsSubscriberFunc.addPermission(`InvokePermission-SnsSubscriberLambda`, { |
| 68 | + action: 'lambda:InvokeFunction', |
| 69 | + principal: new iam.ServicePrincipal('sns.amazonaws.com'), |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + const ignoreActionFunc = new lambda.Function(accountStack, `IgnoreActionLambda`, { |
| 74 | + runtime: lambda.Runtime.NODEJS_12_X, |
| 75 | + handler: 'index.createIgnoreAction', |
| 76 | + code: lambdaCode, |
| 77 | + role, |
| 78 | + timeout: cdk.Duration.minutes(15), |
| 79 | + }); |
| 80 | + |
| 81 | + ignoreActionFunc.addPermission(`InvokePermission-IgnoreActionLambda`, { |
| 82 | + action: 'lambda:InvokeFunction', |
| 83 | + principal: new iam.ServicePrincipal('sns.amazonaws.com'), |
| 84 | + }); |
| 85 | + |
| 86 | + for (const notificationType of SNS_NOTIFICATION_TYPES) { |
| 87 | + const topicName = createSnsTopicName(notificationType); |
| 88 | + const topic = new sns.Topic(accountStack, `SnsNotificationTopic${notificationType}`, { |
| 89 | + displayName: topicName, |
| 90 | + topicName, |
| 91 | + }); |
| 92 | + |
| 93 | + // Allowing Publish from CloudWatch Service form any account |
| 94 | + topic.grantPublish({ |
| 95 | + grantPrincipal: new iam.ServicePrincipal('cloudwatch.amazonaws.com'), |
| 96 | + }); |
| 97 | + |
| 98 | + // Allowing Publish from Lambda Service form any account |
| 99 | + topic.grantPublish({ |
| 100 | + grantPrincipal: new iam.ServicePrincipal('lambda.amazonaws.com'), |
| 101 | + }); |
| 102 | + |
| 103 | + if (region === centralLogServices.region && subscribeEmails && subscribeEmails[notificationType]) { |
| 104 | + subscribeEmails[notificationType].forEach((email, index) => { |
| 105 | + new sns.CfnSubscription(accountStack, `SNSTopicSubscriptionFor${notificationType}-${index + 1}`, { |
| 106 | + topicArn: topic.topicArn, |
| 107 | + protocol: sns.SubscriptionProtocol.EMAIL, |
| 108 | + endpoint: email, |
| 109 | + }); |
| 110 | + }); |
| 111 | + } else if (region === centralLogServices.region && notificationType.toLowerCase() === 'ignore') { |
| 112 | + new sns.CfnSubscription(accountStack, `SNSTopicSubscriptionFor${notificationType}`, { |
| 113 | + topicArn: topic.topicArn, |
| 114 | + protocol: sns.SubscriptionProtocol.LAMBDA, |
| 115 | + endpoint: ignoreActionFunc.functionArn, |
| 116 | + }); |
| 117 | + } else if (region !== centralLogServices.region && snsSubscriberFunc) { |
| 118 | + new sns.CfnSubscription(accountStack, `SNSTopicSubscriptionFor${notificationType}`, { |
| 119 | + topicArn: topic.topicArn, |
| 120 | + protocol: sns.SubscriptionProtocol.LAMBDA, |
| 121 | + endpoint: snsSubscriberFunc.functionArn, |
| 122 | + }); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments