Hi I am creating ecs cluster using cloudformation, I am having trouble in configuring the task and Application load balancer port as my instance gets unhealthy. I might be giving the wrong port, please do point out the mistake. I am only attaching the templte where I think the error is.
Task template:
---
AWSTemplateFormatVersion: 2010-09-09
Parameters:
ExRole:
Type: String
RDS:
Type: String
Dbname:
Type: String
dbpassword:
Type: String
containerName:
Type: String
taskFamily:
Type: String
Resources:
Task:
Type: AWS::ECS::TaskDefinition
Properties:
Family: !Ref taskFamily
Cpu: 1 vCPU
ExecutionRoleArn: !Ref ExRole
Memory: 1 GB
NetworkMode: bridge
RequiresCompatibilities:
- EC2
TaskRoleArn: !Ref ExRole
ContainerDefinitions:
- Essential: true
Image: wordpress:latest
Name: !Ref containerName
PortMappings:
- ContainerPort: 80
HostPort: 0
Protocol: tcp
Environment:
- Name: WORDPRESS_DB_HOST
Value: !Ref RDS
- Name: WORDPRESS_DB_USER
Value: !Ref Dbname
- Name: WORDPRESS_DB_PASSWORD
Value: !Ref dbpassword
- Name: WORDPRESS_DB_NAME
Value: !Ref Dbname
Outputs:
Task:
Description: Contains all the task specifications
Value: !Ref Task
Export:
Name: !Sub "${AWS::StackName}-Task"
Application load balancer:
---
AWSTemplateFormatVersion: 2010-09-09
Parameters:
SubnetA:
Type: String
SubnetB:
Type: String
VpcID:
Type: String
Env:
Type: String
Resources:
Albsg:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: !Sub "albsg-${Env}"
VpcId: !Ref VpcID
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
Description: For traffic from Internet
GroupDescription: Security Group for demo server
Alb:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
IpAddressType: ipv4
Name: !Sub "Alb-${Env}"
Scheme: internet-facing
SecurityGroups:
- !Ref Albsg
Subnets:
- Ref: "SubnetA"
- Ref: "SubnetB"
Type: application
DefaultTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
DependsOn: Alb
Properties:
Name: !Sub "Albtg-${Env}"
VpcId: !Ref VpcID
Port: 80
Protocol: HTTP
Matcher:
HttpCode: 302
LoadBalancerListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
LoadBalancerArn: !Ref Alb
Port: 80
Protocol: HTTP
DefaultActions:
- Type: forward
TargetGroupArn: !Ref DefaultTargetGroup
Outputs:
Albsg:
Description: security group for application load balancer
Value: !Ref Albsg
Export:
Name: !Sub "${AWS::StackName}-Albsg"
Alb:
Description: application load balancer
Value: !Ref Alb
Export:
Name: !Sub "${AWS::StackName}-Alb"
DefaultTargetGroup:
Description: Default Target Group
Value: !Ref DefaultTargetGroup
Export:
Name: !Sub "${AWS::StackName}-DefaultTargetGroup"
My instance is in draining state and goes unhealthy, see this screenshot:
The 301 is that accessing the ECS host on its target health check path is performing a redirect (hence the 301). To fix either update the path in the health check to one that won't redirect or instead update the health checks status code(s) to include a 301
. This can be done as a single status code (301
) or as a range (200-399
).