Skip to content

Commit

Permalink
Revert "feat(infra): remove logging stack as that is now handled inte…
Browse files Browse the repository at this point in the history
…rnally (#1701)"

This reverts commit ffcbf35.
  • Loading branch information
blacha committed Jul 7, 2021
1 parent 366aa2b commit cc7dd98
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/_infra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@aws-cdk/core": ">=1.100.0",
"@basemaps/lambda-tiler": "^6.2.0",
"@basemaps/shared": "^6.1.0",
"@linzjs/cdk-elastic-shipper": "^0.6.2",
"aws-cdk": "^1.100.0"
}
}
3 changes: 3 additions & 0 deletions packages/_infra/src/deploy.env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ export const DeployEnv = {

/** TLS certificate to use with Cloudfront */
CloudFrontTlsCertArn: 'CLOUDFRONT_CERTIFICATE_ARN',

/** Allow another account to write logs into our bucket */
LogAccountId: 'LOG_ACCOUNT_ID',
};
3 changes: 3 additions & 0 deletions packages/_infra/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CogBuilderStack } from './cogify';
import { BaseMapsRegion } from './config';
import { DeployEnv } from './deploy.env';
import { EdgeStack } from './edge';
import { LoggingStack } from './log';
import { getEdgeParameters } from './parameters';
import { ServeStack } from './serve';

Expand All @@ -14,6 +15,8 @@ async function main(): Promise<void> {
/** Using VPC lookups requires a hard coded AWS "account" */
const account = Env.get(DeployEnv.CdkAccount);

new LoggingStack(basemaps, 'Logging', { env: { region: BaseMapsRegion, account } });

/**
* Because we are using Lambda@Edge the edge stack has to be deployed into us-east-1,
* The dynamoDb table needs to be close to our users that has to be deployed in ap-southeast-2
Expand Down
13 changes: 13 additions & 0 deletions packages/_infra/src/log/__test__/log.filter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import o from 'ospec';
import { onLog } from '..';

o.spec('LogFilter', () => {
o('should drop lambda logs', () => {
o(onLog({ '@tags': ['Lambda log'] } as any)).equals(true);
o(onLog({ '@tags': ['Flow Log'] } as any)).equals(undefined);
});

o('should not die if "@tags" doesnt exist', () => {
o(onLog({} as any)).equals(undefined);
});
});
53 changes: 53 additions & 0 deletions packages/_infra/src/log/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Vpc } from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
import * as firehose from '@aws-cdk/aws-kinesisfirehose';
import * as s3 from '@aws-cdk/aws-s3';
import { StringParameter } from '@aws-cdk/aws-ssm';
import * as cdk from '@aws-cdk/core';
import { Env } from '@basemaps/shared';
import { LambdaLogShipperFunction, LogObject } from '@linzjs/cdk-elastic-shipper';
import { DeployEnv } from '../deploy.env';

const ConfigName = `/es-shipper-config/accounts`;

export function onLog(lo: LogObject): boolean | void {
if (lo['@tags']?.includes('Lambda log')) return true;
}

/**
* Basemap's logging
*
* Store all logs generated by basemaps into a s3 bucket for long term storage
* One logs are added to the bucket configure a lambda to ship them elastic search
* for monitoring and alerting
*
* General log flow:
*
* Lambda -> CloudWatch -> Kinesis -> Kinesis Firehose -> S3 -> Lambda -> ElasticSearch
*/
export class LoggingStack extends cdk.Stack {
fireHose: firehose.CfnDeliveryStream;
public shipper: LambdaLogShipperFunction;

public constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const logBucket = new s3.Bucket(this, 'LogBucket', {
versioned: true,
blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
});

const accountId = Env.get(DeployEnv.LogAccountId);
if (accountId != null && accountId !== '') {
logBucket.grantReadWrite(new iam.AccountPrincipal(accountId));
}

new cdk.CfnOutput(this, 'LogBucketArn', { value: logBucket.bucketArn });

const vpc = Vpc.fromLookup(this, 'ShipperVpc', { tags: { default: 'true' } });

const configParameter = StringParameter.fromStringParameterName(this, 'ShipperConfig', ConfigName);
this.shipper = new LambdaLogShipperFunction(this, 'ShipperFunction', { configParameter, vpc, onLog });
this.shipper.addS3Source(logBucket);
}
}

0 comments on commit cc7dd98

Please sign in to comment.