Skip to content

Commit 1b7f1d6

Browse files
committed
fix: ensure dependencies are correctly rendered
1 parent 2341024 commit 1b7f1d6

File tree

6 files changed

+176
-10
lines changed

6 files changed

+176
-10
lines changed

API.md

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/extensions/aliased-port.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as ecs from 'aws-cdk-lib/aws-ecs';
2+
import * as cloudmap from 'aws-cdk-lib/aws-servicediscovery';
23
import { Construct } from 'constructs';
34
import { Service } from '../service';
45
import { Container } from './container';
@@ -47,7 +48,7 @@ export class AliasedPortExtension extends ServiceExtension {
4748
protected alias: string;
4849
protected aliasPort?: number;
4950
protected appProtocol?: ecs.AppProtocol;
50-
protected namespace?: string;
51+
protected namespace?: cloudmap.INamespace;
5152

5253
constructor(props: AliasedPortProps) {
5354
super('aliasedPort');
@@ -62,12 +63,12 @@ export class AliasedPortExtension extends ServiceExtension {
6263
this.scope = scope;
6364

6465
// If there isn't a default cloudmap namespace on the cluster, create a private HTTP namespace for SC.
65-
if (!this.parentService.cluster.defaultCloudMapNamespace) {
66+
if (!this.parentService.environment.cluster.defaultCloudMapNamespace) {
6667
this.parentService.environment.addDefaultCloudMapNamespace({
6768
name: this.parentService.environment.id,
6869
});
6970
}
70-
this.namespace = this.parentService.environment.cluster.defaultCloudMapNamespace?.namespaceName;
71+
this.namespace = this.parentService.environment.cluster.defaultCloudMapNamespace as cloudmap.INamespace;
7172
}
7273

7374
public addHooks(): void {
@@ -84,7 +85,7 @@ export class AliasedPortExtension extends ServiceExtension {
8485
}
8586

8687
public modifyServiceProps(props: ServiceBuild): ServiceBuild {
87-
if (props.serviceConnectConfiguration && props.serviceConnectConfiguration.namespace !== this.namespace) {
88+
if (props.serviceConnectConfiguration && props.serviceConnectConfiguration.namespace !== this.namespace?.namespaceName) {
8889
throw new Error('Service connect cannot be enabled with two different namespaces.');
8990
}
9091

@@ -131,6 +132,13 @@ export class AliasedPortExtension extends ServiceExtension {
131132
},
132133
};
133134
}
135+
136+
public useService(service: ecs.Ec2Service | ecs.FargateService): void {
137+
if (!this.namespace) {
138+
throw new Error('Environment must have a default Cloudmap namespace to enable Service Connect.');
139+
}
140+
service.node.addDependency(this.namespace);
141+
}
134142
}
135143

136144
export interface AliasedPortMutatingHookProps {

src/service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,5 +400,6 @@ export class Service extends Construct {
400400
this.ecsService.enableServiceConnect({
401401
namespace: this.environment.cluster.defaultCloudMapNamespace.namespaceName,
402402
});
403+
this.ecsService.node.addDependency(this.environment.cluster.defaultCloudMapNamespace);
403404
}
404405
}

test/aliased-port.integ.snapshot/aws-ecs-integ.assets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"version": "21.0.0",
33
"files": {
4-
"1904e1ff90c4f297a8a8c9f4f5573101aa4cb632abb96486f141dc588c222614": {
4+
"4ac410b83bf75ad3eee119980f62663b332db0d347a80e1c7a1493bf14772c05": {
55
"source": {
66
"path": "aws-ecs-integ.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "1904e1ff90c4f297a8a8c9f4f5573101aa4cb632abb96486f141dc588c222614.json",
12+
"objectKey": "4ac410b83bf75ad3eee119980f62663b332db0d347a80e1c7a1493bf14772c05.json",
1313
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
1414
}
1515
}

test/aliased-port.integ.snapshot/aws-ecs-integ.template.json

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,10 @@
521521
"TaskDefinition": {
522522
"Ref": "ServiceConnecttaskdefinitionB19E0536"
523523
}
524-
}
524+
},
525+
"DependsOn": [
526+
"productionenvironmentclusterDefaultServiceDiscoveryNamespaceBE74D64D"
527+
]
525528
},
526529
"ServiceConnectserviceSecurityGroup0D1FCAE3": {
527530
"Type": "AWS::EC2::SecurityGroup",
@@ -537,7 +540,143 @@
537540
"VpcId": {
538541
"Ref": "productionenvironmentvpcAEB47DF7"
539542
}
543+
},
544+
"DependsOn": [
545+
"productionenvironmentclusterDefaultServiceDiscoveryNamespaceBE74D64D"
546+
]
547+
},
548+
"ClientServicetaskdefinitionTaskRoleACC19FE3": {
549+
"Type": "AWS::IAM::Role",
550+
"Properties": {
551+
"AssumeRolePolicyDocument": {
552+
"Statement": [
553+
{
554+
"Action": "sts:AssumeRole",
555+
"Effect": "Allow",
556+
"Principal": {
557+
"Service": "ecs-tasks.amazonaws.com"
558+
}
559+
}
560+
],
561+
"Version": "2012-10-17"
562+
}
563+
}
564+
},
565+
"ClientServicetaskdefinition89718CF4": {
566+
"Type": "AWS::ECS::TaskDefinition",
567+
"Properties": {
568+
"ContainerDefinitions": [
569+
{
570+
"Cpu": 256,
571+
"Environment": [
572+
{
573+
"Name": "PORT",
574+
"Value": "80"
575+
},
576+
{
577+
"Name": "URL",
578+
"Value": "http://name"
579+
}
580+
],
581+
"Essential": true,
582+
"Image": "nathanpeck/greeter",
583+
"Memory": 512,
584+
"Name": "app",
585+
"PortMappings": [
586+
{
587+
"ContainerPort": 80,
588+
"Protocol": "tcp"
589+
}
590+
],
591+
"Ulimits": [
592+
{
593+
"HardLimit": 1024000,
594+
"Name": "nofile",
595+
"SoftLimit": 1024000
596+
}
597+
]
598+
}
599+
],
600+
"Cpu": "256",
601+
"Family": "awsecsintegClientServicetaskdefinition11AA48A8",
602+
"Memory": "512",
603+
"NetworkMode": "awsvpc",
604+
"RequiresCompatibilities": [
605+
"EC2",
606+
"FARGATE"
607+
],
608+
"TaskRoleArn": {
609+
"Fn::GetAtt": [
610+
"ClientServicetaskdefinitionTaskRoleACC19FE3",
611+
"Arn"
612+
]
613+
}
540614
}
615+
},
616+
"ClientServiceserviceService8F3E675C": {
617+
"Type": "AWS::ECS::Service",
618+
"Properties": {
619+
"Cluster": {
620+
"Ref": "productionenvironmentclusterC6599D2D"
621+
},
622+
"DeploymentConfiguration": {
623+
"MaximumPercent": 200,
624+
"MinimumHealthyPercent": 100
625+
},
626+
"DesiredCount": 1,
627+
"EnableECSManagedTags": false,
628+
"LaunchType": "FARGATE",
629+
"NetworkConfiguration": {
630+
"AwsvpcConfiguration": {
631+
"AssignPublicIp": "DISABLED",
632+
"SecurityGroups": [
633+
{
634+
"Fn::GetAtt": [
635+
"ClientServiceserviceSecurityGroupFBB92787",
636+
"GroupId"
637+
]
638+
}
639+
],
640+
"Subnets": [
641+
{
642+
"Ref": "productionenvironmentvpcPrivateSubnet1Subnet53F632E6"
643+
},
644+
{
645+
"Ref": "productionenvironmentvpcPrivateSubnet2Subnet756FB93C"
646+
}
647+
]
648+
}
649+
},
650+
"ServiceConnectConfiguration": {
651+
"Enabled": true,
652+
"Namespace": "production"
653+
},
654+
"TaskDefinition": {
655+
"Ref": "ClientServicetaskdefinition89718CF4"
656+
}
657+
},
658+
"DependsOn": [
659+
"productionenvironmentclusterDefaultServiceDiscoveryNamespaceBE74D64D"
660+
]
661+
},
662+
"ClientServiceserviceSecurityGroupFBB92787": {
663+
"Type": "AWS::EC2::SecurityGroup",
664+
"Properties": {
665+
"GroupDescription": "aws-ecs-integ/ClientService-service/SecurityGroup",
666+
"SecurityGroupEgress": [
667+
{
668+
"CidrIp": "0.0.0.0/0",
669+
"Description": "Allow all outbound traffic by default",
670+
"IpProtocol": "-1"
671+
}
672+
],
673+
"VpcId": {
674+
"Ref": "productionenvironmentvpcAEB47DF7"
675+
}
676+
},
677+
"DependsOn": [
678+
"productionenvironmentclusterDefaultServiceDiscoveryNamespaceBE74D64D"
679+
]
541680
}
542681
},
543682
"Parameters": {

test/aliased-port.integ.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,27 @@ aliasedPortServiceDescription.add(new AliasedPortExtension({
2828
}));
2929

3030
new Service(stack, 'ServiceConnect', {
31-
environment: environment,
31+
environment,
3232
serviceDescription: aliasedPortServiceDescription,
3333
desiredCount: 1,
3434
});
35+
36+
const otherDescription = new ServiceDescription();
37+
38+
otherDescription.add(new Container({
39+
cpu: 256,
40+
memoryMiB: 512,
41+
trafficPort: 80,
42+
image: ecs.ContainerImage.fromRegistry('nathanpeck/greeter'),
43+
environment: {
44+
PORT: '80',
45+
URL: 'http://name',
46+
},
47+
}));
48+
49+
new Service(stack, 'ClientService', {
50+
environment,
51+
serviceDescription: otherDescription,
52+
desiredCount: 1,
53+
enableServiceConnect: true,
54+
});

0 commit comments

Comments
 (0)