From 82a713603ff1d2247197aeb12c4713b8d294ba5f Mon Sep 17 00:00:00 2001 From: Marnix Dessing Date: Fri, 2 Jun 2023 17:49:51 +0200 Subject: [PATCH] feat: back to service discovery --- src/ContainerCluster.ts | 22 +++++++++--- src/constructs/EcsFargateService.ts | 56 ++++++++++++++++------------- 2 files changed, 48 insertions(+), 30 deletions(-) diff --git a/src/ContainerCluster.ts b/src/ContainerCluster.ts index ab45d4d..67f0360 100644 --- a/src/ContainerCluster.ts +++ b/src/ContainerCluster.ts @@ -14,6 +14,7 @@ import { aws_iam as iam, aws_apigatewayv2 as cdkApigatewayV2, aws_logs as logs, + Duration, } from 'aws-cdk-lib'; import { Construct } from 'constructs'; import { Configurable } from './Configuration'; @@ -29,10 +30,11 @@ export class ContainerClusterStack extends Stack { const hostedzone = this.importHostedZone(); const vpc = this.setupVpc(); - const listner = this.setupLoadbalancer(vpc, hostedzone); + const namespace = this.setupCloudMap(vpc); const cluster =this.constructEcsCluster(vpc); const api = this.setupApiGateway(hostedzone); - this.addIssueService(cluster, listner, api); + const vpcLink = new apigatewayv2.VpcLink(this, 'vpc-link', { vpc }); + this.addIssueService(cluster, namespace, api, vpcLink); } setupVpc() { @@ -166,8 +168,9 @@ export class ContainerClusterStack extends Stack { addIssueService( cluster: ecs.Cluster, - listner: loadbalancing.ApplicationListener, + namespace: servicediscovery.PrivateDnsNamespace, api: apigatewayv2.HttpApi, + vpcLink: apigatewayv2.VpcLink, ) { // const region = props.configuration.deployFromEnvironment.region; @@ -175,25 +178,34 @@ export class ContainerClusterStack extends Stack { // const branch = props.configuration.branchName; // const ecrRepositoryArn = `arn:aws:ecr:${region}:${account}:repository/yivi-issue-server-${branch}`; + const cloudMapsService = namespace.createService('yivi-issue-service', { + description: 'CloudMap for yivi-issue-service', + dnsRecordType: servicediscovery.DnsRecordType.SRV, // Only supported + dnsTtl: Duration.seconds(10), // Max 10 seconds downtime? + }); + new EcsFargateService(this, 'issue-service', { serviceName: 'yivi-issue', containerImage: 'nginxdemos/hello', repositoryArn: '', containerPort: 80, ecsCluster: cluster, - listner: listner, serviceListnerPath: '/irma', desiredtaskcount: 1, useSpotInstances: true, healthCheckPath: '/status', + cloudMapsService, }); api.addRoutes({ path: '/irma', methods: [apigatewayv2.HttpMethod.ANY], - integration: new apigatewayv2Integrations.HttpAlbIntegration('api-integration', listner), + integration: new apigatewayv2Integrations.HttpServiceDiscoveryIntegration('api-integration', cloudMapsService, { + vpcLink, + }), }); + } createYiviKey() { diff --git a/src/constructs/EcsFargateService.ts b/src/constructs/EcsFargateService.ts index cf7a95b..73015a9 100644 --- a/src/constructs/EcsFargateService.ts +++ b/src/constructs/EcsFargateService.ts @@ -2,9 +2,9 @@ import { aws_logs as logs, aws_ecs as ecs, aws_secretsmanager as secrets, - aws_elasticloadbalancingv2 as loadbalancing, } from 'aws-cdk-lib'; import { SubnetType } from 'aws-cdk-lib/aws-ec2'; +import { IService } from 'aws-cdk-lib/aws-servicediscovery'; import { Construct } from 'constructs'; export interface EcsFargateServiceProps { @@ -68,7 +68,9 @@ export interface EcsFargateServiceProps { */ repositoryArn: string; - listner: loadbalancing.ApplicationListener; + // listner: loadbalancing.ApplicationListener; + + cloudMapsService: IService; } @@ -95,7 +97,7 @@ export class EcsFargateService extends Construct { const task = this.setupTaskDefinition(logGroup, props); this.service = this.setupFargateService(task, props); - this.setupLoadbalancerTarget(this.service, props); + //this.setupLoadbalancerTarget(this.service, props); } @@ -105,28 +107,28 @@ export class EcsFargateService extends Construct { * @param service * @param props */ - setupLoadbalancerTarget(service: ecs.FargateService, props: EcsFargateServiceProps) { - - const conditions = [ - loadbalancing.ListenerCondition.pathPatterns([props.serviceListnerPath]), - ]; - - props.listner.addTargets(`${props.serviceName}-target`, { - port: props.containerPort, - protocol: loadbalancing.ApplicationProtocol.HTTP, - targets: [service], - conditions, - priority: 10, - healthCheck: { - enabled: true, - path: props.healthCheckPath, - // healthyHttpCodes: '200', - // healthyThresholdCount: 2, - // unhealthyThresholdCount: 6, - // timeout: Duration.seconds(10), - // interval: Duration.seconds(15), - }, - }); + setupLoadbalancerTarget(_service: ecs.FargateService, _props: EcsFargateServiceProps) { + + // const conditions = [ + // loadbalancing.ListenerCondition.pathPatterns([props.serviceListnerPath]), + // ]; + + // props.listner.addTargets(`${props.serviceName}-target`, { + // port: props.containerPort, + // protocol: loadbalancing.ApplicationProtocol.HTTP, + // targets: [service], + // conditions, + // priority: 10, + // healthCheck: { + // enabled: true, + // path: props.healthCheckPath, + // // healthyHttpCodes: '200', + // // healthyThresholdCount: 2, + // // unhealthyThresholdCount: 6, + // // timeout: Duration.seconds(10), + // // interval: Duration.seconds(15), + // }, + // }); } @@ -195,6 +197,10 @@ export class EcsFargateService extends Construct { }, }); + service.associateCloudMapService({ + service: props.cloudMapsService, + }); + service.node.addDependency(props.ecsCluster); return service; }