From 093b9b27a674dd7969c0e506e5221bb1cf3f702c Mon Sep 17 00:00:00 2001 From: Cedric Ziel Date: Tue, 12 May 2020 09:51:23 +0200 Subject: [PATCH] Use service registry for discovery --- aws-ecs-ec2/services.yaml | 227 ++++++++++++++++++++++++++++++++++---- 1 file changed, 204 insertions(+), 23 deletions(-) diff --git a/aws-ecs-ec2/services.yaml b/aws-ecs-ec2/services.yaml index 55ad90a..db4ce12 100644 --- a/aws-ecs-ec2/services.yaml +++ b/aws-ecs-ec2/services.yaml @@ -59,7 +59,7 @@ Parameters: Default: nginx Description: The url of a docker image that contains the application process that will handle the traffic for this service - ContainerPort: + WebContainerPort: Type: Number Default: 8080 Description: What port number the application inside the docker container is binding to @@ -136,11 +136,23 @@ Parameters: Default: "" Description: (Optional) An IAM role to give the service's containers if the code within needs to access other AWS resources like S3 buckets, DynamoDB tables, etc + WebLogGroup: + Type: String + Default: "rs-web" Conditions: HasCustomRole: !Not [ !Equals [!Ref 'Role', ''] ] Resources: + Registry: + Type: AWS::ServiceDiscovery::PrivateDnsNamespace + Properties: + Description: RobotShop registry for ecs + Name: robot-shop + Vpc: + Fn::ImportValue: + !Join [':', [!Ref 'StackName', 'VPCId']] + # MongoDB service MongoDbTaskDefinition: Type: AWS::ECS::TaskDefinition @@ -157,7 +169,7 @@ Resources: - Name: !Ref 'MongoDbServiceName' Cpu: !Ref 'ContainerCpu' Memory: !Ref 'ContainerMemory' - Image: robotshop/rs-mongodb + Image: robotshop/rs-mongodb:0.4.17 PortMappings: - ContainerPort: !Ref 'MongoDbContainerPort' LogConfiguration: @@ -245,6 +257,7 @@ Resources: Family: !Ref 'CatalogueServiceName' Cpu: !Ref 'ContainerCpu' Memory: !Ref 'ContainerMemory' + NetworkMode: awsvpc TaskRoleArn: Fn::If: - 'HasCustomRole' @@ -254,21 +267,45 @@ Resources: - Name: !Ref 'CatalogueServiceName' Cpu: !Ref 'ContainerCpu' Memory: !Ref 'ContainerMemory' - Image: robotshop/rs-catalogue + Image: robotshop/rs-catalogue:0.4.17 PortMappings: - ContainerPort: !Ref 'CatalogueContainerPort' LogConfiguration: LogDriver: json-file CatalogueService: Type: AWS::ECS::Service - DependsOn: LoadBalancerRule + DependsOn: [LoadBalancerRule, CatalogueRegistryService] Properties: ServiceName: !Ref 'CatalogueServiceName' + NetworkConfiguration: + AwsvpcConfiguration: + Subnets: + - Fn::ImportValue: + !Join [':', [!Ref 'StackName', 'PrivateSubnetOne']] + - Fn::ImportValue: + !Join [':', [!Ref 'StackName', 'PrivateSubnetTwo']] + ServiceRegistries: + - ContainerName: !Ref 'CatalogueServiceName' + ContainerPort: !Ref 'CatalogueContainerPort' + RegistryArn: !GetAtt CatalogueRegistryService.Arn Cluster: Fn::ImportValue: !Join [':', [!Ref 'StackName', 'ClusterName']] DesiredCount: 1 TaskDefinition: !Ref 'CatalogueTaskDefinition' + CatalogueRegistryService: + Type: AWS::ServiceDiscovery::Service + DependsOn: Registry + Properties: + Name: !Ref 'CatalogueServiceName' + DnsConfig: + NamespaceId: !GetAtt Registry.Id + DnsRecords: + - TTL: 10 + Type: SRV + - TTL: 10 + Type: A + RoutingPolicy: WEIGHTED # user service UserTaskDefinition: @@ -278,6 +315,7 @@ Resources: Family: !Ref 'UserServiceName' Cpu: !Ref 'ContainerCpu' Memory: !Ref 'ContainerMemory' + NetworkMode: awsvpc TaskRoleArn: Fn::If: - 'HasCustomRole' @@ -287,21 +325,45 @@ Resources: - Name: !Ref 'UserServiceName' Cpu: !Ref 'ContainerCpu' Memory: !Ref 'ContainerMemory' - Image: robotshop/rs-user + Image: robotshop/rs-user:0.4.17 PortMappings: - ContainerPort: !Ref 'UserContainerPort' LogConfiguration: LogDriver: json-file UserService: Type: AWS::ECS::Service - DependsOn: LoadBalancerRule + DependsOn: [LoadBalancerRule, UserRegistryService] Properties: ServiceName: !Ref 'UserServiceName' + NetworkConfiguration: + AwsvpcConfiguration: + Subnets: + - Fn::ImportValue: + !Join [':', [!Ref 'StackName', 'PrivateSubnetOne']] + - Fn::ImportValue: + !Join [':', [!Ref 'StackName', 'PrivateSubnetTwo']] + ServiceRegistries: + - ContainerName: !Ref 'UserServiceName' + ContainerPort: !Ref 'UserContainerPort' + RegistryArn: !GetAtt UserRegistryService.Arn Cluster: Fn::ImportValue: !Join [':', [!Ref 'StackName', 'ClusterName']] DesiredCount: 1 TaskDefinition: !Ref 'UserTaskDefinition' + UserRegistryService: + Type: AWS::ServiceDiscovery::Service + DependsOn: Registry + Properties: + Name: !Ref 'UserServiceName' + DnsConfig: + NamespaceId: !GetAtt Registry.Id + DnsRecords: + - TTL: 10 + Type: SRV + - TTL: 10 + Type: A + RoutingPolicy: WEIGHTED # cart service CartTaskDefinition: @@ -311,6 +373,7 @@ Resources: Family: !Ref 'CartServiceName' Cpu: !Ref 'ContainerCpu' Memory: !Ref 'ContainerMemory' + NetworkMode: awsvpc TaskRoleArn: Fn::If: - 'HasCustomRole' @@ -320,21 +383,45 @@ Resources: - Name: !Ref 'CartServiceName' Cpu: !Ref 'ContainerCpu' Memory: !Ref 'ContainerMemory' - Image: robotshop/rs-cart + Image: robotshop/rs-cart:0.4.17 PortMappings: - ContainerPort: !Ref 'CartContainerPort' LogConfiguration: LogDriver: json-file CartService: Type: AWS::ECS::Service - DependsOn: LoadBalancerRule + DependsOn: [LoadBalancerRule, CartRegistryService] Properties: ServiceName: !Ref 'CartServiceName' + NetworkConfiguration: + AwsvpcConfiguration: + Subnets: + - Fn::ImportValue: + !Join [':', [!Ref 'StackName', 'PrivateSubnetOne']] + - Fn::ImportValue: + !Join [':', [!Ref 'StackName', 'PrivateSubnetTwo']] + ServiceRegistries: + - ContainerName: !Ref 'CartServiceName' + ContainerPort: !Ref 'CartContainerPort' + RegistryArn: !GetAtt CartRegistryService.Arn Cluster: Fn::ImportValue: !Join [':', [!Ref 'StackName', 'ClusterName']] DesiredCount: 1 TaskDefinition: !Ref 'CartTaskDefinition' + CartRegistryService: + Type: AWS::ServiceDiscovery::Service + DependsOn: Registry + Properties: + Name: !Ref 'CartServiceName' + DnsConfig: + NamespaceId: !GetAtt Registry.Id + DnsRecords: + - TTL: 10 + Type: SRV + - TTL: 10 + Type: A + RoutingPolicy: WEIGHTED # mysql service MySqlTaskDefinition: @@ -356,7 +443,7 @@ Resources: Capabilities: Add: - NET_ADMIN - Image: robotshop/rs-mysql-db + Image: robotshop/rs-mysql-db:0.4.17 PortMappings: - ContainerPort: !Ref 'MySqlContainerPort' LogConfiguration: @@ -380,6 +467,7 @@ Resources: Family: !Ref 'ShippingServiceName' Cpu: !Ref 'ContainerCpu' Memory: !Ref 'ContainerMemory' + NetworkMode: awsvpc TaskRoleArn: Fn::If: - 'HasCustomRole' @@ -389,21 +477,45 @@ Resources: - Name: !Ref 'ShippingServiceName' Cpu: !Ref 'ContainerCpu' Memory: !Ref 'ContainerMemory' - Image: robotshop/rs-shipping + Image: robotshop/rs-shipping:0.4.17 PortMappings: - ContainerPort: !Ref 'ShippingContainerPort' LogConfiguration: LogDriver: json-file ShippingService: Type: AWS::ECS::Service - DependsOn: LoadBalancerRule + DependsOn: [LoadBalancerRule, ShippingRegistryService] Properties: ServiceName: !Ref 'ShippingServiceName' + NetworkConfiguration: + AwsvpcConfiguration: + Subnets: + - Fn::ImportValue: + !Join [':', [!Ref 'StackName', 'PrivateSubnetOne']] + - Fn::ImportValue: + !Join [':', [!Ref 'StackName', 'PrivateSubnetTwo']] + ServiceRegistries: + - ContainerName: !Ref 'ShippingServiceName' + ContainerPort: !Ref 'ShippingContainerPort' + RegistryArn: !GetAtt ShippingRegistryService.Arn Cluster: Fn::ImportValue: !Join [':', [!Ref 'StackName', 'ClusterName']] DesiredCount: 1 TaskDefinition: !Ref 'ShippingTaskDefinition' + ShippingRegistryService: + Type: AWS::ServiceDiscovery::Service + DependsOn: Registry + Properties: + Name: !Ref 'ShippingServiceName' + DnsConfig: + NamespaceId: !GetAtt Registry.Id + DnsRecords: + - TTL: 10 + Type: SRV + - TTL: 10 + Type: A + RoutingPolicy: WEIGHTED # ratings service RatingsTaskDefinition: @@ -413,6 +525,7 @@ Resources: Family: !Ref 'RatingsServiceName' Cpu: !Ref 'ContainerCpu' Memory: !Ref 'ContainerMemory' + NetworkMode: awsvpc TaskRoleArn: Fn::If: - 'HasCustomRole' @@ -422,21 +535,45 @@ Resources: - Name: !Ref 'RatingsServiceName' Cpu: !Ref 'ContainerCpu' Memory: !Ref 'ContainerMemory' - Image: robotshop/rs-ratings + Image: robotshop/rs-ratings:0.4.17 PortMappings: - ContainerPort: !Ref 'RatingsContainerPort' LogConfiguration: LogDriver: json-file RatingsService: Type: AWS::ECS::Service - DependsOn: LoadBalancerRule + DependsOn: [LoadBalancerRule, RatingsRegistryService] Properties: ServiceName: !Ref 'RatingsServiceName' + NetworkConfiguration: + AwsvpcConfiguration: + Subnets: + - Fn::ImportValue: + !Join [':', [!Ref 'StackName', 'PrivateSubnetOne']] + - Fn::ImportValue: + !Join [':', [!Ref 'StackName', 'PrivateSubnetTwo']] + ServiceRegistries: + - ContainerName: !Ref 'RatingsServiceName' + ContainerPort: !Ref 'RatingsContainerPort' + RegistryArn: !GetAtt RatingsRegistryService.Arn Cluster: Fn::ImportValue: !Join [':', [!Ref 'StackName', 'ClusterName']] DesiredCount: 1 TaskDefinition: !Ref 'RatingsTaskDefinition' + RatingsRegistryService: + Type: AWS::ServiceDiscovery::Service + DependsOn: Registry + Properties: + Name: !Ref 'RatingsServiceName' + DnsConfig: + NamespaceId: !GetAtt Registry.Id + DnsRecords: + - TTL: 10 + Type: SRV + - TTL: 10 + Type: A + RoutingPolicy: WEIGHTED # payment service PaymentTaskDefinition: @@ -446,6 +583,7 @@ Resources: Family: !Ref 'PaymentServiceName' Cpu: !Ref 'ContainerCpu' Memory: !Ref 'ContainerMemory' + NetworkMode: awsvpc TaskRoleArn: Fn::If: - 'HasCustomRole' @@ -455,22 +593,47 @@ Resources: - Name: !Ref 'PaymentServiceName' Cpu: !Ref 'ContainerCpu' Memory: !Ref 'ContainerMemory' - Image: robotshop/rs-payment + Image: robotshop/rs-payment:0.4.17 PortMappings: - ContainerPort: !Ref 'PaymentContainerPort' LogConfiguration: LogDriver: json-file PaymentService: Type: AWS::ECS::Service - DependsOn: LoadBalancerRule + DependsOn: [LoadBalancerRule, PaymentRegistryService] Properties: ServiceName: !Ref 'PaymentServiceName' + NetworkConfiguration: + AwsvpcConfiguration: + Subnets: + - Fn::ImportValue: + !Join [':', [!Ref 'StackName', 'PrivateSubnetOne']] + - Fn::ImportValue: + !Join [':', [!Ref 'StackName', 'PrivateSubnetTwo']] + ServiceRegistries: + - ContainerName: !Ref 'PaymentServiceName' + ContainerPort: !Ref 'PaymentContainerPort' + RegistryArn: !GetAtt PaymentRegistryService.Arn Cluster: Fn::ImportValue: !Join [':', [!Ref 'StackName', 'ClusterName']] DesiredCount: 1 TaskDefinition: !Ref 'PaymentTaskDefinition' + PaymentRegistryService: + Type: AWS::ServiceDiscovery::Service + DependsOn: Registry + Properties: + Name: !Ref 'PaymentServiceName' + DnsConfig: + NamespaceId: !GetAtt Registry.Id + DnsRecords: + - TTL: 10 + Type: SRV + - TTL: 10 + Type: A + RoutingPolicy: WEIGHTED + # dispatch service DispatchTaskDefinition: Type: AWS::ECS::TaskDefinition @@ -488,7 +651,7 @@ Resources: - Name: !Ref 'DispatchServiceName' Cpu: !Ref 'ContainerCpu' Memory: !Ref 'ContainerMemory' - Image: robotshop/rs-dispatch + Image: robotshop/rs-dispatch:0.4.17 PortMappings: - ContainerPort: !Ref 'DispatchContainerPort' LogConfiguration: @@ -519,14 +682,32 @@ Resources: - Name: !Ref 'WebServiceName' Cpu: !Ref 'ContainerCpu' Memory: !Ref 'ContainerMemory' - Image: robotshop/rs-web + Image: robotshop/rs-web:0.4.17 + Environment: + - Name: CATALOGUE_HOST + Value: catalogue.robot-shop + - Name: USER_HOST + Value: user.robot-shop + - Name: CART_HOST + Value: cart.robot-shop + - Name: SHIPPING_HOST + Value: shipping.robot-shop + - Name: PAYMENT_HOST + Value: payment.robot-shop + - Name: RATINGS_HOST + Value: ratings.robot-shop PortMappings: - - ContainerPort: !Ref 'ContainerPort' + - ContainerPort: !Ref 'WebContainerPort' LogConfiguration: - LogDriver: json-file + LogDriver: awslogs + Options: + awslogs-create-group: true + awslogs-region: !Ref AWS::Region + awslogs-group: !Ref WebLogGroup + awslogs-stream-prefix: ecs WebService: Type: AWS::ECS::Service - DependsOn: LoadBalancerRule + DependsOn: ['LoadBalancerRule', 'CatalogueService'] Properties: ServiceName: !Ref 'WebServiceName' Cluster: @@ -539,7 +720,7 @@ Resources: TaskDefinition: !Ref 'WebTaskDefinition' LoadBalancers: - ContainerName: !Ref 'WebServiceName' - ContainerPort: !Ref 'ContainerPort' + ContainerPort: !Ref 'WebContainerPort' TargetGroupArn: !Ref 'WebTargetGroup' WebTargetGroup: Type: AWS::ElasticLoadBalancingV2::TargetGroup @@ -550,7 +731,7 @@ Resources: HealthCheckTimeoutSeconds: 5 HealthyThresholdCount: 2 Name: !Ref 'WebServiceName' - Port: 8080 + Port: !Ref 'WebContainerPort' Protocol: HTTP UnhealthyThresholdCount: 2 VpcId: @@ -567,5 +748,5 @@ Resources: Values: [!Ref 'Path'] ListenerArn: Fn::ImportValue: - !Join [':', [!Ref 'StackName', 'PrivateListener']] + !Join [':', [!Ref 'StackName', 'PublicListener']] Priority: !Ref 'Priority'