Skip to content

Commit

Permalink
wip: add infra
Browse files Browse the repository at this point in the history
  • Loading branch information
blacha committed Dec 13, 2024
1 parent 7aaae0c commit 102fc42
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
32 changes: 30 additions & 2 deletions packages/_infra/src/analytics/edge.analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { RetentionDays } from 'aws-cdk-lib/aws-logs';
import { BlockPublicAccess, Bucket } from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';

const CODE_PATH = '../lambda-analytics/dist';
const CodePath = '../lambda-analytics/dist';
const CodePathV2 = '../lambda-analytics-cloudfront/dist';

export interface EdgeAnalyticsProps extends StackProps {
distributionId: string;
Expand Down Expand Up @@ -36,7 +37,7 @@ export class EdgeAnalytics extends Stack {
memorySize: 2048,
timeout: Duration.minutes(10),
handler: 'index.handler',
code: lambda.Code.fromAsset(CODE_PATH),
code: lambda.Code.fromAsset(CodePath),
environment: {
[Env.Analytics.CloudFrontId]: distributionId,
[Env.Analytics.CacheBucket]: `s3://${cacheBucket.bucketName}`,
Expand All @@ -53,5 +54,32 @@ export class EdgeAnalytics extends Stack {
// Run this lambda function every hour
const rule = new Rule(this, 'AnalyticRule', { schedule: Schedule.rate(Duration.hours(1)) });
rule.addTarget(new LambdaFunction(this.lambda));

const v2Lambda = new lambda.Function(this, 'AnalyticV2Lambda', {
runtime: lambda.Runtime.NODEJS_LATEST,
memorySize: 2048,
timeout: Duration.minutes(10),
handler: 'index.handler',
code: lambda.Code.fromAsset(CodePathV2),
environment: {
[Env.Analytics.CloudFrontId]: distributionId,
[Env.Analytics.CacheBucket]: `s3://${cacheBucket.bucketName}`,
[Env.Analytics.CloudFrontSourceBucket]: `s3://${logBucket.bucketName}`,
[Env.Analytics.MaxRecords]: String(24 * 7 * 4),
[Env.Analytics.ElasticId]: Env.get(Env.Analytics.ElasticId),

Check failure on line 69 in packages/_infra/src/analytics/edge.analytics.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Type of computed property's value is 'string | undefined', which is not assignable to type 'string'.

Check failure on line 69 in packages/_infra/src/analytics/edge.analytics.ts

View workflow job for this annotation

GitHub Actions / build-containers

Type of computed property's value is 'string | undefined', which is not assignable to type 'string'.

Check failure on line 69 in packages/_infra/src/analytics/edge.analytics.ts

View workflow job for this annotation

GitHub Actions / build-deploy

Type of computed property's value is 'string | undefined', which is not assignable to type 'string'.

Check failure on line 69 in packages/_infra/src/analytics/edge.analytics.ts

View workflow job for this annotation

GitHub Actions / screenshot

Type of computed property's value is 'string | undefined', which is not assignable to type 'string'.

Check failure on line 69 in packages/_infra/src/analytics/edge.analytics.ts

View workflow job for this annotation

GitHub Actions / smoke

Type of computed property's value is 'string | undefined', which is not assignable to type 'string'.
[Env.Analytics.ElasticApiKey]: Env.get(Env.Analytics.ElasticApiKey),

Check failure on line 70 in packages/_infra/src/analytics/edge.analytics.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Type of computed property's value is 'string | undefined', which is not assignable to type 'string'.

Check failure on line 70 in packages/_infra/src/analytics/edge.analytics.ts

View workflow job for this annotation

GitHub Actions / build-containers

Type of computed property's value is 'string | undefined', which is not assignable to type 'string'.

Check failure on line 70 in packages/_infra/src/analytics/edge.analytics.ts

View workflow job for this annotation

GitHub Actions / build-deploy

Type of computed property's value is 'string | undefined', which is not assignable to type 'string'.

Check failure on line 70 in packages/_infra/src/analytics/edge.analytics.ts

View workflow job for this annotation

GitHub Actions / screenshot

Type of computed property's value is 'string | undefined', which is not assignable to type 'string'.

Check failure on line 70 in packages/_infra/src/analytics/edge.analytics.ts

View workflow job for this annotation

GitHub Actions / smoke

Type of computed property's value is 'string | undefined', which is not assignable to type 'string'.
AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
},
logRetention: RetentionDays.ONE_MONTH,
loggingFormat: lambda.LoggingFormat.JSON,
});

cacheBucket.grantReadWrite(v2Lambda);
logBucket.grantRead(v2Lambda);

// Run this lambda function every hour
new Rule(this, 'AnalyticRule', { schedule: Schedule.rate(Duration.hours(1)) }).addTarget(
new LambdaFunction(v2Lambda),
);
}
}
10 changes: 9 additions & 1 deletion packages/shared/src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,18 @@ export const Env = {
Analytics: {
CloudFrontId: 'ANALYTICS_CLOUD_FRONT_ID',
CloudFrontSourceBucket: 'ANALYTICS_CLOUD_FRONT_SOURCE_BUCKET',

/** Where to store the analytic cache data */
CacheBucket: 'ANALYTICS_CACHE_BUCKET',

/** Max number of records to process in the analytics process */
MaxRecords: 'ANALYTICS_MAX_RECORDS',
},

/** Elastic server Id */
ElasticId: 'ELASTIC_ID',
/** ElasticSearch's API key */
ElasticApiKey: 'ELASTIC_API_KEY',
} as const,

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

0 comments on commit 102fc42

Please sign in to comment.