To configure the gRPC framework for a single microservice, we found that exposing two different ports for a single ECS container is a problem. The gRPC framework and the application have to be run on individual ports, both ports must be exposed for proper functionality in ECS and ALB.
They must be exposed on different ports in the same ECS container for them to work to allow the application and the gRPC framework to operate correctly. This means updating ECS container task definition to identify two ALB target groups, routing both frameworks to the ELB target groups based on the request.
1. Update Dockerfile
FROM python:alpine
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . /app
EXPOSE <application_port> <gRPC_port>
CMD [ "python", "server.py" ]
2. Update ECS Task Definition
"containerDefinitions": [
{
"name": "Container_Name",
"image": "Container_Image_URI",
"portMappings": [
{
"containerPort": 80,
"protocol": "tcp"
},
{
"containerPort": 81
"protocol": "tcp"
}
],
...
}
]
3. Create Two Target Groups in ALB
Create two target groups:
Target Group 1: Using HTTP protocol for a python application.
Target Group 2: Using HTTP/2 protocol for a gRPC framework.
Make sure the second target group has HTTP/2 set as a ProtocolVersion for gRPC compatibility.
4. Set up Listeners and Listener Rules
5. Attach Target Groups to ECS Service
The one ECS service that runs the container is attached to both target groups. This lets users route traffic correctly to the python application and gRPC framework.
6. HTTP/2 Configuration for gRPC framework
For the gRPC framework related second target group, the ProtocolVersion should be set to HTTP2. gRPC target group example CloudFormation configuration:
TargetGroup:
Type: "AWS::ElasticLoadBalancingV2::TargetGroup"
Properties:
Name: grpc-target-group
Protocol: HTTP
Port: 81
VpcId: <your-vpc-id>
ProtocolVersion: HTTP2
HealthCheckProtocol: HTTP
Ready to transform your business with our technology solutions? Contact Us today to Leverage Our DevOps Expertise.
0