|
| 1 | +import * as c from '@aws-accelerator/common-config'; |
| 2 | +import { AccountStacks } from '../../common/account-stacks'; |
| 3 | +import * as cdk from '@aws-cdk/core'; |
| 4 | +import * as cloudwatch from '@aws-cdk/aws-cloudwatch'; |
| 5 | +import { Account, getAccountId } from '@aws-accelerator/common-outputs/src/accounts'; |
| 6 | +import { createName, createSnsTopicName } from '@aws-accelerator/cdk-accelerator/src/core/accelerator-name-generator'; |
| 7 | + |
| 8 | +export interface CloudWatchStep2Props { |
| 9 | + accountStacks: AccountStacks; |
| 10 | + config: c.AcceleratorConfig; |
| 11 | + accounts: Account[]; |
| 12 | +} |
| 13 | + |
| 14 | +export async function step2(props: CloudWatchStep2Props) { |
| 15 | + const { accountStacks, config, accounts } = props; |
| 16 | + const globalOptions = config['global-options']; |
| 17 | + const centralLogServices = globalOptions['central-log-services']; |
| 18 | + if (!globalOptions.cloudwatch) { |
| 19 | + console.log(`No Configuration defined for CloudWatch Deployment`); |
| 20 | + return; |
| 21 | + } |
| 22 | + const alarmsConfig = globalOptions.cloudwatch.alarms; |
| 23 | + const alarmDefaultDefinition: c.CloudWatchDefaultAlarmDefinition = alarmsConfig; |
| 24 | + for (const alarmconfig of alarmsConfig.definitions) { |
| 25 | + const accountKeys: string[] = []; |
| 26 | + const regions: string[] = []; |
| 27 | + if (alarmconfig.accounts && alarmconfig.accounts.includes('ALL')) { |
| 28 | + // TODO Ignore for now implementation will come in phase 2 |
| 29 | + } else { |
| 30 | + accountKeys.push(...(alarmconfig.accounts || alarmDefaultDefinition['default-accounts'])); |
| 31 | + } |
| 32 | + |
| 33 | + if (alarmconfig.regions && alarmconfig.regions.includes('ALL')) { |
| 34 | + // TODO Ignore for now implementation will come in phase 2 |
| 35 | + } else { |
| 36 | + regions.push(...(alarmconfig.regions || alarmDefaultDefinition['default-regions'])); |
| 37 | + } |
| 38 | + for (const accountKey of accountKeys) { |
| 39 | + for (const region of regions) { |
| 40 | + const accountStack = accountStacks.tryGetOrCreateAccountStack(accountKey, region); |
| 41 | + if (!accountStack) { |
| 42 | + console.error(`Cannot find account stack ${accountKey}: ${region}, while deploying CloudWatch Alarm`); |
| 43 | + continue; |
| 44 | + } |
| 45 | + new cloudwatch.CfnAlarm(accountStack, `CloudAlarm${alarmconfig['alarm-name']}`, { |
| 46 | + alarmDescription: alarmconfig['alarm-description'], |
| 47 | + alarmName: createName({ |
| 48 | + name: alarmconfig['alarm-name'], |
| 49 | + suffixLength: 0, |
| 50 | + }), |
| 51 | + metricName: alarmconfig['metric-name'], |
| 52 | + evaluationPeriods: alarmconfig['evaluation-periods'] || alarmDefaultDefinition['default-evaluation-periods'], |
| 53 | + comparisonOperator: |
| 54 | + alarmconfig['comparison-operator'] || alarmDefaultDefinition['default-comparison-operator'], |
| 55 | + namespace: alarmconfig.namespace || alarmDefaultDefinition['default-namespace'], |
| 56 | + statistic: alarmconfig.statistic || alarmDefaultDefinition['default-statistic'], |
| 57 | + period: alarmconfig.period || alarmDefaultDefinition['default-period'], |
| 58 | + treatMissingData: alarmconfig['treat-missing-data'] || alarmDefaultDefinition['default-treat-missing-data'], |
| 59 | + threshold: alarmconfig.threshold || alarmDefaultDefinition['default-threshold'], |
| 60 | + alarmActions: [ |
| 61 | + `arn:aws:sns:${cdk.Aws.REGION}:${getAccountId(accounts, centralLogServices.account)}:${createSnsTopicName( |
| 62 | + alarmconfig['sns-alert-level'], |
| 63 | + )}`, |
| 64 | + ], |
| 65 | + }); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments