Skip to content

Commit

Permalink
feat: resort back to loadbalancer
Browse files Browse the repository at this point in the history
  • Loading branch information
marnixdessing committed Jun 2, 2023
1 parent f6f6daa commit 2578978
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 60 deletions.
28 changes: 6 additions & 22 deletions src/ContainerCluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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 @@ -30,11 +29,10 @@ export class ContainerClusterStack extends Stack {

const hostedzone = this.importHostedZone();
const vpc = this.setupVpc();
const namespace = this.setupCloudMap(vpc);
const listner = this.setupLoadbalancer(vpc, hostedzone);
const cluster =this.constructEcsCluster(vpc);
const vpcLink = new apigatewayv2.VpcLink(this, 'vpc-link', { vpc });
const api = this.setupApiGateway(hostedzone);
this.addIssueService(cluster, namespace, api, vpcLink);
this.addIssueService(cluster, listner, api);
}

setupVpc() {
Expand Down Expand Up @@ -168,46 +166,32 @@ export class ContainerClusterStack extends Stack {

addIssueService(
cluster: ecs.Cluster,
cloudMapNamespace: servicediscovery.PrivateDnsNamespace,
listner: loadbalancing.ApplicationListener,
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 cloudMapService = cloudMapNamespace.createService('yivi-issue-service', {
description: 'CloudMap Service for yivi-issue-service',
dnsTtl: Duration.seconds(10),
name: 'yivi-issue-service',
});

new EcsFargateService(this, 'issue-service', {
serviceName: 'yivi-issue',
containerImage: 'nginxdemos/hello',
repositoryArn: '',
containerPort: 80,
ecsCluster: cluster,
serviceListnerPath: '/*',
listner: listner,
serviceListnerPath: '/irma',
desiredtaskcount: 1,
useSpotInstances: true,
healthCheckPath: '/status',
cloudMapService,
});


// if (!service.service.cloudMapService) {
// throw Error('Cannot create path in API for yivi-issue-service (ECS) as cloudmapService is undefined');
// }

api.addRoutes({
path: '/irma',
methods: [apigatewayv2.HttpMethod.ANY],
integration: new apigatewayv2Integrations.HttpServiceDiscoveryIntegration('api-integration', cloudMapService, {
vpcLink: vpcLink,
}),
integration: new apigatewayv2Integrations.HttpAlbIntegration('api-integration', listner),
});

}
Expand Down
65 changes: 27 additions & 38 deletions src/constructs/EcsFargateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import {
aws_logs as logs,
aws_ecs as ecs,
aws_secretsmanager as secrets,
aws_elasticloadbalancingv2 as loadbalancing,
Duration,
} 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,10 +69,7 @@ export interface EcsFargateServiceProps {
*/
repositoryArn: string;

/**
* The CloudMap namespace to make this container known in
*/
cloudMapService: IService;
listner: loadbalancing.ApplicationListener;
}


Expand All @@ -98,6 +96,8 @@ export class EcsFargateService extends Construct {
const task = this.setupTaskDefinition(logGroup, props);
this.service = this.setupFargateService(task, props);

this.setupLoadbalancerTarget(this.service, props);

}


Expand All @@ -106,28 +106,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 @@ -194,18 +194,7 @@ export class EcsFargateService extends Construct {
vpcSubnets: {
subnetType: SubnetType.PRIVATE_WITH_EGRESS,
},
// cloudMapOptions: {
// cloudMapNamespace: props.cloudMapNamespace,
// containerPort: 80,
// name: `${props.serviceName}-service`,
// dnsTtl: Duration.seconds(10),
// },
});


service.associateCloudMapService({
service: props.cloudMapService,
})

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

0 comments on commit 2578978

Please sign in to comment.