<?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: Shoya Shiraki</title>
    <description>The latest articles on DEV Community by Shoya Shiraki (@shoya).</description>
    <link>https://dev.to/shoya</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%2F320456%2F141ffb12-9cae-49d7-a1f6-d2c32b830acc.jpg</url>
      <title>DEV Community: Shoya Shiraki</title>
      <link>https://dev.to/shoya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shoya"/>
    <language>en</language>
    <item>
      <title>Deploy Node.js + Express on Amazon ECS Fargate with AWS Copilot</title>
      <dc:creator>Shoya Shiraki</dc:creator>
      <pubDate>Thu, 06 Jan 2022 14:13:47 +0000</pubDate>
      <link>https://dev.to/shoya/deploy-nodejs-express-on-amazon-ecs-fargate-with-aws-copilot-22pl</link>
      <guid>https://dev.to/shoya/deploy-nodejs-express-on-amazon-ecs-fargate-with-aws-copilot-22pl</guid>
      <description>&lt;p&gt;Learn how to deploy Node.js &lt;a href="https://expressjs.com/" rel="noopener noreferrer"&gt;Express&lt;/a&gt; to &lt;a href="https://aws.amazon.com/jp/ecs/" rel="noopener noreferrer"&gt;Amazon ECS&lt;/a&gt; Fargate using &lt;a href="https://aws.amazon.com/jp/containers/copilot/" rel="noopener noreferrer"&gt;AWS Copilot&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Enviroments
&lt;/h2&gt;

&lt;p&gt;AWS CLI and Copilot CLI are already installed and configured, and Docker Desktop for Mac is used for local testing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AWS CLI
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ aws --version
aws-cli/2.2.35 Python/3.8.0 Darwin/20.5.0 source/x86_64 prompt/off
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Copilot CLI
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;copilot &lt;span class="nt"&gt;--version&lt;/span&gt;
Copilot version: v1.13.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Docker Engine
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ docker --version
Docker version 20.10.11, build dea9396
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;maOS 11.4&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Setup Express
&lt;/h2&gt;

&lt;p&gt;Create a working directory and set up Express to work with TypeScript.&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="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;copilot-express &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;copilot-express

&lt;span class="nv"&gt;$ &lt;/span&gt;npm init &lt;span class="nt"&gt;-y&lt;/span&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-D&lt;/span&gt; typescript ts-node @types/node @types/node @types/express

&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;express

&lt;span class="nv"&gt;$ &lt;/span&gt;npx tsc &lt;span class="nt"&gt;--init&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create &lt;code&gt;index.ts&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;touch &lt;/span&gt;index.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the created &lt;code&gt;index.ts&lt;/code&gt;, we will start Express and define a GET request handler for &lt;code&gt;/&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Express app works!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Express listening on port &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Add to startup script
&lt;/h3&gt;

&lt;p&gt;Add &lt;code&gt;"start": "ts-node index.ts"&lt;/code&gt; to scripts in package.json.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ts-node index.ts"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create Dockerfile for Express
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;touch &lt;/span&gt;Dockerfile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; node:16&lt;/span&gt;

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /usr/src/app&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; package*.json ./&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;

&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 3000&lt;/span&gt;

&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; [ "npm", "start" ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Docker image build &amp;amp; run
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker build &lt;span class="nt"&gt;-t&lt;/span&gt; copilot-express &lt;span class="nb"&gt;.&lt;/span&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;docker images | &lt;span class="nb"&gt;grep &lt;/span&gt;copilot-express

&lt;span class="nv"&gt;$ &lt;/span&gt;docker run &lt;span class="nt"&gt;-p&lt;/span&gt; 3000:3000 &lt;span class="nt"&gt;-d&lt;/span&gt; copilot-express
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Access to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; and if you see &lt;code&gt;Express app works!&lt;/code&gt; you're ready to go.&lt;/p&gt;




&lt;h2&gt;
  
  
  Deploy to ECS with Copilot
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Copilot concepts
&lt;/h3&gt;

&lt;p&gt;Copilot has three main concepts: Service, Environment, and Application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnvd8u80iv5x9knv8ljfg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnvd8u80iv5x9knv8ljfg.png" alt="Copilot concepts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;"Service" is the application that runs in the container. In this case, we will create a Service named "api".&lt;/p&gt;

&lt;p&gt;"Environment" is an environment such as test/staging/production. In this case, we will create an Environment named "test".&lt;/p&gt;

&lt;p&gt;"Application" is a concept that combines Service and Environment. In this case, we will create an Application with the name "copilot-express".&lt;/p&gt;

&lt;p&gt;In this case, we will have one Service, one Environment, and one Application.&lt;/p&gt;

&lt;p&gt;Learn more: &lt;a href="https://aws.github.io/copilot-cli/docs/concepts/environments/" rel="noopener noreferrer"&gt;https://aws.github.io/copilot-cli/docs/concepts/environments/&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Run &lt;code&gt;copilot init&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Run &lt;code&gt;copilot init&lt;/code&gt; to initialize &amp;amp; deploy.&lt;br&gt;
You will be asked several questions to answer.&lt;/p&gt;
&lt;h4&gt;
  
  
  Type &lt;code&gt;copilot-express&lt;/code&gt; when asked for the Application name.
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;copilot init

Note: It&lt;span class="s1"&gt;'s best to run this command in the root of your Git repository.
Welcome to the Copilot CLI! We'&lt;/span&gt;re going to walk you through some questions
To &lt;span class="nb"&gt;help &lt;/span&gt;you get &lt;span class="nb"&gt;set &lt;/span&gt;up with a containerized application on AWS. An application is a collection of
An application is a collection of containerized services that operate together.

What would you like to name your application? &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="nb"&gt;help&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; copilot-express
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Select &lt;code&gt;Load Balanced Web Service&lt;/code&gt; when asked for the workload type.
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Which workload &lt;span class="nb"&gt;type &lt;/span&gt;best represents your architecture? &lt;span class="k"&gt;for &lt;/span&gt;more &lt;span class="nb"&gt;help&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
  Request-Driven Web Service &lt;span class="o"&gt;(&lt;/span&gt;App Runner&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; Load Balanced Web Service &lt;span class="o"&gt;(&lt;/span&gt;Internet to ECS on Fargate&lt;span class="o"&gt;)&lt;/span&gt;
  Backend Service &lt;span class="o"&gt;(&lt;/span&gt;ECS on Fargate&lt;span class="o"&gt;)&lt;/span&gt;
  Worker Service &lt;span class="o"&gt;(&lt;/span&gt;Events to SQS to ECS on Fargate&lt;span class="o"&gt;)&lt;/span&gt;
  Scheduled Job &lt;span class="o"&gt;(&lt;/span&gt;Scheduled event to State Machine to Fargate&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Type &lt;code&gt;api&lt;/code&gt; when asked for the Service name.
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;What &lt;span class="k"&gt;do &lt;/span&gt;you want to name this service? &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="nb"&gt;help&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Select &lt;code&gt;./Dockerfile&lt;/code&gt; when asked which Dockerfile to use.
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Which Dockerfile would you like to use &lt;span class="k"&gt;for &lt;/span&gt;api? &lt;span class="o"&gt;[&lt;/span&gt;Use arrows to move, &lt;span class="nb"&gt;type &lt;/span&gt;to filter, ? &lt;span class="k"&gt;for &lt;/span&gt;more &lt;span class="nb"&gt;help&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
  &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; ./Dockerfile
    Enter custom path &lt;span class="k"&gt;for &lt;/span&gt;your Dockerfile
    Use an existing image instead
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  This time, we're deploying to a test environment, so enter &lt;code&gt;y&lt;/code&gt;.
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;All right, you&lt;span class="s1"&gt;'re all set for local development.

Would you like to deploy a test environment? for help] (y/N) y
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;After answering the questions, it will start to build the environment with IAM Role, Subnet, VPC, etc. Wait for a while.&lt;/p&gt;

&lt;p&gt;After the environment is built, the Docker image will be pushed to ECR and deployment to ECS will start.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;✔ Linked account AWS_ACCOUNT_ID and region ap-northeast-1 to application copilot-express..

✔ Proposing infrastructure changes &lt;span class="k"&gt;for &lt;/span&gt;the copilot-express-test environment.

✔ Created environment &lt;span class="nb"&gt;test &lt;/span&gt;&lt;span class="k"&gt;in &lt;/span&gt;region ap-northeast-1 under application copilot-express.

✔ Deployed service api.
Recommended follow-up action:
  - You can access your service at http://xxxxx.ap-northeast-1.elb.amazonaws.com over the internet.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Access the URL displayed and if you see &lt;code&gt;Express app works!&lt;/code&gt; as you did locally, deployment is complete!&lt;/p&gt;

&lt;h3&gt;
  
  
  Remove the Copilot Application
&lt;/h3&gt;

&lt;p&gt;After confirming the operation and the status of the application actually being deployed to ECS.&lt;br&gt;
Delete the Application created this time.&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="nv"&gt;$ &lt;/span&gt;copilot app delete
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Once I learned the concept of Copilot (similar to Beanstalk) and the steps, I found it to be a great way to deploy containers to ECS!&lt;/p&gt;

&lt;p&gt;In actual production, Copilot will also include CI/CD and &lt;a href="https://aws.github.io/copilot-cli/docs/developing/sidecars/" rel="noopener noreferrer"&gt;Sidecar pattern&lt;/a&gt;, so I'll be experimenting with that as well.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>ecs</category>
      <category>node</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
