diff --git a/iac/Indexer.ts b/iac/Indexer.ts index 90b4527..dad17b5 100644 --- a/iac/Indexer.ts +++ b/iac/Indexer.ts @@ -11,7 +11,8 @@ import { } from "aws-cdk-lib/aws-cloudfront"; import { HttpOrigin } from "aws-cdk-lib/aws-cloudfront-origins"; import { Vpc } from "aws-cdk-lib/aws-ec2"; -import { ApplicationLoadBalancer, ApplicationProtocol } from "aws-cdk-lib/aws-elasticloadbalancingv2"; +import { Cluster, type ICluster } from "aws-cdk-lib/aws-ecs"; +import { ApplicationLoadBalancer } from "aws-cdk-lib/aws-elasticloadbalancingv2"; import { Duration } from "aws-cdk-lib/core"; import { type App, @@ -35,12 +36,18 @@ export function IndexerStack({ app, stack }: StackContext) { natGateways: 1, }); - // Add the indexer service - const indexerService = addIndexerService({ stack, app, vpc }); + // Create the cluster for each services + const cluster = new Cluster(stack, "EcsCluster", { + clusterName: `${stack.stage}-IndexingCluster`, + vpc, + }); // Then add the erpc service const erpcService = addErpcService({ stack, app, vpc }); + // Add the indexer service + const indexerService = addIndexerService({ stack, app, vpc, cluster }); + // If we are missing the fargate services, early exit const indexerFaragateService = indexerService.cdk?.fargateService; const erpcFargateService = erpcService.cdk?.fargateService; @@ -76,11 +83,11 @@ export function IndexerStack({ app, stack }: StackContext) { // todo: add erpc service to the ALB // Add the listener on port 8080 for the rpc /*const erpcListener = alb.addListener("ErpcListener", { - port: 8080, + port: 4001, protocol: ApplicationProtocol.HTTP, }); erpcListener.addTargets("ErpcTarget", { - port: 4000, + port: 4001, protocol: ApplicationProtocol.HTTP, targets: [erpcFargateService], deregistrationDelay: Duration.seconds(30), @@ -141,34 +148,28 @@ export function IndexerStack({ app, stack }: StackContext) { } /** - * Add the indexer service to the stack + * Add the eRPC service to the stack */ -function addIndexerService({ +function addErpcService({ stack, app, vpc, -}: { stack: Stack; app: App; vpc: Vpc }) { + cluster, +}: { stack: Stack; app: App; vpc: Vpc; cluster: ICluster }) { // All the secrets env variable we will be using (in local you can just use a .env file) - const { rpcSecrets, ponderDb } = use(ConfigStack); - const secrets = [...rpcSecrets, ponderDb]; + const { rpcSecrets, erpcDb } = use(ConfigStack); + const secrets = [...rpcSecrets, erpcDb]; // Get our CDK secrets map - const cdkSecretsMap = buildSecretsMap({ stack, secrets, name: "indexer" }); + const cdkSecretsMap = buildSecretsMap({ stack, secrets, name: "erpc" }); // Get the container props of our prebuilt binaries - const indexerImage = getImageFromName({ stack, app, name: "indexer" }); + const erpcImage = getImageFromName({ stack, app, name: "erpc" }); // The service itself - const indexerService = new Service(stack, "IndexerService", { - path: "packages/ponder", - // SST not happy, can't connect to ECR to fetch the instance during the build process - // file: "Dockerfile.prebuilt", - port: 42069, - // Domain mapping - customDomain: { - domainName: "indexer.frak.id", - hostedZone: "frak.id", - }, + const erpcService = new Service(stack, "ErpcService", { + path: "packages/erpc", + port: 4000, // Setup some capacity options scaling: { minContainers: 1, @@ -188,12 +189,11 @@ function addIndexerService({ logRetention: "one_week", // Set the right environment variables environment: { - // Ponder related stuff - PONDER_LOG_LEVEL: "info", - PONDER_TELEMETRY_DISABLED: "true", + ERPC_LOG_LEVEL: "warn", }, cdk: { vpc, + cluster, // Don't auto setup the ALB since we will be using the one from the indexer service // todo: setup the ALB after the indexer service is deployed applicationLoadBalancer: false, @@ -206,47 +206,57 @@ function addIndexerService({ desiredCount: 1, minHealthyPercent: 0, maxHealthyPercent: 100, - // Increase health check grace period - healthCheckGracePeriod: Duration.seconds(120), }, // Directly specify the image position in the registry here container: { - containerName: "indexer", - image: indexerImage, + containerName: "erpc", + image: erpcImage, secrets: cdkSecretsMap, + portMappings: [ + { containerPort: 4000, hostPort: 4000 }, + { containerPort: 4001, hostPort: 4001 }, + ], }, }, }); stack.addOutputs({ - indexerServiceId: indexerService.id, + erpcServiceId: erpcService.id, }); - return indexerService; + return erpcService; } /** - * Add the eRPC service to the stack + * Add the indexer service to the stack */ -function addErpcService({ +function addIndexerService({ stack, app, vpc, -}: { stack: Stack; app: App; vpc: Vpc }) { + cluster, +}: { stack: Stack; app: App; vpc: Vpc; cluster: ICluster }) { // All the secrets env variable we will be using (in local you can just use a .env file) - const { rpcSecrets, erpcDb } = use(ConfigStack); - const secrets = [...rpcSecrets, erpcDb]; + const { rpcSecrets, ponderDb } = use(ConfigStack); + const secrets = [...rpcSecrets, ponderDb]; // Get our CDK secrets map - const cdkSecretsMap = buildSecretsMap({ stack, secrets, name: "erpc" }); + const cdkSecretsMap = buildSecretsMap({ stack, secrets, name: "indexer" }); // Get the container props of our prebuilt binaries - const erpcImage = getImageFromName({ stack, app, name: "erpc" }); + const indexerImage = getImageFromName({ stack, app, name: "indexer" }); // The service itself - const erpcService = new Service(stack, "ErpcService", { - path: "packages/erpc", - port: 4000, + const indexerService = new Service(stack, "IndexerService", { + path: "packages/ponder", + // SST not happy, can't connect to ECR to fetch the instance during the build process + // file: "Dockerfile.prebuilt", + port: 42069, + // Domain mapping + customDomain: { + domainName: "indexer.frak.id", + hostedZone: "frak.id", + }, // Setup some capacity options scaling: { minContainers: 1, @@ -266,10 +276,13 @@ function addErpcService({ logRetention: "one_week", // Set the right environment variables environment: { - ERPC_LOG_LEVEL: "warn", + // Ponder related stuff + PONDER_LOG_LEVEL: "info", + PONDER_TELEMETRY_DISABLED: "true", }, cdk: { vpc, + cluster, // Don't auto setup the ALB since we will be using the one from the indexer service // todo: setup the ALB after the indexer service is deployed applicationLoadBalancer: false, @@ -282,23 +295,21 @@ function addErpcService({ desiredCount: 1, minHealthyPercent: 0, maxHealthyPercent: 100, + // Increase health check grace period + healthCheckGracePeriod: Duration.seconds(120), }, // Directly specify the image position in the registry here container: { - containerName: "erpc", - image: erpcImage, + containerName: "indexer", + image: indexerImage, secrets: cdkSecretsMap, - portMappings: [ - { containerPort: 4000 }, - { containerPort: 4001 }, - ], }, }, }); stack.addOutputs({ - erpcServiceId: erpcService.id, + indexerServiceId: indexerService.id, }); - return erpcService; + return indexerService; }