<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Krishna Chaitanya</title>
    <description>The latest articles on DEV Community by Krishna Chaitanya (@chaitan94).</description>
    <link>https://dev.to/chaitan94</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F295205%2F666aceaf-d965-4fce-b23d-0c4f080c5f3b.jpeg</url>
      <title>DEV Community: Krishna Chaitanya</title>
      <link>https://dev.to/chaitan94</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chaitan94"/>
    <language>en</language>
    <item>
      <title>Deploying a gRPC service in ECS with the EC2 launch type</title>
      <dc:creator>Krishna Chaitanya</dc:creator>
      <pubDate>Sun, 01 Nov 2020 16:32:21 +0000</pubDate>
      <link>https://dev.to/chaitan94/deploying-a-grpc-service-in-ecs-with-the-ec2-launch-type-2aa</link>
      <guid>https://dev.to/chaitan94/deploying-a-grpc-service-in-ecs-with-the-ec2-launch-type-2aa</guid>
      <description>&lt;p&gt;The AWS team recently launched end-to-end support for HTTP/2 on Application Load Balancers, as &lt;a href="https://aws.amazon.com/about-aws/whats-new/2020/10/application-load-balancers-enable-grpc-workloads-end-to-end-http-2-support/"&gt;announced on their blog&lt;/a&gt;. This is great news if you want to upgrade your stack to use modern tooling like &lt;a href="https://grpc.io/"&gt;gRPC&lt;/a&gt;. The AWS team has also &lt;a href="https://aws.amazon.com/blogs/opensource/containerize-and-deploy-a-grpc-application-on-aws-fargate/"&gt;published a post&lt;/a&gt; on how to setup and deploy a gRPC application on ECS, using the &lt;strong&gt;Fargate&lt;/strong&gt; launch type. This post aims to explain how to achieve the same using the &lt;strong&gt;EC2&lt;/strong&gt; launch type.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Normally, you might directly go ahead and create a service from the ECS console. This will automatically create a target group for you. But unfortunately, the automatic target group creation does not seem to support gRPC (at least as of Nov 1st 2020). Hence, this blog post.&lt;/p&gt;

&lt;h2&gt;
  
  
  The tl;dr Solution
&lt;/h2&gt;

&lt;p&gt;So instead, we have to follow this flow:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Create Target Group first, with the gRPC protocol
2. Setup the routing in your ALB to let the traffic flow into your target group
3. Create Service using this newly created Target Group
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  The step-by-step Solution
&lt;/h1&gt;

&lt;p&gt;Note: In this post, I will be using golang for the service and client. However, it should be applicable no matter what language you are using for your gRPC service.&lt;/p&gt;

&lt;p&gt;I will be using a modified version of the &lt;a href="https://github.com/grpc/grpc-go/tree/master/examples/helloworld"&gt;Greeter service example from the grpc-go&lt;/a&gt; repository. Complete version of the modified code can be found on my Github: &lt;a href="https://github.com/chaitan94/grpc-on-ecs"&gt;https://github.com/chaitan94/grpc-on-ecs&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/chaitan94/grpc-on-ecs.git
&lt;span class="nb"&gt;cd &lt;/span&gt;grpc-on-ecs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To follow-along with the step-by-step solution, clone the repo, and follow these steps:&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Build the service as a docker image
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Generating the stubs
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;greeter_server

&lt;span class="c"&gt;# We need to generate the gRPC stubs.&lt;/span&gt;
&lt;span class="c"&gt;# Before doing that, make sure you have these installed first:&lt;/span&gt;
&lt;span class="c"&gt;#  - protoc&lt;/span&gt;
&lt;span class="c"&gt;#  - protoc-gen-go&lt;/span&gt;
&lt;span class="c"&gt;#  - protoc-gen-go-grpc&lt;/span&gt;
protoc &lt;span class="nt"&gt;--go_out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;./gen &lt;span class="nt"&gt;--go_opt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;paths&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;source_relative &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;--go-grpc_out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;./gen &lt;span class="nt"&gt;--go-grpc_opt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;paths&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;source_relative &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;-I&lt;/span&gt;../helloworld helloworld.proto

&lt;span class="c"&gt;# Building the server as a docker image&lt;/span&gt;
docker build &lt;span class="nt"&gt;-t&lt;/span&gt; greeter_server &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;(Optional) Step 1.5: Testing locally&lt;/strong&gt;&lt;br&gt;
Run the server image locally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt; greeter_server &lt;span class="nt"&gt;-p&lt;/span&gt; 50051:50051 greeter_server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While the server is running, in a separate terminal, run the client:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;client
go run main.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you can see the message &lt;code&gt;Greeting: Hello world&lt;/code&gt;, you ran everything successfully! :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Deploy the server image to ECR
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create an ECR repository, say with the name 'greeter_server'&lt;/li&gt;
&lt;li&gt;Push your image to ECR (modify accordingly to match your AWS region and ECR repo tag)
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="si"&gt;$(&lt;/span&gt;aws ecr get-login &lt;span class="nt"&gt;--no-include-email&lt;/span&gt; &lt;span class="nt"&gt;--region&lt;/span&gt; ap-south-1&lt;span class="si"&gt;)&lt;/span&gt;
docker tag greeter_server 123123123123.dkr.ecr.ap-south-1.amazonaws.com/greeter_server:latest
docker push 123123123123.dkr.ecr.ap-south-1.amazonaws.com/greeter_server:latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Create a new Task Definition
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Select &lt;strong&gt;EC2&lt;/strong&gt; as the Launch type, and select &lt;strong&gt;Next step&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Provide a &lt;strong&gt;Task Definition Name&lt;/strong&gt;, such as greeter_server.&lt;/li&gt;
&lt;li&gt;Provide the &lt;strong&gt;Task Memory&lt;/strong&gt; and &lt;strong&gt;CPU&lt;/strong&gt; (0.5 GB memory and 0.25 vCPU is sufficient).&lt;/li&gt;
&lt;li&gt;To add a container to the task, select &lt;strong&gt;Add container&lt;/strong&gt; and provide the following parameters:

&lt;ul&gt;
&lt;li&gt;Container name: greeter_server&lt;/li&gt;
&lt;li&gt;Image: 123123123123.dkr.ecr.ap-south-1.amazonaws.com/greeter_server:latest&lt;/li&gt;
&lt;li&gt;Memory Limits (MiB): Soft Limit, 128&lt;/li&gt;
&lt;li&gt;Port mappings: Host port: 0, Container port: 50051, Protocol: tcp&lt;/li&gt;
&lt;li&gt;Choose &lt;strong&gt;Add&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Navigate to the bottom of the page and choose &lt;strong&gt;Create&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 4: Setting up the target group and load balancer
&lt;/h2&gt;

&lt;p&gt;As explained above, we need to create target group manually, otherwise your ECS service won't work over the gRPC protocol.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create Target Group

&lt;ol&gt;
&lt;li&gt;Go to EC2 console -&amp;gt; Target groups.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Create Target Group&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Provide a &lt;strong&gt;Target Group Name&lt;/strong&gt;, such as greeter-server.&lt;/li&gt;
&lt;li&gt;Leave &lt;strong&gt;Protocol&lt;/strong&gt; and &lt;strong&gt;Port&lt;/strong&gt; to the default values, i.e., "HTTP" and "80".&lt;/li&gt;
&lt;li&gt;Set &lt;strong&gt;Protocol version&lt;/strong&gt; to "gRPC". (Note: this is different from the protocol we selected in the previous step)&lt;/li&gt;
&lt;li&gt;In health checks section, set &lt;strong&gt;Health check path&lt;/strong&gt; to "/helloworld.Greeter/".&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Next&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;In the next step, no need to register any targets as ECS would do that for us. Proceed to click "Create target group".&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;Setup your load balancer to receive traffic and forward it to your newly created target group.

&lt;ol&gt;
&lt;li&gt;Go to your ALB's &lt;strong&gt;Listeners&lt;/strong&gt; tab.&lt;/li&gt;
&lt;li&gt;Assuming you are using a HTTPS listener, Click &lt;strong&gt;View/edit rules&lt;/strong&gt;, for the listener "HTTPS : 443". (Note: HTTP listener is not supported by AWS ALB for forwarding gRPC traffic right now.)&lt;/li&gt;
&lt;li&gt;Click the plus icon to add a new rule.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Insert rule&lt;/strong&gt; at an appropriate location.&lt;/li&gt;
&lt;li&gt;Add a path condition with path value = "/helloworld.Greeter*" and an action to forward to the target group "greeter-server".&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Save&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 5: Creating the Service
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;If you don't have a cluster yet, create one.&lt;/li&gt;
&lt;li&gt;In your cluster, under the &lt;strong&gt;Services&lt;/strong&gt; tab, click &lt;strong&gt;Create&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;EC2&lt;/strong&gt; as the Launch type.&lt;/li&gt;
&lt;li&gt;Select the &lt;strong&gt;Task Definition Family&lt;/strong&gt; and &lt;strong&gt;Revision&lt;/strong&gt; from the previous step.&lt;/li&gt;
&lt;li&gt;Provide a Service name, such as greeter_server.&lt;/li&gt;
&lt;li&gt;Set the Number of tasks to 1.&lt;/li&gt;
&lt;li&gt;Choose Next step.&lt;/li&gt;
&lt;li&gt;Create a new security group. (or select an existing one, if you already have one. Make sure 50051 port is allowed in the inbound rules.

&lt;ul&gt;
&lt;li&gt;Choose Edit next to Security groups.&lt;/li&gt;
&lt;li&gt;For Inbound rules for security group, change the Type to Custom TCP and set the Port range to 50051. (Note: This is a bad security posture and only permissible for this demo. Your application should have more strict security rules.)&lt;/li&gt;
&lt;li&gt;Choose Save.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;In the Load Balancing section, select your &lt;strong&gt;Application Load Balancer&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Ensure that "greeter_service:0:50051" is selected, and Click &lt;strong&gt;Add to Load Balancer&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;In &lt;strong&gt;Production listener port&lt;/strong&gt;, Select "443:HTTPS".&lt;/li&gt;
&lt;li&gt;Under &lt;strong&gt;Target group name&lt;/strong&gt;, select the target group created in the previous step. i.e, "greeter-server"&lt;/li&gt;
&lt;li&gt;Choose &lt;strong&gt;Next step&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Choose &lt;strong&gt;Next step&lt;/strong&gt; again to skip auto scaling.&lt;/li&gt;
&lt;li&gt;Choose &lt;strong&gt;Create Service&lt;/strong&gt; to launch the service.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 6: Verify your deployment
&lt;/h2&gt;

&lt;p&gt;You can verify that your service is running properly by connecting to it using the client.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd client
GREETER_ENDPOINT=mydomain.com:443 INSECURE=0 go run main.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you can see the message &lt;code&gt;Greeting: Hello world&lt;/code&gt;, you ran everything successfully! :)&lt;/p&gt;

&lt;p&gt;Note: Here I am assuming you have your domain called mydomain.com mapped to your ALB. You can also use your ALB's hostname directly. If you do not use HTTPS, you can ignore the INSECURE flag while running.&lt;/p&gt;

&lt;p&gt;And that's it. I hope this post helped you in any way!&lt;/p&gt;

&lt;p&gt;Feel free to &lt;a href="https://twitter.com/chaitan94"&gt;DM me on twitter&lt;/a&gt; if you have any feedback or questions :)&lt;/p&gt;

</description>
      <category>grpc</category>
      <category>aws</category>
      <category>ecs</category>
      <category>go</category>
    </item>
  </channel>
</rss>
