<?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>Forem: Nico Duldhardt</title>
    <description>The latest articles on Forem by Nico Duldhardt (@2start).</description>
    <link>https://forem.com/2start</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%2F893842%2Fb0964bc5-e7bc-48de-bdb0-356b869c5b8b.jpeg</url>
      <title>Forem: Nico Duldhardt</title>
      <link>https://forem.com/2start</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/2start"/>
    <language>en</language>
    <item>
      <title>Deploy your containerized application to AWS ECS in 10 minutes using Terraform</title>
      <dc:creator>Nico Duldhardt</dc:creator>
      <pubDate>Fri, 25 Nov 2022 10:56:57 +0000</pubDate>
      <link>https://forem.com/canida/deploy-your-containerized-application-to-aws-ecs-in-10-minutes-using-terraform-4fim</link>
      <guid>https://forem.com/canida/deploy-your-containerized-application-to-aws-ecs-in-10-minutes-using-terraform-4fim</guid>
      <description>&lt;h1&gt;
  
  
  Deploy your containerized application to AWS ECS in 10 minutes using Terraform
&lt;/h1&gt;

&lt;p&gt;This quickstart guide helps you to quickly run a containerized application on AWS. For startups/smaller projects the setup is production ready and easily extensible to fit more advanced needs. All the required resources can be created in a single command by using Terraform. The final setup is depicted below.&lt;/p&gt;

&lt;p&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---IyZLeJ8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/canida-software/terraform-ecs-fargate-spot-quickstart/raw/main/images/overview.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---IyZLeJ8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/canida-software/terraform-ecs-fargate-spot-quickstart/raw/main/images/overview.jpg" width="869" height="813"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  What you will get.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;An application that runs on &lt;a href="https://sample-app.your-domain.com"&gt;https://sample-app.your-domain.com&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;TLS encryption i.e. https works. &lt;/li&gt;
&lt;li&gt;Optional hosting in 2 AZ's for maximum reliability.&lt;/li&gt;
&lt;li&gt;Low price setup by utilizing spot instances.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What you need to bring.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Git Repository Clone: &lt;a href="https://github.com/canida-software/terraform-ecs-fargate-spot-quickstart.git"&gt;terraform-ecs-fargate-spot-quickstart&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Your Containerized Application.&lt;/li&gt;
&lt;li&gt;Terraform Installation&lt;/li&gt;
&lt;li&gt;AWS CLI v2 Installation&lt;/li&gt;
&lt;li&gt;Domain on AWS

&lt;ul&gt;
&lt;li&gt;Alternatively, you can also just create a DNS zone delegation to manage subdomain.your-domain.com via AWS while leaving your-domain.com at your previous DNS provider.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Quickstart.
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Adapt configuration values.
&lt;/h3&gt;

&lt;p&gt;Check out &lt;a href="https://github.com/canida-software/terraform-ecs-fargate-spot-quickstart/blob/main/config/sample-app.tfvars"&gt;config/sample-app.tfvars&lt;/a&gt; &lt;br&gt;
and modify the variables to serve your needs. The variables are also explained in &lt;a href="https://github.com/canida-software/terraform-ecs-fargate-spot-quickstart/blob/main/variables.tf"&gt;variables.tf&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Configure State Backend
&lt;/h3&gt;

&lt;p&gt;You may store the Terraform state in this git repository. To do so comment out the following section in &lt;a href="https://github.com/canida-software/terraform-ecs-fargate-spot-quickstart/blob/main/main.tf"&gt;main.tf&lt;/a&gt; that configures s3 a remote backend.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;terraform {
#  backend "s3" {
#    bucket = "my-terraform-bucket"
#    key    = "app.tfstate"
#    region = "eu-central-1"
#  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to use s3 to store Terraform state create a bucket as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws s3api create-bucket --bucket my-terraform-bucket --region eu-central-1 --create-bucket-configuration LocationConstraint=eu-central-1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Initialize Terraform
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;terraform init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Deploy Application
&lt;/h3&gt;

&lt;p&gt;Run &lt;code&gt;terraform apply&lt;/code&gt; to deploy this setup. Terraform will display the resources that will be created and you can confirm the changes. Creating the resources on AWS takes some time ~10 minutes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;terraform apply --var-file config/sample-app.tfvars
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Verify Setup
&lt;/h3&gt;

&lt;p&gt;Visit &lt;code&gt;sample-app.your-domain.com&lt;/code&gt; to verify that the setup worked. If you deployed the nginx image. It should look as follows:&lt;/p&gt;

&lt;p&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_-46Llv---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/canida-software/terraform-ecs-fargate-spot-quickstart/raw/main/images/nginx.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_-46Llv---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/canida-software/terraform-ecs-fargate-spot-quickstart/raw/main/images/nginx.jpg" width="629" height="257"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Clean Up
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;terraform destroy --var-file config/sample-app.tfvars
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Extending the Setup
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Multiple Apps
&lt;/h3&gt;

&lt;p&gt;You can use Terraform workspaces to manage multiple apps via this terraform module. Let's say you want to manage &lt;code&gt;sample-app1&lt;/code&gt; and &lt;code&gt;sample-app2&lt;/code&gt;. You can create workspaces for each app as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;terraform workspace new sample-app1
terraform workspace new sample-app2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and switch to a workspace using &lt;code&gt;terraform workspace select sample-app1&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Multiple apps in Same ECS Cluster
&lt;/h3&gt;

&lt;p&gt;You can create a second ECS service to host another app. The load balancer costs ~20$ per month, therefore I recommend to share the load balancer between the apps.&lt;/p&gt;

&lt;h3&gt;
  
  
  Database Access
&lt;/h3&gt;

&lt;p&gt;I recommend to use a managed database created by RDS. You can just configure the database url via an environment variable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Information
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Official AWS Quickstart Guide
&lt;/h3&gt;

&lt;p&gt;AWS provides their own quickstart. However, I did not like the quality. That's why I created this setup. Check it out here: &lt;a href="https://github.com/aws-quickstart/terraform-aws-ecs-fargate"&gt;https://github.com/aws-quickstart/terraform-aws-ecs-fargate&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Spot Instance Reliability
&lt;/h3&gt;

&lt;p&gt;If you enable multi-az in the configuration. The app will be deployed across 2 availability zones. I.e. you won't have any downtime even if AWS kills one of your spot instances. The setup will just start another instance and in the meantime traffic is routed to your other replica only. The probability of your spot instance to get killed is &amp;lt;10% for a whole month. For many instances its &amp;lt; 5%. Check out information for specific instance types here: &lt;a href="https://aws.amazon.com/ec2/spot/instance-advisor/"&gt;https://aws.amazon.com/ec2/spot/instance-advisor/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuration Management using 1Password
&lt;/h2&gt;

&lt;p&gt;We use 1password to store the configuration files for Terraform.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create new application
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Note: 1Password allows you to create the same configuration multiple times. If that happens you will have to delete one version o.w. updating the config will fail.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MYAPP=sample-app
op document create --vault demo-apps config/${MYAPP}.tfvars
terraform apply --var-file ${MYAPP}.tfvars
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Update application
&lt;/h3&gt;

&lt;p&gt;First, retrieve the latest app configuration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MYAPP=sample-app
op document get --vault demo-apps ${MYAPP}.tfvars &amp;gt; config/${MYAPP}.tfvars
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, edit the config file locally, update the documents in 1password and apply the changes via Terraform.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;op document edit --vault demo-apps ${MYAPP}.tfvars  config/${MYAPP}.tfvars
terraform apply --var-file ${MYAPP}.tfvars
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Debugging Problems
&lt;/h2&gt;

&lt;p&gt;A good first step is to visit ECS in the management console and check out if the tasks throw any errors.&lt;/p&gt;

</description>
      <category>ecs</category>
      <category>aws</category>
      <category>terraform</category>
      <category>fargate</category>
    </item>
  </channel>
</rss>
