Skip to content

Commit 102fc42

Browse files
committed
wip: add infra
1 parent 7aaae0c commit 102fc42

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

packages/_infra/src/analytics/edge.analytics.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { RetentionDays } from 'aws-cdk-lib/aws-logs';
77
import { BlockPublicAccess, Bucket } from 'aws-cdk-lib/aws-s3';
88
import { Construct } from 'constructs';
99

10-
const CODE_PATH = '../lambda-analytics/dist';
10+
const CodePath = '../lambda-analytics/dist';
11+
const CodePathV2 = '../lambda-analytics-cloudfront/dist';
1112

1213
export interface EdgeAnalyticsProps extends StackProps {
1314
distributionId: string;
@@ -36,7 +37,7 @@ export class EdgeAnalytics extends Stack {
3637
memorySize: 2048,
3738
timeout: Duration.minutes(10),
3839
handler: 'index.handler',
39-
code: lambda.Code.fromAsset(CODE_PATH),
40+
code: lambda.Code.fromAsset(CodePath),
4041
environment: {
4142
[Env.Analytics.CloudFrontId]: distributionId,
4243
[Env.Analytics.CacheBucket]: `s3://${cacheBucket.bucketName}`,
@@ -53,5 +54,32 @@ export class EdgeAnalytics extends Stack {
5354
// Run this lambda function every hour
5455
const rule = new Rule(this, 'AnalyticRule', { schedule: Schedule.rate(Duration.hours(1)) });
5556
rule.addTarget(new LambdaFunction(this.lambda));
57+
58+
const v2Lambda = new lambda.Function(this, 'AnalyticV2Lambda', {
59+
runtime: lambda.Runtime.NODEJS_LATEST,
60+
memorySize: 2048,
61+
timeout: Duration.minutes(10),
62+
handler: 'index.handler',
63+
code: lambda.Code.fromAsset(CodePathV2),
64+
environment: {
65+
[Env.Analytics.CloudFrontId]: distributionId,
66+
[Env.Analytics.CacheBucket]: `s3://${cacheBucket.bucketName}`,
67+
[Env.Analytics.CloudFrontSourceBucket]: `s3://${logBucket.bucketName}`,
68+
[Env.Analytics.MaxRecords]: String(24 * 7 * 4),
69+
[Env.Analytics.ElasticId]: Env.get(Env.Analytics.ElasticId),
70+
[Env.Analytics.ElasticApiKey]: Env.get(Env.Analytics.ElasticApiKey),
71+
AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
72+
},
73+
logRetention: RetentionDays.ONE_MONTH,
74+
loggingFormat: lambda.LoggingFormat.JSON,
75+
});
76+
77+
cacheBucket.grantReadWrite(v2Lambda);
78+
logBucket.grantRead(v2Lambda);
79+
80+
// Run this lambda function every hour
81+
new Rule(this, 'AnalyticRule', { schedule: Schedule.rate(Duration.hours(1)) }).addTarget(
82+
new LambdaFunction(v2Lambda),
83+
);
5684
}
5785
}

packages/shared/src/const.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,18 @@ export const Env = {
5353
Analytics: {
5454
CloudFrontId: 'ANALYTICS_CLOUD_FRONT_ID',
5555
CloudFrontSourceBucket: 'ANALYTICS_CLOUD_FRONT_SOURCE_BUCKET',
56+
57+
/** Where to store the analytic cache data */
5658
CacheBucket: 'ANALYTICS_CACHE_BUCKET',
59+
5760
/** Max number of records to process in the analytics process */
5861
MaxRecords: 'ANALYTICS_MAX_RECORDS',
59-
},
62+
63+
/** Elastic server Id */
64+
ElasticId: 'ELASTIC_ID',
65+
/** ElasticSearch's API key */
66+
ElasticApiKey: 'ELASTIC_API_KEY',
67+
} as const,
6068

6169
/** Load a environment var defaulting to defaultOutput if it does not exist */
6270
get(envName: string): string | undefined {

0 commit comments

Comments
 (0)