Aws Template Format Version
Aws Template Format Version
Parameters:
Port:
Type: Number
Default: 80
VpcId:
Type: String
Default: vpc-0bf9484fb475a8ee4
PrivateSubnet1:
Type: String
Default: subnet-0b3a1d8dd39bb7f22
PrivateSubnet2:
Type: String
Default: subnet-07cb24f6b5eb7b9ee
AppName:
Type: String
Default: CAD
Description: Application name for the service
ServiceNameui:
Type: String
Default: ui-service
Description: A name for the service
ServiceNamewebApi1:
Type: String
Default: DashBoardservice
Description: A name for the web api service
ServiceNamewebApi2:
Type: String
Default: Adminservice
Description: A name for the web api service
ImageUrlui:
Type: String
Default: 273492721237.dkr.ecr.us-west-2.amazonaws.com/hsbc-cad-angular-
certificate:latest
ImageUrlwebApi1:
Type: String
Default: 273492721237.dkr.ecr.us-west-2.amazonaws.com/hsbccadapi:latest
Description:
The url of a docker image that contains the application process that
will handle the traffic for this service
ImageUrlwebApi2:
Type: String
Default: 273492721237.dkr.ecr.us-west-2.amazonaws.com/hsbccadapi2:latest
Description:
The url of a docker image that contains the application process that
will handle the traffic for this service
ContainerPortUI:
Type: Number
Default: 80
ContainerCpuUI:
Type: Number
Default: 256
Description: How much CPU to give the container. 1024 is 1 CPU
ContainerMemoryUI:
Type: Number
Default: 512
Description: How much memory in megabytes to give the container
ContainerPortwebApi1:
Type: Number
Default: 80
ContainerCpuwebApi1:
Type: Number
Default: 256
Description: How much CPU to give the container. 1024 is 1 CPU
ContainerMemorywebApi1:
Type: Number
Default: 512
Description: How much memory in megabytes to give the container
ContainerPortwebApi2:
Type: Number
Default: 80
ContainerCpuwebApi2:
Type: Number
Default: 256
Description: How much CPU to give the container. 1024 is 1 CPU
ContainerMemorywebApi2:
Type: Number
Default: 512
Description: How much memory in megabytes to give the container
Path:
Type: String
Default: "*"
Description: A path on the public load balancer that this service
should be connected to. Use * to send all load balancer
traffic to this service.
Priority:
Type: Number
Default: 1
Description: The priority for the routing rule added to the load balancer.
This only applies if your have multiple services which have been
assigned to different paths on the load balancer.
DesiredCount:
Type: Number
Default: 2
Description: How many copies of the service task to run
Role:
Type: String
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
HealthCheckPathui:
Type: String
Default: /
HealthCheckPathwebApi1:
Type: String
Default: /RequestDetail
HealthCheckPathwebApi2:
Type: String
Default: /RequestDetail
LoadBalancerHTTPPort:
Type: Number
Default: 80
LoadBalancerHTTPSPort:
Type: Number
Default: 443
CertificateArn:
Type: String
Default: arn:aws:iam::273492721237:server-certificate/hsbcCert
# for autoscaling
MinContainers:
Type: Number
Default: 2
# for autoscaling
MaxContainers:
Type: Number
Default: 10
# target CPU utilization (%)
AutoScalingTargetValue:
Type: Number
Default: 50
ListenerRule1:
Type: 'AWS::ElasticLoadBalancingV2::ListenerRule'
DependsOn:
- LoadBalancerListenerwebApi
- TargetGroupwebApi1
Properties:
Actions:
- Type: forward
TargetGroupArn: !Ref TargetGroupwebApi1
ListenerArn: !Ref LoadBalancerListenerwebApi
Priority: 1
Conditions:
- Field: path-pattern
Values:
- /
ListenerRule2:
Type: 'AWS::ElasticLoadBalancingV2::ListenerRule'
DependsOn:
- LoadBalancerListenerwebApi
- TargetGroupwebApi2
Properties:
Actions:
- Type: forward
TargetGroupArn: !Ref TargetGroupwebApi2
ListenerArn: !Ref LoadBalancerListenerwebApi
Priority: 2
Conditions:
- Field: path-pattern
Values:
- /Admin
ECSServicewebApi1:
Type: AWS::ECS::Service
DependsOn:
- ECSCluster
- ECSTaskDefinitionwebApi1
- ContainerSecurityGroupwebApi1
- TargetGroupwebApi1
- LoadBalancerListenerwebApi
- LoadBalancerHttpsListenerwebApi
- ListenerRule1
Properties:
ServiceName: !Ref ServiceNamewebApi1
Cluster: !Ref ECSCluster
TaskDefinition: !Ref ECSTaskDefinitionwebApi1
DeploymentConfiguration:
MaximumPercent: 200
MinimumHealthyPercent: 100
DesiredCount: 1
LaunchType: FARGATE
NetworkConfiguration:
AwsvpcConfiguration:
AssignPublicIp: ENABLED
Subnets:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
SecurityGroups:
- !Ref ContainerSecurityGroupwebApi1
LoadBalancers:
- ContainerName: !Join ['', [!Ref ServiceNamewebApi1, container]]
ContainerPort: !Ref ContainerPortwebApi1
TargetGroupArn: !Ref TargetGroupwebApi1
ECSTaskDefinitionwebApi2:
Type: AWS::ECS::TaskDefinition
DependsOn: LogGroupwebApi2
Properties:
ContainerDefinitions:
- Name: !Join ['', [!Ref ServiceNamewebApi2, container]]
Image: !Ref ImageUrlwebApi2
PortMappings:
- ContainerPort: !Ref ContainerPortwebApi2
# Setting to stop the task if the container fails
Essential: true
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-region: !Ref AWS::Region
awslogs-group: !Ref LogGroupwebApi2
awslogs-stream-prefix: ecs
Cpu: 256
# A role needed by ECS.
# "The ARN of the task execution role that containers in this task can
assume. All containers in this task are granted the permissions that are
specified in this role."
# "There is an optional task execution IAM role that you can specify with
Fargate to allow your Fargate tasks to make API calls to Amazon ECR."
ExecutionRoleArn: !Ref ExecutionRole
# "The Amazon Resource Name (ARN) of an AWS Identity and Access Management
(IAM) role that grants containers in the task permission to call AWS APIs on your
behalf."
TaskRoleArn: !Ref TaskRole
Family: !Join ["", [!Ref ServiceNamewebApi2, TaskDefinitionwebApi2]]
Memory: 512
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
ContainerSecurityGroupwebApi2:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: !Join ["", [!Ref AppName, "-ecs-cluster",
ContainerSGwebApi2]]
VpcId: !Ref VpcId
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: !Ref ContainerPortwebApi2
ToPort: !Ref ContainerPortwebApi2
SourceSecurityGroupId: !Ref LoadBalancerSecurityGroupwebApi
ECSServicewebApi2:
Type: AWS::ECS::Service
DependsOn:
- ECSCluster
- ECSTaskDefinitionwebApi2
- ContainerSecurityGroupwebApi2
- TargetGroupwebApi2
- LoadBalancerListenerwebApi
- ListenerRule2
Properties:
ServiceName: !Ref ServiceNamewebApi2
Cluster: !Ref ECSCluster
TaskDefinition: !Ref ECSTaskDefinitionwebApi2
DeploymentConfiguration:
MaximumPercent: 200
MinimumHealthyPercent: 100
DesiredCount: 1
LaunchType: FARGATE
NetworkConfiguration:
AwsvpcConfiguration:
AssignPublicIp: ENABLED
Subnets:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
SecurityGroups:
- !Ref ContainerSecurityGroupwebApi2
LoadBalancers:
- ContainerName: !Join ['', [!Ref ServiceNamewebApi2, container]]
ContainerPort: !Ref ContainerPortwebApi2
TargetGroupArn: !Ref TargetGroupwebApi2
ScalableTargetui:
Type: AWS::ApplicationAutoScaling::ScalableTarget
DependsOn:
- "AutoScalingRole"
- "ECSServiceui"
Properties:
RoleARN: !GetAtt AutoScalingRole.Arn
ResourceId:
!Join [
"",
[service/, !Ref AppName, "-ecs-cluster", /, !Ref ServiceNameui],
]
ServiceNamespace: ecs
ScalableDimension: ecs:service:DesiredCount
MinCapacity: 1
MaxCapacity: 5
ScalingPolicyui:
Type: AWS::ApplicationAutoScaling::ScalingPolicy
DependsOn:
- "ScalableTargetui"
Properties:
PolicyName:
!Join ["", [!Ref AppName, !Ref ServiceNameui, -ecs-auto-scaling-policy]]
PolicyType: TargetTrackingScaling
ScalingTargetId: !Ref ScalableTargetui
TargetTrackingScalingPolicyConfiguration:
PredefinedMetricSpecification:
PredefinedMetricType: ECSServiceAverageCPUUtilization
TargetValue: 75.0
ScalableTargetwebApi1:
Type: AWS::ApplicationAutoScaling::ScalableTarget
DependsOn:
- "AutoScalingRole"
- "ECSServicewebApi1"
Properties:
RoleARN: !GetAtt AutoScalingRole.Arn
ResourceId:
!Join [
"",
[service/, !Ref AppName, "-ecs-cluster", /, !Ref ServiceNamewebApi1],
]
ServiceNamespace: ecs
ScalableDimension: ecs:service:DesiredCount
MinCapacity: 1
MaxCapacity: 5
ScalingPolicywebApi1:
Type: AWS::ApplicationAutoScaling::ScalingPolicy
DependsOn:
- "ScalableTargetwebApi1"
Properties:
PolicyName:
!Join ["", [!Ref AppName, !Ref ServiceNamewebApi1, -ecs-auto-scaling-
policy]]
PolicyType: TargetTrackingScaling
ScalingTargetId: !Ref ScalableTargetwebApi1
TargetTrackingScalingPolicyConfiguration:
PredefinedMetricSpecification:
PredefinedMetricType: ECSServiceAverageCPUUtilization
TargetValue: 75.0
ScalingPolicywebApi2:
Type: AWS::ApplicationAutoScaling::ScalingPolicy
DependsOn:
- "ScalableTargetwebApi2"
Properties:
PolicyName:
!Join ["", [!Ref AppName, !Ref ServiceNamewebApi2, -ecs-auto-scaling-
policy]]
PolicyType: TargetTrackingScaling
ScalingTargetId: !Ref ScalableTargetwebApi2
TargetTrackingScalingPolicyConfiguration:
PredefinedMetricSpecification:
PredefinedMetricType: ECSServiceAverageCPUUtilization
TargetValue: 75.0
ScalableTargetwebApi2:
Type: AWS::ApplicationAutoScaling::ScalableTarget
DependsOn:
- "AutoScalingRole"
- "ECSServicewebApi2"
Properties:
RoleARN: !GetAtt AutoScalingRole.Arn
ResourceId:
!Join [
"",
[service/, !Ref AppName, "-ecs-cluster", /, !Ref ServiceNamewebApi2],
]
ServiceNamespace: ecs
ScalableDimension: ecs:service:DesiredCount
MinCapacity: 1
MaxCapacity: 5
AutoScalingRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Join ["", [!Ref AppName, -ECS-service-auto-scaling-role]]
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: [application-autoscaling.amazonaws.com]
Action: ["sts:AssumeRole"]
Policies:
- PolicyName: !Join ["", [!Ref AppName, -ECS-service-auto-scaling-
policy]]
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- ecs:DescribeServices
- ecs:UpdateService
- cloudwatch:PutMetricAlarm
- application-autoscaling:*
- cloudwatch:DescribeAlarms
- cloudwatch:DeleteAlarms
- cloudwatch:GetMetricStatistics
Resource:
- "*"
LogGroupui:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join ["", [/ecs/, !Ref AppName, !Ref ServiceNameui,
Tdefui]]
LogGroupwebApi1:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join ["", [/ecs/, !Ref AppName, !Ref ServiceNamewebApi1,
TdefwebApi1]]
LogGroupwebApi2:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join ["", [/ecs/, !Ref AppName, !Ref ServiceNamewebApi2,
TdefwebApi2]]
TargetGroupwebApi1:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
Name: !Join ["", [!Ref AppName, !Ref ServiceNamewebApi1, TGwebApi1]]
VpcId: !Ref VpcId
Port: !Ref ContainerPortwebApi1
Protocol: HTTP
TargetType: ip
HealthCheckIntervalSeconds: 10
# will look for a 200 status code by default unless specified otherwise
HealthCheckPath: !Ref HealthCheckPathwebApi1
HealthCheckTimeoutSeconds: 5
UnhealthyThresholdCount: 2
HealthyThresholdCount: 2
TargetGroupAttributes:
- Key: deregistration_delay.timeout_seconds
Value: 60 # default is 300
TargetGroupwebApi2:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
Name: !Join ["", [!Ref AppName, !Ref ServiceNamewebApi2, TGwebApi2]]
VpcId: !Ref VpcId
Port: !Ref ContainerPortwebApi2
Protocol: HTTP
TargetType: ip
HealthCheckIntervalSeconds: 10
# will look for a 200 status code by default unless specified otherwise
HealthCheckPath: !Ref HealthCheckPathwebApi2
HealthCheckTimeoutSeconds: 5
UnhealthyThresholdCount: 2
HealthyThresholdCount: 2
TargetGroupAttributes:
- Key: deregistration_delay.timeout_seconds
Value: 60 # default is 300
Outputs:
LoadBalanceruiDNSName:
Value: !GetAtt LoadBalancerui.DNSName
Description: DNSName of the Public Load Balancer
LoadBalancerapiDNSName:
Value: !GetAtt LoadBalancerwebApi.DNSName
Description: DNSName of the internal Load Balancer
ECSClusterId:
Description: The cluster Id
Value: !Ref ECSCluster