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 {
DATADOG_API_KEY,
DEFAULT_CONTINUE_BEFORE_STEADY_STATE,
EcsDeploymentFailureAlarm,
withTelemetry,
ServiceTargetGroup,
} from '../../packages/resources';
import { EcrImage } from '../../packages/service';
import {
CLOUD_TRAIL_SNS_TOPIC_ARN,
DopplerEcsEnvironment,
GatewayService,
getGatewayAlb,
stack,
} from '../../packages/shared';
const gatewayLoadBalancer = getGatewayAlb();
const BASE_NAME = pulumi.getProject();
const REPO_ROOT = '../../..';
type CreateNotificationServiceArgs = {
cloudStorageClusterName: pulumi.Output<string> | string;
ecsClusterArn: pulumi.Output<string> | string;
vpc: {
vpcId: pulumi.Output<string> | string;
privateSubnetIds: pulumi.Output<string[]> | string[];
};
platform: { family: string; architecture: 'amd64' | 'arm64' };
serviceContainerPort: number;
containerEnvVars: { name: string; value: pulumi.Output<string> | string }[];
healthCheckPath: string;
secretKeyArns: (pulumi.Output<string> | string)[];
queueArns: Array<pulumi.Output<string> | string>;
snsPlatformArns: Array<pulumi.Output<string> | string>;
extraManagedPolicyArns?: Array<pulumi.Output<string> | string>;
tags: { [key: string]: string };
};
export class NotificationService extends pulumi.ComponentResource {
public role: aws.iam.Role;
public ecr: awsx.ecr.Repository;
public serviceSg: aws.ec2.SecurityGroup;
public targetGroup: aws.lb.TargetGroup;
public service: awsx.ecs.FargateService;
public cloudStorageClusterName: pulumi.Output<string> | string;
public tags: { [key: string]: string };
constructor(
name: string,
{
ecsClusterArn,
vpc,
platform,
serviceContainerPort,
healthCheckPath,
containerEnvVars,
cloudStorageClusterName,
queueArns,
snsPlatformArns,
extraManagedPolicyArns = [],
secretKeyArns,
tags,
}: CreateNotificationServiceArgs,
opts?: pulumi.ComponentResourceOptions
) {
super('my:components:CloudStorageService', name, {}, opts);
this.tags = tags;
this.cloudStorageClusterName = cloudStorageClusterName;
// role
const secretsManagerPolicy = new aws.iam.Policy(
`${BASE_NAME}-secrets-manager-policy`,
{
name: `${BASE_NAME}-secrets-manager-policy-${stack}`,
policy: {
Version: '2012-10-17',
Statement: [
{
Action: [
'secretsmanager:GetSecretValue',
'secretsmanager:DescribeSecret',
],
Resource: [...secretKeyArns],
Effect: 'Allow',
},
],
},
tags: this.tags,
},
{ parent: this }
);
const queuePolicy = new aws.iam.Policy(
`${BASE_NAME}-sqs-policy`,
{
name: `${BASE_NAME}-sqs-policy-${stack}`,
policy: {
Version: '2012-10-17',
Statement: [
{
Action: ['sqs:*'],
Resource: queueArns,
Effect: 'Allow',
},
],
},
tags: this.tags,
},
{ parent: this }
);
const sesPolicy = new aws.iam.Policy(
`${BASE_NAME}-ses-policy`,
{
name: `${BASE_NAME}-ses-policy-${stack}`,
policy: {
Version: '2012-10-17',
Statement: [
{
Action: [
'ses:SendEmail',
'ses:SendRawEmail',
'ses:SendTemplatedEmail',
],
// A send that names a configuration set (SES_CONFIGURATION_SET)
// is also authorized against that set.
Resource: [
`arn:aws:ses:us-east-1:569036502058:identity/notification.macro.com`,
`arn:aws:ses:us-east-1:569036502058:configuration-set/*`,
],
Effect: 'Allow',
},
],
},
tags: this.tags,
},
{ parent: this }
);
const snsPolicy = new aws.iam.Policy(
`${BASE_NAME}-sns-policy`,
{
name: `${BASE_NAME}-sns-policy-${stack}`,
policy: {
Version: '2012-10-17',
Statement: [
{
Action: [
'sns:CreatePlatformEndpoint',
'sns:DeleteEndpoint',
'sns:GetEndpointAttributes',
'sns:SetEndpointAttributes',
'sns:Publish',
],
Resource: snsPlatformArns,
Effect: 'Allow',
},
],
},
tags: this.tags,
},
{ parent: this }
);
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: [
secretsManagerPolicy.arn,
queuePolicy.arn,
sesPolicy.arn,
snsPolicy.arn,
...extraManagedPolicyArns,
],
},
{ parent: this }
);
// ecr image
const image = new EcrImage(
`${BASE_NAME}-ecr-image-${stack}`,
{
repositoryId: `${BASE_NAME}-ecr-${stack}`,
repositoryName: `${BASE_NAME}-${stack}`,
imageId: `${BASE_NAME}-image-${stack}`,
imagePath: REPO_ROOT,
dockerfile: 'docker/Dockerfile',
platform,
buildArgs: {
SERVICE_NAME: 'notification_service',
},
tags: this.tags,
},
{ parent: this }
);
this.ecr = image.ecr;
// sg
this.serviceSg = this.initializeSecurityGroups({ vpcId: vpc.vpcId });
const gatewayTargetGroup = new ServiceTargetGroup(
`${stack}-${BASE_NAME}`,
{
tags: this.tags,
listenerArn: gatewayLoadBalancer.httpsListenerArn,
vpcId: vpc.vpcId,
containerPort: serviceContainerPort,
service: GatewayService.NOTIFICATION_SERVICE,
healthCheckPath,
pathPatterns: ['/notification', '/notification/*'],
serviceSecurityGroupId: this.serviceSg.id,
albSecurityGroupId: gatewayLoadBalancer.albSecurityGroupId,
},
{ parent: this }
);
this.targetGroup = gatewayTargetGroup.target_group;
const dopplerEcsEnvironment = new DopplerEcsEnvironment(
BASE_NAME,
{ tags: this.tags },
{ parent: this }
);
// service
const service = new awsx.ecs.FargateService(
`${BASE_NAME}`,
{
tags,
cluster: ecsClusterArn,
networkConfiguration: {
subnets: vpc.privateSubnetIds,
securityGroups: [this.serviceSg.id],
},
continueBeforeSteadyState: DEFAULT_CONTINUE_BEFORE_STEADY_STATE,
deploymentCircuitBreaker: {
enable: true,
rollback: true,
},
// Register tasks only with the shared gateway.
loadBalancers: [
{
targetGroupArn: gatewayTargetGroup.target_group.arn,
containerName: 'service',
containerPort: serviceContainerPort,
},
],
taskDefinitionArgs: {
taskRole: {
roleArn: this.role.arn,
},
executionRole: {
roleArn: dopplerEcsEnvironment.executionRole.arn,
},
containers: withTelemetry(BASE_NAME, {
service: {
name: BASE_NAME,
image: image.image.imageUri,
stopTimeout: 10, // 10 seconds to force kill the task
cpu: stack === 'prod' ? 1024 : 256,
memory: stack === 'prod' ? 2048 : 512,
environment: [...(containerEnvVars ?? [])],
secrets: [...dopplerEcsEnvironment.containerSecrets],
logConfiguration: {
logDriver: 'awsfirelens',
options: {
Name: 'datadog',
Host: 'http-intake.logs.us5.datadoghq.com',
apikey: DATADOG_API_KEY,
dd_service: 'notification-service',
dd_source: 'fargate',
dd_tags: `project:cloudstorage, env:${stack}`,
provider: 'ecs',
},
},
portMappings: [
{
appProtocol: 'http',
name: `${BASE_NAME}-tcp-${stack}`,
hostPort: serviceContainerPort,
containerPort: serviceContainerPort,
targetGroup: this.targetGroup,
},
],
},
}),
runtimePlatform: {
operatingSystemFamily: `${platform.family.toUpperCase()}`,
cpuArchitecture: `${
platform.architecture === 'amd64'
? 'X86_64'
: platform.architecture.toUpperCase()
}`,
},
},
desiredCount: 1,
},
{
parent: this,
// ECS refuses a service whose target group is not yet associated with
// a load balancer; it is the listener rule that creates that
// association
dependsOn: [gatewayTargetGroup.listener_rule],
}
);
this.service = service;
this.setupAutoScaling();
this.setupServiceAlarms();
}
initializeSecurityGroups({
vpcId,
}: {
vpcId: pulumi.Output<string> | string;
}) {
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.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 }
);
return serviceSg;
}
setupAutoScaling() {
if (!this.service) return;
const serviceScalableTarget = new aws.appautoscaling.Target(
`${BASE_NAME}-service-scalable-target-${stack}`,
{
maxCapacity: stack === 'prod' ? 10 : 3,
minCapacity: 1,
resourceId: pulumi.interpolate`service/${this.cloudStorageClusterName}/${this.service.service.name}`,
scalableDimension: 'ecs:service:DesiredCount',
serviceNamespace: 'ecs',
tags: this.tags,
},
{ parent: this }
);
const resourceLabel = pulumi.interpolate`${gatewayLoadBalancer.albArnSuffix}/${this.targetGroup.arnSuffix}`;
// 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 }
);
}
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: 80,
comparisonOperator: 'GreaterThanThreshold',
dimensions: {
ClusterName: this.cloudStorageClusterName,
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: 80,
comparisonOperator: 'GreaterThanThreshold',
dimensions: {
ClusterName: this.cloudStorageClusterName,
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}`,
// Target-generated errors can be scoped to this service on the shared ALB.
metricName: 'HTTPCode_Target_5XX_Count',
namespace: 'AWS/ApplicationELB',
statistic: 'Sum',
period: 180,
evaluationPeriods: 1,
threshold: 25,
comparisonOperator: 'GreaterThanOrEqualToThreshold',
dimensions: {
LoadBalancer: gatewayLoadBalancer.albArnSuffix,
TargetGroup: this.targetGroup.arnSuffix,
},
alarmDescription: `High HTTP 5XX count alarm for ${BASE_NAME} gateway target group.`,
actionsEnabled: true,
alarmActions: [CLOUD_TRAIL_SNS_TOPIC_ARN],
tags: this.tags,
},
{ parent: this }
);
}
}