Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
import * as aws from '@pulumi/aws';
import * as awsx from '@pulumi/awsx';
import * as pulumi from '@pulumi/pulumi';
import {
DEFAULT_CONTINUE_BEFORE_STEADY_STATE,
EcsDeploymentFailureAlarm,
} from '../../packages/resources';
import { serviceLoadBalancer } from './resources/load_balancer';
import {
BASE_DOMAIN,
CLOUD_TRAIL_SNS_TOPIC_ARN,
config,
stack,
} from './resources/shared';
const BASE_NAME = 'fusionauth';
export const DOMAIN_NAME =
stack === 'prod'
? `auth.${BASE_DOMAIN}`
: `fusionauth-${stack}.${BASE_DOMAIN}`;
type CreateFusionAuthServiceArgs = {
clusterName: pulumi.Output<string> | string;
clusterArn: pulumi.Output<string> | string;
vpc: {
vpcId: pulumi.Output<string> | string;
publicSubnetIds: pulumi.Output<string[]> | string[];
privateSubnetIds: pulumi.Output<string[]> | string[];
};
platform: { family: string; architecture: 'amd64' | 'arm64' };
serviceContainerPort: number;
isPrivate?: boolean;
containerEnvVars: { name: string; value: pulumi.Output<string> | string }[];
healthCheckPath: string;
tags: { [key: string]: string };
};
export class FusionAuthService extends pulumi.ComponentResource {
public role: aws.iam.Role;
public serviceAlbSg: aws.ec2.SecurityGroup;
public serviceSg: aws.ec2.SecurityGroup;
public targetGroup: aws.lb.TargetGroup;
public lb: aws.lb.LoadBalancer;
public listener: aws.lb.Listener;
public service: awsx.ecs.FargateService;
public domain: string;
public clusterName: pulumi.Output<string> | string;
public tags: { [key: string]: string };
constructor(
name: string,
{
clusterArn,
vpc,
platform,
serviceContainerPort,
healthCheckPath,
isPrivate,
containerEnvVars,
clusterName,
tags,
}: CreateFusionAuthServiceArgs,
opts?: pulumi.ComponentResourceOptions
) {
super('my:components:FusionAuthService', name, {}, opts);
this.tags = tags;
this.clusterName = clusterName;
this.domain = `https://${DOMAIN_NAME}`;
// role
this.role = new aws.iam.Role(
`${BASE_NAME}-role`,
{
name: `${BASE_NAME}-role-${stack}`,
assumeRolePolicy: {
Version: '2012-10-17',
Statement: [
{
Action: 'sts:AssumeRole',
Principal: {
Service: 'ecs-tasks.amazonaws.com',
},
Effect: 'Allow',
Sid: '',
},
],
},
tags: this.tags,
managedPolicyArns: [],
},
{ parent: this }
);
// sg
const sg = this.initializeSecurityGroups({
vpcId: vpc.vpcId,
serviceContainerPort,
});
this.serviceAlbSg = sg.serviceAlbSg;
this.serviceSg = sg.serviceSg;
// lb
const { targetGroup, lb, listener } = serviceLoadBalancer(this, {
serviceName: BASE_NAME, // service name
serviceContainerPort,
healthCheckPath,
vpc,
albSecurityGroupId: this.serviceAlbSg.id,
isPrivate,
tags,
});
this.targetGroup = targetGroup;
this.lb = lb;
this.listener = listener;
// service
containerEnvVars.push({
name: 'FUSIONAUTH_APP_URL',
value: pulumi.interpolate`${this.domain}`,
});
const service = new awsx.ecs.FargateService(
`${BASE_NAME}`,
{
tags,
cluster: clusterArn,
networkConfiguration: {
subnets: vpc.privateSubnetIds,
securityGroups: [this.serviceSg.id],
},
continueBeforeSteadyState: DEFAULT_CONTINUE_BEFORE_STEADY_STATE,
deploymentCircuitBreaker: {
enable: true,
rollback: true,
},
taskDefinitionArgs: {
taskRole: {
roleArn: this.role.arn,
},
containers: {
service: {
name: BASE_NAME,
image: config.require('image'),
cpu: stack === 'prod' ? 2048 : 2048,
memory: stack === 'prod' ? 4096 : 4096,
environment: containerEnvVars,
portMappings: [
{
appProtocol: 'http',
name: `${BASE_NAME}-tcp-${stack}`,
hostPort: serviceContainerPort,
containerPort: serviceContainerPort,
targetGroup,
},
],
},
},
runtimePlatform: {
operatingSystemFamily: `${platform.family.toUpperCase()}`,
cpuArchitecture: `${
platform.architecture === 'amd64'
? 'X86_64'
: platform.architecture.toUpperCase()
}`,
},
},
desiredCount: stack === 'prod' ? 2 : 1,
},
{
parent: this,
}
);
this.service = service;
this.setupServiceAlarms();
// domain record
const zone = aws.route53.getZoneOutput({ name: BASE_DOMAIN });
new aws.route53.Record(
`${BASE_NAME}-domain-record`,
{
name: DOMAIN_NAME,
type: 'A',
zoneId: zone.zoneId,
aliases: [
{
evaluateTargetHealth: false,
name: this.lb.dnsName,
zoneId: this.lb.zoneId,
},
],
},
{ parent: this }
);
// We only setup auto scaling for prod
if (stack === 'prod') {
this.setupAutoScaling();
}
}
initializeSecurityGroups({
vpcId,
serviceContainerPort,
}: {
vpcId: pulumi.Output<string> | string;
serviceContainerPort: number;
}) {
const serviceAlbSg = new aws.ec2.SecurityGroup(
`${BASE_NAME}-alb-sg-${stack}`,
{
name: `${BASE_NAME}-alb-sg-${stack}`,
description: `${BASE_NAME} application load balancer security group`,
vpcId,
tags: this.tags,
},
{ parent: this }
);
const serviceSg = new aws.ec2.SecurityGroup(
`${BASE_NAME}-sg-${stack}`,
{
name: `${BASE_NAME}-sg-${stack}`,
vpcId,
description: `${BASE_NAME} security group that is attached directly to the service`,
tags: this.tags,
},
{ parent: this }
);
new aws.vpc.SecurityGroupIngressRule(
`${BASE_NAME}-alb-in`,
{
securityGroupId: serviceSg.id,
description: 'Allow inbound traffic from the services ALB',
referencedSecurityGroupId: serviceAlbSg.id,
fromPort: serviceContainerPort,
toPort: serviceContainerPort,
ipProtocol: 'tcp',
tags: this.tags,
},
{ parent: this }
);
new aws.vpc.SecurityGroupEgressRule(
`${BASE_NAME}-all-out`,
{
securityGroupId: serviceSg.id,
description: 'Allow all outbound',
cidrIpv4: '0.0.0.0/0',
ipProtocol: '-1',
tags: this.tags,
},
{ parent: this }
);
// ALB SG rules
new aws.vpc.SecurityGroupIngressRule(
`${BASE_NAME}-http`,
{
securityGroupId: serviceAlbSg.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(
`${BASE_NAME}-https`,
{
securityGroupId: serviceAlbSg.id,
description: 'Allow inbound HTTPS traffic',
cidrIpv4: '0.0.0.0/0',
fromPort: 443,
ipProtocol: 'tcp',
toPort: 443,
tags: this.tags,
},
{ parent: this }
);
new aws.vpc.SecurityGroupEgressRule(
`${BASE_NAME}-out-service`,
{
description: 'Allow traffic to the service security group',
securityGroupId: serviceAlbSg.id,
referencedSecurityGroupId: serviceSg.id,
fromPort: serviceContainerPort,
ipProtocol: 'tcp',
toPort: serviceContainerPort,
tags: this.tags,
},
{ parent: this }
);
return { serviceAlbSg, serviceSg };
}
setupServiceAlarms() {
new EcsDeploymentFailureAlarm(
`${BASE_NAME}-deployment-failure-alarm`,
{
serviceName: BASE_NAME,
serviceArn: this.service.service.arn,
tags: this.tags,
},
{ parent: this }
);
new aws.cloudwatch.MetricAlarm(
`${BASE_NAME}-high-cpu-alarm`,
{
name: `${BASE_NAME}-high-cpu-alarm-${stack}`,
metricName: 'CPUUtilization',
namespace: 'AWS/ECS',
statistic: 'Average',
period: 180,
evaluationPeriods: 1,
threshold: 70,
comparisonOperator: 'GreaterThanThreshold',
dimensions: {
ClusterName: this.clusterName,
ServiceName: this.service.service.name,
},
alarmDescription: `High CPU usage alarm for ${BASE_NAME} service.`,
actionsEnabled: true,
alarmActions: [CLOUD_TRAIL_SNS_TOPIC_ARN],
tags: this.tags,
},
{ parent: this }
);
new aws.cloudwatch.MetricAlarm(
`${BASE_NAME}-high-mem-alarm`,
{
name: `${BASE_NAME}-high-mem-alarm-${stack}`,
metricName: 'MemoryUtilization',
namespace: 'AWS/ECS',
statistic: 'Average',
period: 180,
evaluationPeriods: 1,
threshold: 70,
comparisonOperator: 'GreaterThanThreshold',
dimensions: {
ClusterName: this.clusterName,
ServiceName: this.service.service.name,
},
alarmDescription: `High Memory usage alarm for ${BASE_NAME} service.`,
actionsEnabled: true,
alarmActions: [CLOUD_TRAIL_SNS_TOPIC_ARN],
tags: this.tags,
},
{ parent: this }
);
new aws.cloudwatch.MetricAlarm(
`${BASE_NAME}-http-5xx-alarm`,
{
name: `${BASE_NAME}-http-5xx-${stack}`,
metricName: 'HTTPCode_ELB_5XX_Count',
namespace: 'AWS/ApplicationELB',
statistic: 'Sum',
period: 180,
evaluationPeriods: 1,
threshold: 25,
comparisonOperator: 'GreaterThanOrEqualToThreshold',
dimensions: {
LoadBalancer: this.lb.arn,
},
alarmDescription: `High HTTP 5XX count alarm for ${BASE_NAME} Load Balancer.`,
actionsEnabled: true,
alarmActions: [CLOUD_TRAIL_SNS_TOPIC_ARN],
tags: this.tags,
},
{ parent: this }
);
}
setupAutoScaling() {
if (!this.service) return;
if (stack !== 'prod') return;
const serviceScalableTarget = new aws.appautoscaling.Target(
`${BASE_NAME}-service-scalable-target-${stack}`,
{
maxCapacity: stack === 'prod' ? 5 : 3,
minCapacity: stack === 'prod' ? 2 : 1,
resourceId: pulumi.interpolate`service/${this.clusterName}/${this.service.service.name}`,
scalableDimension: 'ecs:service:DesiredCount',
serviceNamespace: 'ecs',
tags: this.tags,
},
{ parent: this }
);
const lbPortion: pulumi.Output<string> = this.lb.arn.apply((arn) => {
const parts = arn.split(':loadbalancer/');
return parts[1];
});
const tgPortion: pulumi.Output<string> = this.targetGroup.arn.apply(
(arn) => {
const parts = arn.split(':');
return parts[parts.length - 1];
}
);
const resourceLabel = pulumi.interpolate`${lbPortion}/${tgPortion}`;
// Create an Auto Scaling policy for request count.
new aws.appautoscaling.Policy(
`${BASE_NAME}-scaling-policy-request-count-${stack}`,
{
policyType: 'TargetTrackingScaling',
resourceId: serviceScalableTarget.resourceId,
scalableDimension: serviceScalableTarget.scalableDimension,
serviceNamespace: serviceScalableTarget.serviceNamespace,
targetTrackingScalingPolicyConfiguration: {
targetValue: 1000, // TODO: play with this
predefinedMetricSpecification: {
predefinedMetricType: 'ALBRequestCountPerTarget',
resourceLabel,
},
scaleInCooldown: 60,
scaleOutCooldown: 120,
},
},
{ parent: this }
);
// Create an Auto Scaling policy for CPU utilization.
new aws.appautoscaling.Policy(
`${BASE_NAME}-scaling-policy-cpu-${stack}`,
{
policyType: 'TargetTrackingScaling',
resourceId: serviceScalableTarget.resourceId,
scalableDimension: serviceScalableTarget.scalableDimension,
serviceNamespace: serviceScalableTarget.serviceNamespace,
targetTrackingScalingPolicyConfiguration: {
targetValue: 70.0,
predefinedMetricSpecification: {
predefinedMetricType: 'ECSServiceAverageCPUUtilization',
},
scaleInCooldown: 100,
scaleOutCooldown: 300,
},
},
{ parent: this }
);
new aws.appautoscaling.Policy(
`${BASE_NAME}-scaling-policy-memory-${stack}`,
{
policyType: 'TargetTrackingScaling',
resourceId: serviceScalableTarget.resourceId,
scalableDimension: serviceScalableTarget.scalableDimension,
serviceNamespace: serviceScalableTarget.serviceNamespace,
targetTrackingScalingPolicyConfiguration: {
targetValue: 70.0,
predefinedMetricSpecification: {
predefinedMetricType: 'ECSServiceAverageMemoryUtilization',
},
scaleInCooldown: 100,
scaleOutCooldown: 300,
},
},
{ parent: this }
);
}
}