|
| 1 | +import * as cdk from 'aws-cdk-lib' |
| 2 | +import * as lambda from 'aws-cdk-lib/aws-lambda' |
| 3 | +import * as logs from 'aws-cdk-lib/aws-logs' |
| 4 | +import { Construct } from 'constructs' |
| 5 | + |
| 6 | +/** |
| 7 | + * Interface for LogForwardingSetup properties |
| 8 | + */ |
| 9 | +interface LogForwardingSetupProps { |
| 10 | + /** Prefix for naming resources */ |
| 11 | + prefix: string |
| 12 | + /** Deployment stage (sit, uat, prod) */ |
| 13 | + stage: string |
| 14 | + /** ARN of the log destination in NGAP SecLog account */ |
| 15 | + logDestinationArn: string |
| 16 | + /** Map of Lambda functions to configure log forwarding for */ |
| 17 | + lambdas: { [key: string]: lambda.Function } |
| 18 | + /** Log retention period in days */ |
| 19 | + logRetentionDays?: logs.RetentionDays |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Sets up CloudWatch Log Groups and Subscription Filters to forward logs to NGAP SecLog account. |
| 24 | + * |
| 25 | + * This class configures log forwarding to Splunk via NGAP's centralized logging infrastructure. |
| 26 | + * Logs are forwarded to a Kinesis stream in the SecLog account which then forwards them to Splunk. |
| 27 | + * |
| 28 | + * @see https://wiki.earthdata.nasa.gov/pages/viewpage.action?pageId=147423378 |
| 29 | + */ |
| 30 | +export class LogForwardingSetup { |
| 31 | + /** The ARN of the log destination for log forwarding */ |
| 32 | + private readonly logDestinationArn: string |
| 33 | + |
| 34 | + /** Log retention period */ |
| 35 | + private readonly logRetentionDays: logs.RetentionDays |
| 36 | + |
| 37 | + /** Prefix for naming resources */ |
| 38 | + private readonly prefix: string |
| 39 | + |
| 40 | + /** Deployment stage */ |
| 41 | + private readonly stage: string |
| 42 | + |
| 43 | + /** Stack name for resource naming */ |
| 44 | + private readonly stackName: string |
| 45 | + |
| 46 | + /** |
| 47 | + * Constructs a new LogForwardingSetup instance |
| 48 | + * @param {Construct} scope - The scope in which to define this construct |
| 49 | + * @param {string} id - The construct ID |
| 50 | + * @param {LogForwardingSetupProps} props - Configuration properties |
| 51 | + */ |
| 52 | + constructor(scope: Construct, id: string, props: LogForwardingSetupProps) { |
| 53 | + this.logRetentionDays = props.logRetentionDays || logs.RetentionDays.ONE_MONTH |
| 54 | + this.prefix = props.prefix |
| 55 | + this.stage = props.stage |
| 56 | + this.stackName = cdk.Stack.of(scope).stackName |
| 57 | + |
| 58 | + if (!props.logDestinationArn) { |
| 59 | + throw new Error( |
| 60 | + 'logDestinationArn is required for log forwarding. ' |
| 61 | + + 'Please set the LOG_DESTINATION_ARN environment variable in your Bamboo deployment.' |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + this.logDestinationArn = props.logDestinationArn |
| 66 | + |
| 67 | + // Set up log groups and subscription filters for each Lambda function |
| 68 | + this.setupLogForwarding(scope, props) |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Sets up log groups and subscription filters for all Lambda functions. |
| 73 | + * |
| 74 | + * For each Lambda function: |
| 75 | + * 1. Creates an explicit CloudWatch Log Group with retention policy |
| 76 | + * 2. Creates a Subscription Filter to forward logs to NGAP SecLog destination |
| 77 | + * |
| 78 | + * Note: We use the pre-configured destination ARN in the SecLog account, |
| 79 | + * not a role from our account. The destination already has the necessary |
| 80 | + * permissions to receive logs from our account. |
| 81 | + * |
| 82 | + * @param {Construct} scope - The scope in which to define constructs |
| 83 | + * @param {LogForwardingSetupProps} props - Configuration properties |
| 84 | + * @private |
| 85 | + */ |
| 86 | + private setupLogForwarding(scope: Construct, props: LogForwardingSetupProps) { |
| 87 | + const { lambdas } = props |
| 88 | + |
| 89 | + // Set up log forwarding for each Lambda function |
| 90 | + Object.entries(lambdas).forEach(([key, lambdaFunction]) => { |
| 91 | + const sanitizedKey = key.replace(/[^a-zA-Z0-9]/g, '-') |
| 92 | + |
| 93 | + // Create explicit log group with retention |
| 94 | + // Log group naming: /aws/lambda/{prefix}-{stage}-{functionName} |
| 95 | + // This enables easy Splunk searches: source="/aws/lambda/kms-sit-*" |
| 96 | + const logGroup = new logs.LogGroup(scope, `${sanitizedKey}-LogGroup`, { |
| 97 | + logGroupName: `/aws/lambda/${lambdaFunction.functionName}`, |
| 98 | + retention: this.logRetentionDays, |
| 99 | + removalPolicy: cdk.RemovalPolicy.DESTROY |
| 100 | + }) |
| 101 | + |
| 102 | + // Create subscription filter to forward logs to NGAP SecLog destination |
| 103 | + // The destination ARN is pre-configured in the SecLog account and has |
| 104 | + // the necessary permissions to accept logs from our account |
| 105 | + const subscriptionFilter = new logs.CfnSubscriptionFilter( |
| 106 | + scope, |
| 107 | + `${sanitizedKey}-subscriptionFilter`, |
| 108 | + { |
| 109 | + logGroupName: logGroup.logGroupName, |
| 110 | + filterPattern: '', // Empty pattern forwards all logs |
| 111 | + destinationArn: this.logDestinationArn, |
| 112 | + filterName: `${this.stackName}-${this.stage}-${sanitizedKey}-subscriptionFilter` |
| 113 | + // No roleArn needed - the destination is in the SecLog account |
| 114 | + } |
| 115 | + ) |
| 116 | + |
| 117 | + // Ensure proper dependency order |
| 118 | + subscriptionFilter.node.addDependency(logGroup) |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
0 commit comments