Skip to content

Commit

Permalink
feat: back to service discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
marnixdessing committed Jun 2, 2023
1 parent 68219be commit 82a7136
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 30 deletions.
22 changes: 17 additions & 5 deletions src/ContainerCluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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() {
Expand Down Expand Up @@ -166,34 +168,44 @@ 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;
// const account = props.configuration.deployFromEnvironment.account;
// 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() {
Expand Down
56 changes: 31 additions & 25 deletions src/constructs/EcsFargateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -68,7 +68,9 @@ export interface EcsFargateServiceProps {
*/
repositoryArn: string;

listner: loadbalancing.ApplicationListener;
// listner: loadbalancing.ApplicationListener;

cloudMapsService: IService;
}


Expand All @@ -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);

}

Expand All @@ -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),
// },
// });
}


Expand Down Expand Up @@ -195,6 +197,10 @@ export class EcsFargateService extends Construct {
},
});

service.associateCloudMapService({
service: props.cloudMapsService,
});

service.node.addDependency(props.ecsCluster);
return service;
}
Expand Down

0 comments on commit 82a7136

Please sign in to comment.