Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(infra): gateway (#5476)

Will Hutchinson0abc7ec43679
diff --git a/infra/bun.lock b/infra/bun.lock
index f46951cc16..375acef765 100644
--- a/infra/bun.lock
+++ b/infra/bun.lock
@@ -135,6 +135,10 @@
"pulumi-fusionauth": "6.0.2",
},
},
+ "stacks/gateway": {
+ "name": "gateway-stack",
+ "version": "0.0.0",
+ },
"stacks/github-runners": {
"name": "github-runners-stack",
"version": "0.0.0",
@@ -573,6 +577,8 @@
"fusionauth-instance-stack": ["fusionauth-instance-stack@workspace:stacks/fusionauth-instance"],
+ "gateway-stack": ["gateway-stack@workspace:stacks/gateway"],
+
"get-caller-file": ["[email protected]", "", {}, "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg=="],
"get-stream": ["[email protected]", "", {}, "sha512-ts6Wi+2j3jQjqi70w5AlN8DFnkSwC+MqmxEzdEALB2qXZYV3X/b1CTfgPLGJNMeAWxdPfU8FO1ms3NUfaHCPYg=="],
diff --git a/infra/packages/resources/src/index.ts b/infra/packages/resources/src/index.ts
index 569dd7bf51..447b1a47d7 100644
--- a/infra/packages/resources/src/index.ts
+++ b/infra/packages/resources/src/index.ts
@@ -22,7 +22,10 @@ export {
createFrecencyTablePolicy,
} from './resources/frecency';
export { createImage } from './resources/image';
-export { serviceLoadBalancer } from './resources/load_balancer';
+export {
+ serviceLoadBalancer,
+ MacroApplicationLoadBalancer,
+} from './resources/load_balancer';
export { Queue } from './resources/queue';
export { QueueAlarms } from './resources/queue_alarms';
export { Database } from './resources/rds';
diff --git a/infra/packages/resources/src/resources/load_balancer.ts b/infra/packages/resources/src/resources/load_balancer.ts
index 2aac4b8df5..325fce4975 100644
--- a/infra/packages/resources/src/resources/load_balancer.ts
+++ b/infra/packages/resources/src/resources/load_balancer.ts
@@ -1,12 +1,258 @@
import * as aws from '@pulumi/aws';
-import type * as pulumi from '@pulumi/pulumi';
+import * as pulumi from '@pulumi/pulumi';
import type { Output } from '@pulumi/pulumi';
-import { MACRO_SUBDOMAIN_CERT, stack } from '../../../shared';
+import {
+ BASE_DOMAIN,
+ CLOUD_TRAIL_SNS_TOPIC_ARN,
+ MACRO_SUBDOMAIN_CERT,
+ stack,
+} from '../../../shared';
import {
DEFAULT_DEREGISTRATION_DELAY_SECONDS,
DEFAULT_TARGET_GROUP_HEALTH_CHECK,
} from './ecs_deployment_defaults';
+type MacroApplicationLoadBalancerArgs = {
+ // The sub domain for the application load balancer
+ // This will be `${subDomain}.${BASE_DOMAIN}`
+ subDomain: string;
+ // AWS resource tags
+ tags: { [key: string]: string };
+ // If the load balancer is internal only or not
+ isInternal: boolean;
+ /// Vpc
+ vpc: {
+ vpcId: pulumi.Output<string> | string;
+ publicSubnetIds: pulumi.Output<string[]> | string[];
+ privateSubnetIds: pulumi.Output<string[]> | string[];
+ };
+ // idle timeout for connections
+ idleTimeout?: number;
+ // Tuning for the CloudWatch alarms. The alarms themselves are always
+ // created; these only override their defaults.
+ alarmOverrides?: {
+ // SNS topics to notify (defaults to the CloudTrail SNS topic)
+ alarmActions?: aws.cloudwatch.MetricAlarmArgs['alarmActions'];
+ // Threshold for ALB-generated 5xx responses per period (default 25)
+ elb5xxThreshold?: number;
+ // Period in seconds for the ALB-generated 5xx alarm (default 180)
+ elb5xxPeriod?: number;
+ };
+};
+
+/**
+ * @description creates an application load balancer component.
+ */
+export class MacroApplicationLoadBalancer extends pulumi.ComponentResource {
+ tags: { [key: string]: string };
+ loadbalancer_security_group: aws.ec2.SecurityGroup;
+ load_balancer: aws.lb.LoadBalancer;
+ https_listener: aws.lb.Listener;
+ dns_record: aws.route53.Record;
+ elb_5xx_alarm: aws.cloudwatch.MetricAlarm;
+ rejected_connections_alarm: aws.cloudwatch.MetricAlarm;
+ constructor(
+ name: string,
+ args: MacroApplicationLoadBalancerArgs,
+ opts?: pulumi.ComponentResourceOptions
+ ) {
+ super('my:components:MacroApplicationLoadBalancer', name, {}, opts);
+ const { tags, isInternal, vpc, idleTimeout, subDomain } = args;
+
+ this.tags = tags;
+
+ // Create load balancer security group
+ this.loadbalancer_security_group = new aws.ec2.SecurityGroup(
+ 'security-group',
+ {
+ name: `${name}-alb-sg`,
+ description: `${name} application load balancer security group`,
+ vpcId: vpc.vpcId,
+ tags: this.tags,
+ },
+ { parent: this }
+ );
+
+ // Ingress rules
+ new aws.vpc.SecurityGroupIngressRule(
+ 'security-group-ingress-http',
+ {
+ securityGroupId: this.loadbalancer_security_group.id,
+ description: 'Allow inbound HTTP traffic',
+ cidrIpv4: '0.0.0.0/0',
+ fromPort: 80,
+ ipProtocol: 'tcp',
+ toPort: 80,
+ tags: this.tags,
+ },
+ { parent: this }
+ );
+
+ new aws.vpc.SecurityGroupIngressRule(
+ 'security-group-ingress-https',
+ {
+ securityGroupId: this.loadbalancer_security_group.id,
+ description: 'Allow inbound HTTPS traffic',
+ cidrIpv4: '0.0.0.0/0',
+ fromPort: 443,
+ ipProtocol: 'tcp',
+ toPort: 443,
+ tags: this.tags,
+ },
+ { parent: this }
+ );
+
+ this.load_balancer = new aws.lb.LoadBalancer(
+ 'application-load-balancer',
+ {
+ name: `${name}-alb`,
+ internal: isInternal,
+ loadBalancerType: 'application', // TODO: eventually we'll want to allow this to be configurable
+ securityGroups: [this.loadbalancer_security_group.id],
+ subnets: isInternal ? vpc.privateSubnetIds : vpc.publicSubnetIds,
+ enableDeletionProtection: false,
+ // default is 60 seconds, can be up to 4000 seconds
+ idleTimeout,
+ tags,
+ // Automatic access logs enabled for the prod (SOC2 compliance)
+ accessLogs: {
+ bucket: 'macro-alb-logging',
+ enabled: stack === 'prod',
+ prefix: `${name}-alb`,
+ },
+ },
+ { parent: this }
+ );
+
+ this.https_listener = new aws.lb.Listener(
+ 'https-listener',
+ {
+ loadBalancerArn: this.load_balancer.arn,
+ port: 443,
+ protocol: 'HTTPS',
+ sslPolicy: 'ELBSecurityPolicy-TLS13-1-2-2021-06',
+ certificateArn: MACRO_SUBDOMAIN_CERT,
+ tags,
+ defaultActions: [
+ {
+ type: 'fixed-response',
+ fixedResponse: {
+ contentType: 'text/plain',
+ messageBody: 'not found',
+ statusCode: '404',
+ },
+ },
+ ],
+ },
+ { parent: this }
+ );
+
+ new aws.lb.Listener(
+ 'http-listener',
+ {
+ loadBalancerArn: this.load_balancer.arn,
+ port: 80,
+ protocol: 'HTTP',
+ tags,
+ defaultActions: [
+ {
+ redirect: {
+ port: '443',
+ statusCode: 'HTTP_301',
+ protocol: 'HTTPS',
+ },
+ type: 'redirect',
+ },
+ ],
+ },
+ { parent: this }
+ );
+
+ // domain record
+ const zone = aws.route53.getZoneOutput({ name: BASE_DOMAIN });
+
+ this.dns_record = new aws.route53.Record(
+ 'domain-record',
+ {
+ name: `${subDomain}.${BASE_DOMAIN}`,
+ type: 'A',
+ zoneId: zone.zoneId,
+ aliases: [
+ {
+ evaluateTargetHealth: false,
+ name: this.load_balancer.dnsName,
+ zoneId: this.load_balancer.zoneId,
+ },
+ ],
+ },
+ { parent: this }
+ );
+
+ // Alarms for failures generated by the load balancer itself. These never
+ // appear in any target group's metrics: a service with no healthy targets
+ // produces ELB 503s, not target 5xxs. Per-service failures (target 5xx,
+ // unhealthy hosts, latency) are alarmed per target group in the service
+ // stacks instead.
+ const alarmActions = args.alarmOverrides?.alarmActions ?? [
+ CLOUD_TRAIL_SNS_TOPIC_ARN,
+ ];
+
+ // 502/503/504s generated by the ALB: no healthy targets in a matched
+ // target group, targets crashing mid-request, or target response timeouts
+ this.elb_5xx_alarm = new aws.cloudwatch.MetricAlarm(
+ 'alarm-elb-5xx',
+ {
+ name: `${name}-elb-5xx-alarm`,
+ alarmDescription: `[${stack.toUpperCase()}]: ${name} ALB is generating 5xx responses (no healthy targets, target crashes, or target timeouts).`,
+ namespace: 'AWS/ApplicationELB',
+ metricName: 'HTTPCode_ELB_5XX_Count',
+ statistic: 'Sum',
+ period: args.alarmOverrides?.elb5xxPeriod ?? 180,
+ evaluationPeriods: 1,
+ threshold: args.alarmOverrides?.elb5xxThreshold ?? 25,
+ comparisonOperator: 'GreaterThanOrEqualToThreshold',
+ // the ALB only emits this metric when at least one 5xx occurred, so
+ // missing data means healthy
+ treatMissingData: 'notBreaching',
+ dimensions: {
+ // CloudWatch expects the arn suffix (`app/...`), not the full arn
+ LoadBalancer: this.load_balancer.arnSuffix,
+ },
+ alarmActions,
+ tags: this.tags,
+ },
+ { parent: this }
+ );
+
+ // Connections rejected because the ALB reached its connection limit;
+ // should always be zero
+ this.rejected_connections_alarm = new aws.cloudwatch.MetricAlarm(
+ 'alarm-rejected-connections',
+ {
+ name: `${name}-rejected-connections-alarm`,
+ alarmDescription: `[${stack.toUpperCase()}]: ${name} ALB is rejecting connections (connection limit reached).`,
+ namespace: 'AWS/ApplicationELB',
+ metricName: 'RejectedConnectionCount',
+ statistic: 'Sum',
+ period: 300,
+ evaluationPeriods: 1,
+ threshold: 0,
+ comparisonOperator: 'GreaterThanThreshold',
+ treatMissingData: 'notBreaching',
+ dimensions: {
+ LoadBalancer: this.load_balancer.arnSuffix,
+ },
+ alarmActions,
+ tags: this.tags,
+ },
+ { parent: this }
+ );
+ }
+}
+
+/**
+ * Legacy service load balancer creation
+ */
export function serviceLoadBalancer(
parent: pulumi.ComponentResource | undefined,
{
diff --git a/infra/packages/shared/src/gateway.ts b/infra/packages/shared/src/gateway.ts
new file mode 100644
index 0000000000..47c01756ac
--- /dev/null
+++ b/infra/packages/shared/src/gateway.ts
@@ -0,0 +1,55 @@
+import * as pulumi from '@pulumi/pulumi';
+import { MACRO_ORG_NAME, stack } from '.';
+
+/**
+ * Outputs exported by the gateway stack (`infra/stacks/gateway`).
+ */
+export type GatewayAlb = {
+ /** ARN of the gateway ALB */
+ albArn: pulumi.Output<string>;
+ /** ARN of the HTTPS listener that service stacks attach their listener rules to */
+ httpsListenerArn: pulumi.Output<string>;
+ /** Security group of the gateway ALB, for service SG ingress/egress pairing */
+ albSecurityGroupId: pulumi.Output<string>;
+ /** DNS name of the gateway ALB, for Route53 alias records in service stacks */
+ albDnsName: pulumi.Output<string>;
+ /** Canonical hosted zone of the gateway ALB (alias record target zone) */
+ albZoneId: pulumi.Output<string>;
+ /** ALB arn suffix (`app/...`) for CloudWatch dimensions and autoscaling resource labels */
+ albArnSuffix: pulumi.Output<string>;
+};
+
+/**
+ * Singleton for the gateway stack reference
+ */
+let _GATEWAY_ALB: GatewayAlb | undefined = undefined;
+
+/**
+ * Gets the shared gateway ALB outputs for the current stack.
+ *
+ * Fails at deploy time if the gateway stack has not been deployed in this
+ * environment yet.
+ */
+export function getGatewayAlb(): GatewayAlb {
+ if (_GATEWAY_ALB) {
+ return _GATEWAY_ALB;
+ }
+
+ const gatewayStack = new pulumi.StackReference('gateway-stack', {
+ name: `${MACRO_ORG_NAME}/gateway/${stack}`,
+ });
+
+ const requireString = (output: string): pulumi.Output<string> =>
+ gatewayStack.requireOutput(output) as pulumi.Output<string>;
+
+ _GATEWAY_ALB = {
+ albArn: requireString('albArn'),
+ httpsListenerArn: requireString('httpsListenerArn'),
+ albSecurityGroupId: requireString('albSecurityGroupId'),
+ albDnsName: requireString('albDnsName'),
+ albZoneId: requireString('albZoneId'),
+ albArnSuffix: requireString('albArnSuffix'),
+ };
+
+ return _GATEWAY_ALB;
+}
diff --git a/infra/packages/shared/src/index.ts b/infra/packages/shared/src/index.ts
index edf2ddbe03..8acd879c9e 100644
--- a/infra/packages/shared/src/index.ts
+++ b/infra/packages/shared/src/index.ts
@@ -51,5 +51,6 @@ export {
type AiToolsInfra,
} from './ai_tools';
export { getKafkaClusterPolicy } from './kafka_cluster_policy';
+export { getGatewayAlb, type GatewayAlb } from './gateway';
export * from './service_urls';
diff --git a/infra/stacks/authentication-service/tsconfig.json b/infra/stacks/authentication-service/tsconfig.json
index 6f83eb665f..4082f16a5d 100644
--- a/infra/stacks/authentication-service/tsconfig.json
+++ b/infra/stacks/authentication-service/tsconfig.json
@@ -1,3 +1,3 @@
{
- "extends": "../../tsconfig.json",
+ "extends": "../../tsconfig.json"
}
diff --git a/infra/stacks/gateway/Pulumi.dev.yaml b/infra/stacks/gateway/Pulumi.dev.yaml
new file mode 100644
index 0000000000..9cfd7a1eb5
--- /dev/null
+++ b/infra/stacks/gateway/Pulumi.dev.yaml
@@ -0,0 +1,3 @@
+config:
+ aws-native:region: us-east-1
+ aws:region: us-east-1
diff --git a/infra/stacks/gateway/Pulumi.prod.yaml b/infra/stacks/gateway/Pulumi.prod.yaml
new file mode 100644
index 0000000000..9cfd7a1eb5
--- /dev/null
+++ b/infra/stacks/gateway/Pulumi.prod.yaml
@@ -0,0 +1,3 @@
+config:
+ aws-native:region: us-east-1
+ aws:region: us-east-1
diff --git a/infra/stacks/gateway/Pulumi.yaml b/infra/stacks/gateway/Pulumi.yaml
new file mode 100644
index 0000000000..e7d8bfcc22
--- /dev/null
+++ b/infra/stacks/gateway/Pulumi.yaml
@@ -0,0 +1,3 @@
+name: gateway
+runtime: nodejs
+description: Gateway Application Load Balancer
diff --git a/infra/stacks/gateway/index.ts b/infra/stacks/gateway/index.ts
new file mode 100644
index 0000000000..70ccfe2c85
--- /dev/null
+++ b/infra/stacks/gateway/index.ts
@@ -0,0 +1,44 @@
+import { stack } from '../../packages/shared';
+import { get_coparse_api_vpc } from '../../packages/vpc';
+import { MacroApplicationLoadBalancer } from '../../packages/resources';
+
+const tags = {
+ environment: stack,
+ tech_lead: 'hutch',
+ project: 'gateway',
+};
+
+const vpc = get_coparse_api_vpc();
+
+const gatewayApplicationLoadBalancer = new MacroApplicationLoadBalancer(
+ `${stack}-gateway`,
+ {
+ // We want `gateway.macro.com` for prod and `${stack}-gateway` for all non-prod environments
+ subDomain: stack === 'prod' ? 'gateway' : `${stack}-gateway`,
+ tags,
+ isInternal: false,
+ idleTimeout: 3600, // This was derived from max idleTimeout used in existing service application load balancers
+ vpc,
+ }
+);
+
+/** ARN of the gateway ALB */
+export const albArn = gatewayApplicationLoadBalancer.load_balancer.arn;
+
+/** ARN of the HTTPS listener that service stacks attach their listener rules to */
+export const httpsListenerArn =
+ gatewayApplicationLoadBalancer.https_listener.arn;
+
+/** Security group of the gateway ALB, for service SG ingress/egress pairing */
+export const albSecurityGroupId =
+ gatewayApplicationLoadBalancer.loadbalancer_security_group.id;
+
+/** DNS name of the gateway ALB, for Route53 alias records in service stacks */
+export const albDnsName = gatewayApplicationLoadBalancer.load_balancer.dnsName;
+
+/** Canonical hosted zone of the gateway ALB (alias record target zone) */
+export const albZoneId = gatewayApplicationLoadBalancer.load_balancer.zoneId;
+
+/** ALB arn suffix (`app/...`) for CloudWatch dimensions and autoscaling resource labels */
+export const albArnSuffix =
+ gatewayApplicationLoadBalancer.load_balancer.arnSuffix;
diff --git a/infra/stacks/gateway/package.json b/infra/stacks/gateway/package.json
new file mode 100644
index 0000000000..e5416573b6
--- /dev/null
+++ b/infra/stacks/gateway/package.json
@@ -0,0 +1,7 @@
+{
+ "name": "gateway-stack",
+ "version": "0.0.0",
+ "private": true,
+ "license": "MIT",
+ "main": "index.ts"
+}
diff --git a/infra/stacks/gateway/tsconfig.json b/infra/stacks/gateway/tsconfig.json
new file mode 100644
index 0000000000..4082f16a5d
--- /dev/null
+++ b/infra/stacks/gateway/tsconfig.json
@@ -0,0 +1,3 @@
+{
+ "extends": "../../tsconfig.json"
+}