<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jully Achenchi on Medium]]></title>
        <description><![CDATA[Stories by Jully Achenchi on Medium]]></description>
        <link>https://medium.com/@jullyachenchi8?source=rss-7a6e9f200238------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*VmzA_yZfKIfBnEU2PH8KaQ.jpeg</url>
            <title>Stories by Jully Achenchi on Medium</title>
            <link>https://medium.com/@jullyachenchi8?source=rss-7a6e9f200238------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 16:11:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jullyachenchi8/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Refactoring in Terraform — A practical hands-on example.]]></title>
            <link>https://medium.com/@jullyachenchi8/refactoring-in-terraform-a-practical-hands-on-example-801381adc70b?source=rss-7a6e9f200238------2</link>
            <guid isPermaLink="false">https://medium.com/p/801381adc70b</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[terraform-modules]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[terraform-associate-exam]]></category>
            <dc:creator><![CDATA[Jully Achenchi]]></dc:creator>
            <pubDate>Tue, 29 Sep 2026 16:57:18 GMT</pubDate>
            <atom:updated>2026-09-29T16:57:18.293Z</atom:updated>
            <content:encoded><![CDATA[<h4>How to decouple your Terraform code</h4><p>Terraform has become the go-to tool for provisioning and managing cloud resources. Over time, your Terraform codebase can become complex, error-prone, and difficult to maintain. This is where Terraform code refactoring steps in, offering a path to cleaner, more efficient, and maintainable infrastructure code.</p><p><a href="https://medium.com/@jullyachenchi8/how-to-deploy-a-react-and-go-application-on-aws-ecs-and-s3-using-terraform-de4fc8932a77">In an earlier article</a>, I talked about deploying a Go API and React Frontend on Amazon ECS EC2 using Terraform. If you had a look at it, you’d notice that I bundled all the infrastructure code into a monolith configuration file, save for variables and outputs.</p><p>In this article, we are going to use that mono-file to demonstrate how to successfully do Terraform refactoring.</p><p>This is what we will cover:</p><ul><li>Prerequisites</li><li>What is code refactoring</li><li>Benefits of code refactoring</li><li>Refactoring techniques for Terraform</li><li>How to do code refactoring — A practical example</li><li>Conclusion</li></ul><h3>Prerequisites</h3><p>To get the most out of this hands-on article, you will need:</p><ul><li>Terraform installed</li><li>AWS CLI installed and configured</li><li>Existing Terraform code</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JIl8ABxTlEiDIqEUeKmiHQ.png" /><figcaption>A visual representation of Terraform code refactoring</figcaption></figure><h3>What is code refactoring?</h3><p>Code refactoring is the process of restructuring existing code without changing its external behaviour. In the context of Terraform, it means making improvements to your infrastructure code to improve its readability, maintainability, scalability, and overall performance while preserving its intended functionality.</p><h3>Benefits of Terraform code refactoring</h3><ol><li><strong>Enhanced Readability and Maintainability: </strong>Code that’s easy to read and understand is also easy to maintain and troubleshoot. Refactoring can make your Terraform configurations more human-friendly.</li><li><strong>Reduced Duplication:</strong> Code duplication leads to inconsistencies and makes updates cumbersome. Refactoring helps eliminate redundancy, resulting in cleaner, DRY (Don’t Repeat Yourself) code.</li><li><strong>Improved Reusability:</strong> Well-refactored Terraform code can be organized into modules, making it easier to reuse across projects and collaborate with others.</li><li><strong>Future-Proofing:</strong> As your infrastructure evolves, refactored code can adapt more easily to changes in requirements and infrastructure providers.</li><li><strong>Streamlined Collaboration</strong>: Well-organised code enables smoother collaboration and knowledge sharing within teams.</li></ol><h3>Refactoring Techniques in Terraform</h3><p>When dealing with a sprawling, monolithic Terraform codebase, refactoring generally happens in progressive phases:</p><ol><li><strong>File-Level Segregation (Domain Decomposition):</strong> Breaking apart a massive main.tf into domain-specific .tf files (e.g., vpc.tf, ecs.tf, database.tf). Since Terraform automatically combines all .tf files in a directory into one unified graph, this step requires <strong>zero state migration</strong>.</li><li><strong>Module Extraction:</strong> Encapsulating cohesive infrastructure tiers into self-contained, reusable modules (e.g., extracting DocumentDB, ECS compute, or CloudFront distributions).</li><li><strong>State-Safe Migration (</strong><strong>moved blocks):</strong> When moving existing resources into modules or renaming resource blocks, utilizing Terraform’s declarative moved blocks ensures Terraform re-indexes the state instead of <strong>destroying and recreating</strong> live resources.</li></ol><h3>How to do code refactoring — A practical example</h3><p>Let’s dive into refactoring our monolithic main.tf from the <a href="https://medium.com/@jullyachenchi8/how-to-deploy-a-react-and-go-application-on-aws-ecs-and-s3-using-terraform-de4fc8932a77">earlier article</a> into a modular, production-ready architecture.</p><h3>Step 1: The Monolith vs. Target Modular Structure</h3><p>Here is how our project transforms:</p><pre># BEFORE: Monolithic<br>infra/<br>├── main.tf          # 700+ lines (VPC, DocDB, ECS, ALB, S3, CloudFront, IAM)<br>├── variables.tf<br>└── outputs.tf<br><br># AFTER: Fully Modularized<br>infra/<br>├── main.tf                  # Root orchestrator: calls and wires modules together<br>├── variables.tf             # Root environment variables<br>├── outputs.tf               # Root outputs (CloudFront URL, ALB DNS, DB Endpoint)<br>├── providers.tf             # Provider configs (AWS, Random, Local)<br>├── moved.tf                 # Declarative state migrations<br>└── modules/<br>    ├── networking/          # VPC, Public &amp; Private Subnets, IGW, Route Tables<br>    ├── security/            # Security Groups (ALB, ECS, DocumentDB)<br>    ├── database/            # DocumentDB Cluster, Instances &amp; AWS Secrets Manager<br>    ├── alb/                 # Application Load Balancer, Listener &amp; Target Group<br>    ├── ecs/                 # Launch Template, ASG, ECS Cluster, Task &amp; Service<br>    └── frontend/            # S3 Static Hosting, CloudFront CDN &amp; OAC</pre><h3>Step 2: Creating the Infrastructure Modules</h3><p>Every module follows the standard Terraform structure:</p><ul><li>variables.tfExplicit input configuration.</li><li>main.tfResources managed by the module.</li><li>outputs.tfValues exposed to the root orchestrator.</li></ul><p>Let us look at how the key modules are built:</p><h4><strong>a. Networking Module (</strong><strong>modules/networking)</strong></h4><p>Handles the VPC, multi-AZ subnets, and routing.</p><p><strong>modules/networking/variables.tf</strong></p><pre>variable &quot;cidr_block&quot; {<br>  type        = string<br>  description = &quot;VPC CIDR block&quot;<br>}</pre><p><strong>modules/networking/main.tf</strong></p><pre>resource &quot;aws_vpc&quot; &quot;main&quot; {<br>  cidr_block           = var.cidr_block<br>  enable_dns_hostnames = true<br>  enable_dns_support   = true<br><br>  tags = {<br>    Name = &quot;main-vpc&quot;<br>  }<br>}<br><br>resource &quot;aws_subnet&quot; &quot;public_subnet_1&quot; {<br>  vpc_id                  = aws_vpc.main.id<br>  cidr_block              = cidrsubnet(aws_vpc.main.cidr_block, 8, 1) # 10.0.1.0/24<br>  map_public_ip_on_launch = true<br>  availability_zone       = &quot;us-east-1a&quot;<br>  tags                    = { Name = &quot;ecs-public-subnet-1&quot;, Tier = &quot;Public&quot; }<br>}<br><br>resource &quot;aws_subnet&quot; &quot;public_subnet_2&quot; {<br>  vpc_id                  = aws_vpc.main.id<br>  cidr_block              = cidrsubnet(aws_vpc.main.cidr_block, 8, 2) # 10.0.2.0/24<br>  map_public_ip_on_launch = true<br>  availability_zone       = &quot;us-east-1b&quot;<br>  tags                    = { Name = &quot;ecs-public-subnet-2&quot;, Tier = &quot;Public&quot; }<br>}<br><br>resource &quot;aws_subnet&quot; &quot;private_subnet_1&quot; {<br>  vpc_id                  = aws_vpc.main.id<br>  cidr_block              = cidrsubnet(aws_vpc.main.cidr_block, 8, 10) # 10.0.10.0/24<br>  map_public_ip_on_launch = false<br>  availability_zone       = &quot;us-east-1a&quot;<br>  tags                    = { Name = &quot;docdb-private-subnet-1&quot;, Tier = &quot;Private&quot; }<br>}<br><br>resource &quot;aws_subnet&quot; &quot;private_subnet_2&quot; {<br>  vpc_id                  = aws_vpc.main.id<br>  cidr_block              = cidrsubnet(aws_vpc.main.cidr_block, 8, 11) # 10.0.11.0/24<br>  map_public_ip_on_launch = false<br>  availability_zone       = &quot;us-east-1b&quot;<br>  tags                    = { Name = &quot;docdb-private-subnet-2&quot;, Tier = &quot;Private&quot; }<br>}<br><br>resource &quot;aws_internet_gateway&quot; &quot;igw&quot; {<br>  vpc_id = aws_vpc.main.id<br>  tags   = { Name = &quot;main-igw&quot; }<br>}<br><br>resource &quot;aws_route_table&quot; &quot;public_rt&quot; {<br>  vpc_id = aws_vpc.main.id<br>  route {<br>    cidr_block = &quot;0.0.0.0/0&quot;<br>    gateway_id = aws_internet_gateway.igw.id<br>  }<br>}<br><br>resource &quot;aws_route_table_association&quot; &quot;public_1&quot; {<br>  subnet_id      = aws_subnet.public_subnet_1.id<br>  route_table_id = aws_route_table.public_rt.id<br>}<br><br>resource &quot;aws_route_table_association&quot; &quot;public_2&quot; {<br>  subnet_id      = aws_subnet.public_subnet_2.id<br>  route_table_id = aws_route_table.public_rt.id<br>}</pre><p><strong>modules/networking/outputs.tf</strong></p><pre>output &quot;vpc_id&quot; {<br>  value = aws_vpc.main.id<br>}<br><br>output &quot;public_subnet_ids&quot; {<br>  value = [aws_subnet.public_subnet_1.id, aws_subnet.public_subnet_2.id]<br>}<br><br>output &quot;private_subnet_ids&quot; {<br>  value = [aws_subnet.private_subnet_1.id, aws_subnet.private_subnet_2.id]<br>}</pre><h4><strong>b. Database Module (</strong><strong>modules/database)</strong></h4><p>Encapsulates DocumentDB clustering, credential generation, and AWS Secrets Manager storage.</p><p><strong>modules/database/variables.tf</strong></p><pre>variable &quot;subnet_ids&quot; { type = list(string) }<br>variable &quot;security_group_ids&quot; { type = list(string) }<br>variable &quot;master_username&quot; { type = string; default = &quot;docdbadmin&quot; }<br>variable &quot;master_password&quot; { type = string; default = &quot;&quot;; sensitive = true }<br>variable &quot;instance_class&quot; { type = string; default = &quot;db.t3.medium&quot; }</pre><p><strong>modules/database/main.tf</strong></p><pre>resource &quot;random_password&quot; &quot;docdb_password&quot; {<br>  length           = 16<br>  special          = true<br>  override_special = &quot;!_-&quot;<br>}<br><br>locals {<br>  password = var.master_password != &quot;&quot; ? var.master_password : random_password.docdb_password.result<br>}<br><br>resource &quot;aws_docdb_subnet_group&quot; &quot;subnet_group&quot; {<br>  name        = &quot;docdb-private-subnet-group&quot;<br>  subnet_ids  = var.subnet_ids<br>  description = &quot;Subnet group for DocumentDB residing in isolated private subnets&quot;<br>}<br><br>resource &quot;aws_docdb_cluster&quot; &quot;cluster&quot; {<br>  cluster_identifier     = &quot;golang-docdb-cluster&quot;<br>  engine                 = &quot;docdb&quot;<br>  master_username        = var.master_username<br>  master_password        = local.password<br>  db_subnet_group_name   = aws_docdb_subnet_group.subnet_group.name<br>  vpc_security_group_ids = var.security_group_ids<br>  skip_final_snapshot    = true<br>  deletion_protection    = false<br>}<br><br>resource &quot;aws_docdb_cluster_instance&quot; &quot;instance&quot; {<br>  count              = 1<br>  identifier         = &quot;golang-docdb-instance-1&quot;<br>  cluster_identifier = aws_docdb_cluster.cluster.id<br>  instance_class     = var.instance_class<br>}<br><br>resource &quot;aws_secretsmanager_secret&quot; &quot;secret&quot; {<br>  name_prefix             = &quot;docdb-credentials-&quot;<br>  recovery_window_in_days = 0<br>}<br><br>resource &quot;aws_secretsmanager_secret_version&quot; &quot;secret_version&quot; {<br>  secret_id = aws_secretsmanager_secret.secret.id<br>  secret_string = jsonencode({<br>    username = var.master_username<br>    password = local.password<br>    host     = aws_docdb_cluster.cluster.endpoint<br>    port     = 27017<br>    database = &quot;golang_db&quot;<br>    uri      = &quot;mongodb://${var.master_username}:${local.password}@${aws_docdb_cluster.cluster.endpoint}:27017/golang_db?tls=true&amp;replicaSet=rs0&amp;readPreference=secondaryPreferred&amp;retryWrites=false&quot;<br>  })<br>}</pre><p><strong>modules/database/outputs.tf</strong></p><pre>output &quot;cluster_endpoint&quot; {<br>  value = aws_docdb_cluster.cluster.endpoint<br>}<br><br>output &quot;secret_arn&quot; {<br>  value = aws_secretsmanager_secret.secret.arn<br>}<br><br>output &quot;cluster_instance_id&quot; {<br>  value = aws_docdb_cluster_instance.instance[0].id<br>}</pre><h4><strong>c. Frontend &amp; CDN Module (</strong><strong>modules/frontend)</strong></h4><p>Packages S3 static web hosting, CloudFront CDN distribution, Origin Access Control (OAC), and API reverse proxying.</p><p><strong>modules/frontend/variables.tf</strong></p><pre>variable &quot;alb_dns_name&quot; {<br>  type        = string<br>  description = &quot;ALB DNS name for routing /api/* backend requests&quot;<br>}</pre><p><strong>modules/frontend/main.tf</strong></p><pre>resource &quot;random_password&quot; &quot;origin_secret&quot; {<br>  length  = 32<br>  special = false<br>}<br><br>resource &quot;aws_s3_bucket&quot; &quot;react_bucket&quot; {<br>  bucket_prefix = &quot;react-frontend-&quot;<br>}<br><br>resource &quot;aws_s3_bucket_website_configuration&quot; &quot;static_hosting&quot; {<br>  bucket = aws_s3_bucket.react_bucket.id<br>  index_document {<br>    suffix = &quot;index.html&quot;<br>  }<br>}<br><br>resource &quot;aws_cloudfront_origin_access_control&quot; &quot;default&quot; {<br>  name                              = &quot;react-s3-oac&quot;<br>  origin_access_control_origin_type = &quot;s3&quot;<br>  signing_behavior                  = &quot;always&quot;<br>  signing_protocol                  = &quot;sigv4&quot;<br>}<br><br>resource &quot;aws_cloudfront_distribution&quot; &quot;this&quot; {<br>  enabled             = true<br>  is_ipv6_enabled     = true<br>  default_root_object = &quot;index.html&quot;<br><br>  origin {<br>    domain_name              = aws_s3_bucket.react_bucket.bucket_regional_domain_name<br>    origin_id                = &quot;s3-react-frontend&quot;<br>    origin_access_control_id = aws_cloudfront_origin_access_control.default.id<br>  }<br><br>  origin {<br>    domain_name = var.alb_dns_name<br>    origin_id   = &quot;alb-backend-api&quot;<br><br>    custom_header {<br>      name  = &quot;X-Origin-Verify&quot;<br>      value = random_password.origin_secret.result<br>    }<br><br>    custom_origin_config {<br>      http_port              = 80<br>      https_port             = 443<br>      origin_protocol_policy = &quot;http-only&quot;<br>      origin_ssl_protocols   = [&quot;TLSv1.2&quot;]<br>    }<br>  }<br><br>  ordered_cache_behavior {<br>    path_pattern     = &quot;/api/*&quot;<br>    target_origin_id = &quot;alb-backend-api&quot;<br>    allowed_methods  = [&quot;GET&quot;, &quot;HEAD&quot;, &quot;OPTIONS&quot;, &quot;PUT&quot;, &quot;POST&quot;, &quot;PATCH&quot;, &quot;DELETE&quot;]<br>    cached_methods   = [&quot;GET&quot;, &quot;HEAD&quot;]<br><br>    forwarded_values {<br>      query_string = true<br>      headers      = [&quot;Accept&quot;, &quot;Authorization&quot;, &quot;Content-Type&quot;, &quot;Origin&quot;, &quot;Referer&quot;]<br>      cookies {<br>        forward = &quot;all&quot;<br>      }<br>    }<br>    viewer_protocol_policy = &quot;redirect-to-https&quot;<br>  }<br><br>  default_cache_behavior {<br>    target_origin_id       = &quot;s3-react-frontend&quot;<br>    allowed_methods        = [&quot;GET&quot;, &quot;HEAD&quot;, &quot;OPTIONS&quot;]<br>    cached_methods         = [&quot;GET&quot;, &quot;HEAD&quot;]<br>    viewer_protocol_policy = &quot;redirect-to-https&quot;<br><br>    forwarded_values {<br>      query_string = false<br>      cookies {<br>        forward = &quot;none&quot;<br>      }<br>    }<br>  }<br><br>  restrictions {<br>    geo_restriction { restriction_type = &quot;none&quot; }<br>  }<br><br>  viewer_certificate {<br>    cloudfront_default_certificate = true<br>  }<br>}</pre><p><strong>modules/frontend/outputs.tf</strong></p><pre>output &quot;cloudfront_domain_name&quot; {<br>  value = aws_cloudfront_distribution.this.domain_name<br>}<br><br>output &quot;s3_bucket_name&quot; {<br>  value = aws_s3_bucket.react_bucket.id<br>}<br><br>output &quot;origin_secret_header_value&quot; {<br>  value     = random_password.origin_secret.result<br>  sensitive = true<br>}</pre><h3>Step 3: Assembling the Root main.tf</h3><p>main.tf serves purely as a clean orchestrator, wiring module outputs into other module inputs:</p><pre># infra/main.tf<br><br>module &quot;networking&quot; {<br>  source     = &quot;./modules/networking&quot;<br>  cidr_block = var.cidr_block<br>}<br><br>module &quot;security&quot; {<br>  source = &quot;./modules/security&quot;<br>  vpc_id = module.networking.vpc_id<br>}<br><br>module &quot;database&quot; {<br>  source             = &quot;./modules/database&quot;<br>  subnet_ids         = module.networking.private_subnet_ids<br>  security_group_ids = [module.security.docdb_sg_id]<br>  master_username    = var.docdb_username<br>  master_password    = var.docdb_password<br>  instance_class     = var.docdb_instance_class<br>}<br><br>module &quot;alb&quot; {<br>  source                      = &quot;./modules/alb&quot;<br>  vpc_id                      = module.networking.vpc_id<br>  public_subnet_ids           = module.networking.public_subnet_ids<br>  alb_security_group_id       = module.security.alb_sg_id<br>  origin_verify_header_secret = module.frontend.origin_secret_header_value<br>}<br><br>module &quot;ecs&quot; {<br>  source                = &quot;./modules/ecs&quot;<br>  vpc_id                = module.networking.vpc_id<br>  public_subnet_ids     = module.networking.public_subnet_ids<br>  ecs_security_group_id = module.security.ecs_sg_id<br>  target_group_arn      = module.alb.target_group_arn<br>  docdb_secret_arn      = module.database.secret_arn<br>  key_name              = var.key_name<br>  db_cluster_instance_id = module.database.cluster_instance_id<br>}<br><br>module &quot;frontend&quot; {<br>  source       = &quot;./modules/frontend&quot;<br>  alb_dns_name = module.alb.alb_dns_name<br>}</pre><h4>Step 4: Zero-Downtime State Migration with moved Blocks</h4><p>Here comes the most critical part of Terraform refactoring.</p><p>If you run terraform plan right now, Terraform will attempt to destroy all your existing monolithic resources and create new ones inside the modules:</p><pre>Plan: 25 to add, 0 to change, 25 to destroy.</pre><p>In a production environment, this would cause catastrophic data loss and downtime.</p><p>To prevent this, Terraform 1.1+ introduced <strong>declarative </strong><strong>moved blocks</strong>. We add a moved.tf file to map every monolithic resource address to its new module location:</p><p><strong>infra/moved.tf</strong></p><pre># --- Networking Module Moves ---<br>moved {<br>  from = aws_vpc.main<br>  to   = module.networking.aws_vpc.main<br>}<br>moved {<br>  from = aws_subnet.public_subnet_1<br>  to   = module.networking.aws_subnet.public_subnet_1<br>}<br>moved {<br>  from = aws_subnet.public_subnet_2<br>  to   = module.networking.aws_subnet.public_subnet_2<br>}<br>moved {<br>  from = aws_subnet.private_subnet_1<br>  to   = module.networking.aws_subnet.private_subnet_1<br>}<br>moved {<br>  from = aws_subnet.private_subnet_2<br>  to   = module.networking.aws_subnet.private_subnet_2<br>}<br>moved {<br>  from = aws_internet_gateway.igw<br>  to   = module.networking.aws_internet_gateway.igw<br>}<br>moved {<br>  from = aws_route_table.public_rt<br>  to   = module.networking.aws_route_table.public_rt<br>}<br>moved {<br>  from = aws_route_table_association.public_1<br>  to   = module.networking.aws_route_table_association.public_1<br>}<br>moved {<br>  from = aws_route_table_association.public_2<br>  to   = module.networking.aws_route_table_association.public_2<br>}<br><br># --- Database Module Moves ---<br>moved {<br>  from = aws_docdb_subnet_group.docdb_subnet_group<br>  to   = module.database.aws_docdb_subnet_group.subnet_group<br>}<br>moved {<br>  from = aws_docdb_cluster.docdb_cluster<br>  to   = module.database.aws_docdb_cluster.cluster<br>}<br>moved {<br>  from = aws_docdb_cluster_instance.docdb_cluster_instance[0]<br>  to   = module.database.aws_docdb_cluster_instance.instance[0]<br>}<br>moved {<br>  from = aws_secretsmanager_secret.docdb_secret<br>  to   = module.database.aws_secretsmanager_secret.secret<br>}<br>moved {<br>  from = aws_secretsmanager_secret_version.docdb_secret_version<br>  to   = module.database.aws_secretsmanager_secret_version.secret_version<br>}<br><br># --- Frontend Module Moves ---<br>moved {<br>  from = random_password.origin_secret<br>  to   = module.frontend.random_password.origin_secret<br>}<br>moved {<br>  from = aws_s3_bucket.react_bucket<br>  to   = module.frontend.aws_s3_bucket.react_bucket<br>}<br>moved {<br>  from = aws_s3_bucket_website_configuration.static_hosting<br>  to   = module.frontend.aws_s3_bucket_website_configuration.static_hosting<br>}<br>moved {<br>  from = aws_cloudfront_origin_access_control.default<br>  to   = module.frontend.aws_cloudfront_origin_access_control.default<br>}<br>moved {<br>  from = aws_cloudfront_distribution.this<br>  to   = module.frontend.aws_cloudfront_distribution.this<br>}</pre><h4>Step 5: Validating the Migration</h4><p>Initialize the new modules and run a plan:</p><pre>terraform init <br>terraform plan</pre><p>Expected Output:</p><pre>Terraform will perform the following actions:<br><br>  .....<br><br>Plan: 0 to add, 0 to change, 0 to destroy.</pre><p>Apply the migration safely:</p><pre>terraform apply -auto-approve</pre><p>Terraform re-indexes its state file to reflect the modular structure instantly, without touching live cloud resources.</p><h3>Conclusion</h3><p>Transforming a monolithic Terraform configuration into modular architecture is one of the highest investments you can make in your cloud infrastructure.</p><p>By extracting self-contained modules (networking, database, ecs, frontend) and orchestrating them via the root module with moved blocks, we achieved:</p><ul><li><strong>Clean Separation of Concerns:</strong> Tiers can evolve independently.</li><li><strong>Enterprise Reusability:</strong> Modules can be shared across multiple environments.</li><li><strong>Safe, Zero-Downtime Migration:</strong> Infrastructure state updated smoothly with 0 resource destruction.</li></ul><p>Happy Terraforming. Feel free to check out the full refactored code in the <a href="https://github.com/achenchi7/react-go-application/tree/main/infra-modular">GitHub repository</a>.</p><p><em>If you found this guide valuable, feel free to share it, leave your feedback in the comments, and connect with me on </em><a href="https://www.linkedin.com/in/jully-achenchi"><strong><em>LinkedIn</em></strong></a><em>!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=801381adc70b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Deploy a React and Go application on AWS ECS and S3 using Terraform]]></title>
            <link>https://medium.com/@jullyachenchi8/how-to-deploy-a-react-and-go-application-on-aws-ecs-and-s3-using-terraform-de4fc8932a77?source=rss-7a6e9f200238------2</link>
            <guid isPermaLink="false">https://medium.com/p/de4fc8932a77</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[aws-ecs]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Jully Achenchi]]></dc:creator>
            <pubDate>Mon, 21 Sep 2026 16:04:44 GMT</pubDate>
            <atom:updated>2026-09-21T16:04:44.309Z</atom:updated>
            <content:encoded><![CDATA[<h4>A Step-by-Step Architectural Guide to Provisioning a Secure, Scalable System with Zero CORS Overhead</h4><p>Recently, I have been exploring how to keep a system as straightforward as possible without sacrificing security, reliability, or scalability. One such experiment led me to build a simple to-do API using the Go programming language, React as the frontend framework, and MongoDB for persistence, and then deploy it to AWS.</p><p>This guide will not emphasize how to build the API. Rather, we will aim our focus on building the architecture to deploy the application. At the end of this guide, you will learn how to deploy a Go API on ECS and React on S3, and the decisions that go behind the choice of each service and the specific configurations.</p><p>Shall we?</p><h3><strong>Introduction to the Tech Stack</strong></h3><p><strong>Go</strong>, often referred to as <strong>Golang</strong>, is an open-source, statically typed, compiled programming language that helps software engineers build scalable, reliable, and highly efficient cloud infrastructure and backend systems. [<a href="https://go.dev/">1</a>, <a href="https://www.coursera.org/articles/go-programming-language">2</a>]. I used Go to build a simple to-do API that lets users add a task, edit a task, mark it as done, and delete it. I used MongoDB as the database.</p><p><a href="https://www.mongodb.com/">MongoDB</a> is a NoSQL database used to store and retrieve application data.</p><p><a href="https://react.dev/">React.js</a> is a declarative, component-based front-end library used to build interactive Single Page Applications.</p><p><a href="https://www.terraform.io/">Terraform</a><strong> </strong>is a cloud-agnostic<strong> </strong>Infrastructure as Code (IaC) tool by HashiCorp used to provision and manage all our cloud resources declaratively.</p><h3>What we will cover</h3><ul><li>The architecture overview</li><li>Prerequisites</li><li>Containerizing the Go API with Multi-Stage Build Optimization</li><li>VPC Networking &amp; Multi-AZ Subnet Design</li><li>Private Database Isolation with Amazon DocumentDB</li><li>Compute &amp; Orchestration with Amazon ECS on EC2</li><li>Load Balancing &amp; Health Check Configuration</li><li>Frontend Hosting &amp; Ingress with S3 and CloudFront</li><li>Provisioning &amp; Deploying the architecture</li><li>Conclusion &amp; Key Takeaways</li></ul><h3>The Architecture Overview</h3><p>Having understood the tools we will use in this project, let us have a look at the blueprint of how data and user requests will flow through the system</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1oJGvonwypQAnguvDToong.png" /></figure><h4>The Architectural Tiers:</h4><ol><li><strong>Edge routing using Amazon CloudFront, </strong>which<strong> </strong>acts as the single point of entry over HTTPS. It uses path patterns to direct static requests (/*) to Amazon S3 and API calls (/api/*) to the Application Load Balancer.</li><li><strong>Amazon S3 with Website hosting </strong>hosts the bundled React production files. The bucket is 100% private, and it is accessible strictly by CloudFront through <strong>Origin Access Control (OAC)</strong>.</li><li><strong>Ingress &amp; Compute with ALB and Amazon ECS on EC2:</strong> An Application Load Balancer routes incoming API traffic across containerized Go tasks running on an EC2 Auto Scaling Group.</li><li><strong>Data Isolation with Amazon DocumentDB,</strong> which <strong>s</strong>tores task data in a private database subnet group, completely closed off from public internet access.</li></ol><h3>Prerequisites</h3><ul><li>An active AWS account</li><li><a href="https://github.com/achenchi7/react-go-application">The application code</a></li><li><a href="https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html">AWS CLI installed and configured</a></li><li><a href="https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli">Terraform installed</a></li><li><a href="https://docs.docker.com/engine/install/">Docker installed</a></li><li><a href="https://nodejs.org/en">Node.js &amp; npm installed</a> for bundling the React frontend.</li></ul><h3>Step 1: Containerizing the Go API with Multi-Stage Build Optimization</h3><p>Containerizing is the process of packaging your application and all its parts into a single portable file called a container.</p><p>To achieve fast deployments and rapid scaling on ECS, container size matters. A basic single-stage Dockerfile includes the entire Go compiler and toolchain, producing an image exceeding <strong>850MB</strong>.</p><p>By leveraging a <strong>multi-stage build</strong>, we compile our Go binary in an isolated builder stage and copy only the final executable into a minimal Alpine Linux image:</p><pre># Stage 1: Build the binary<br>FROM golang:1.24-alpine AS builder<br><br>WORKDIR /app<br><br># Install build dependencies<br>RUN apk add --no-cache ca-certificates git<br><br># Cache dependencies<br>COPY go.mod go.sum ./<br>RUN go mod download<br><br># Copy source code and compile statically linked binary<br>COPY . .<br>RUN CGO_ENABLED=0 GOOS=linux go build -ldflags=&quot;-w -s&quot; -o /app/go-api ./main.go<br><br># Stage 2: Minimal runtime image<br>FROM alpine:3.20<br><br>WORKDIR /app<br><br># Install CA certificates (including the AWS RDS CA bundle for DocumentDB TLS)<br>RUN apk add --no-cache ca-certificates curl &amp;&amp; \<br>    curl -sS &quot;https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem&quot; -o /usr/local/share/ca-certificates/rds-global-bundle.crt &amp;&amp; \<br>    update-ca-certificates<br><br># Copy compiled binary from builder<br>COPY --from=builder /app/go-api /app/go-api<br><br>EXPOSE 80<br><br>CMD [&quot;/app/go-api&quot;]</pre><h3>Step 2: VPC Networking &amp; Multi-AZ Subnet Design</h3><p>In Terraform, we provision a custom VPC with public subnets distributed across two Availability Zones (us-east-1a and us-east-1b) for high availability:</p><pre>terraform {<br>  required_providers {<br>    aws = {<br>      version = &quot;&gt;=6.6.0&quot;<br>      source  = &quot;hashicorp/aws&quot;<br>    }<br>    local = {<br>      source  = &quot;hashicorp/local&quot;<br>      version = &quot;~&gt;2.0&quot;<br>    }<br>  }<br>}<br><br>provider &quot;aws&quot; {<br>  region = &quot;us-east-1&quot;<br><br>  default_tags {<br>    tags = {<br>      Project    = &quot;react-go-application&quot;<br>      Managed_by = &quot;Terraform&quot;<br>    }<br>  }<br>}<br><br># 1. Virtual Private Cloud<br>resource &quot;aws_vpc&quot; &quot;main&quot; {<br>  cidr_block           = var.cidr_block # 10.0.0.0/16<br>  enable_dns_hostnames = true<br>  enable_dns_support   = true<br>}<br><br># 2. Multi-AZ Subnets<br>resource &quot;aws_subnet&quot; &quot;subnet-1&quot; {<br>  vpc_id                  = aws_vpc.main.id<br>  cidr_block              = cidrsubnet(aws_vpc.main.cidr_block, 8, 1) # 10.0.1.0/24<br>  map_public_ip_on_launch = true<br>  availability_zone       = &quot;us-east-1a&quot;<br><br>  tags = {<br>    Name = &quot;ecs-subnet-1&quot;<br>  }<br>}<br><br>resource &quot;aws_subnet&quot; &quot;subnet-2&quot; {<br>  vpc_id                  = aws_vpc.main.id<br>  cidr_block              = cidrsubnet(aws_vpc.main.cidr_block, 8, 2) # 10.0.2.0/24<br>  map_public_ip_on_launch = true<br>  availability_zone       = &quot;us-east-1b&quot;<br><br>  tags = {<br>    Name = &quot;ecs-subnet-2&quot;<br>  }<br>}<br><br># 3. Internet Gateway &amp; Routing<br>resource &quot;aws_internet_gateway&quot; &quot;igw&quot; {<br>  vpc_id = aws_vpc.main.id<br>}<br><br>resource &quot;aws_route_table&quot; &quot;rt-main&quot; {<br>  vpc_id = aws_vpc.main.id<br>  route {<br>    cidr_block = &quot;0.0.0.0/0&quot;<br>    gateway_id = aws_internet_gateway.igw.id<br>  }<br>}<br><br>resource &quot;aws_route_table_association&quot; &quot;subnet1-route&quot; {<br>  subnet_id      = aws_subnet.subnet-1.id<br>  route_table_id = aws_route_table.rt-main.id<br>}<br><br>resource &quot;aws_route_table_association&quot; &quot;subnet2-route&quot; {<br>  subnet_id      = aws_subnet.subnet-2.id<br>  route_table_id = aws_route_table.rt-main.id<br>}<br><br># 4. Security Group for ECS and Load Balancer<br>resource &quot;aws_security_group&quot; &quot;ecs-sg&quot; {<br>  name        = &quot;ecs-security-group&quot;<br>  description = &quot;Security group for ECS tasks and ALB&quot;<br>  vpc_id      = aws_vpc.main.id<br><br>  ingress {<br>    from_port   = 80<br>    to_port     = 80<br>    protocol    = &quot;tcp&quot;<br>    cidr_blocks = [&quot;0.0.0.0/0&quot;]<br>    description = &quot;Allow inbound HTTP traffic&quot;<br>  }<br><br>  ingress {<br>    from_port   = 0<br>    to_port     = 0<br>    protocol    = &quot;-1&quot;<br>    self        = true<br>    description = &quot;Allow all internal communication within SG&quot;<br>  }<br><br>  egress {<br>    from_port   = 0<br>    to_port     = 0<br>    protocol    = &quot;-1&quot;<br>    cidr_blocks = [&quot;0.0.0.0/0&quot;]<br>  }<br>}</pre><h3>Step 3: Private Database Isolation with Amazon DocumentDB</h3><p>For this section, we will use Amazon DocumentDB with MongoDB compatibility. DocumentDB allows you to separate compute and storage so each layer can scale independently.</p><p>Exposing databases to the public internet is a major security risk. With Amazon DocumentDB, we create a multi-AZ cluster and lock down the port 27017 so it accepts traffic <strong>only</strong> from our ECS compute nodes (aws_security_group.ecs-sg):</p><pre># 1. DocumentDB Subnet Group<br>resource &quot;aws_docdb_subnet_group&quot; &quot;docdb_subnet_group&quot; {<br>  name        = &quot;docdb-subnet-group&quot;<br>  description = &quot;Subnet group for DocumentDB&quot;<br>  subnet_ids  = [aws_subnet.subnet-1.id, aws_subnet.subnet-2.id]<br>}<br><br># 2. Database Security Group (Strictly bound to ecs-sg)<br>resource &quot;aws_security_group&quot; &quot;docdb_sg&quot; {<br>  name        = &quot;docdb-security-group&quot;<br>  description = &quot;Allow inbound MongoDB traffic from ECS tasks&quot;<br>  vpc_id      = aws_vpc.main.id<br><br>  ingress {<br>    description     = &quot;MongoDB connection from ECS&quot;<br>    from_port       = 27017<br>    to_port         = 27017<br>    protocol        = &quot;tcp&quot;<br>    security_groups = [aws_security_group.ecs-sg.id] # Only ECS tasks allowed!<br>  }<br><br>  egress {<br>    from_port   = 0<br>    to_port     = 0<br>    protocol    = &quot;-1&quot;<br>    cidr_blocks = [&quot;0.0.0.0/0&quot;]<br>  }<br>}<br><br># 3. The DocumentDB Cluster &amp; Instance<br>resource &quot;aws_docdb_cluster&quot; &quot;docdb_cluster&quot; {<br>  cluster_identifier     = &quot;golang-docdb-cluster&quot;<br>  engine                 = &quot;docdb&quot;<br>  master_username        = var.docdb_username<br>  master_password        = var.docdb_password<br>  db_subnet_group_name   = aws_docdb_subnet_group.docdb_subnet_group.name<br>  vpc_security_group_ids = [aws_security_group.docdb_sg.id]<br>  skip_final_snapshot    = true<br>  deletion_protection    = false<br>}<br><br>resource &quot;aws_docdb_cluster_instance&quot; &quot;docdb_cluster_instance&quot; {<br>  count              = 1<br>  identifier         = &quot;golang-docdb-instance-1&quot;<br>  cluster_identifier = aws_docdb_cluster.docdb_cluster.id<br>  instance_class     = var.docdb_instance_class # db.t3.medium<br>}</pre><h3>Step 4: Compute &amp; Orchestration with Amazon ECS on EC2</h3><p>To host our containerized Go API, we use Amazon ECS with EC2 compute instances managed by an Auto Scaling Group.</p><ol><li><strong>Launch Template (</strong><strong>aws_launch_template.ecs_lt)</strong>: Uses the Amazon Linux 2 ECS-Optimized AMI and runs a bootstrap script (ecs.sh containing echo ECS_CLUSTER=my-ecs-cluster &gt;&gt; /etc/ecs/ecs.config) to join the cluster.</li><li><strong>Auto Scaling Group (</strong><strong>aws_autoscaling_group.ecs_asg) </strong>which maintains at least 2 EC2 instances across both subnets.</li><li><strong>ECS Task Definition (</strong><strong>aws_ecs_task_definition.ecs_task_definition)</strong>: Defines container memory, CPU, port mapping, CloudWatch logging (aws_cloudwatch_log_group.ecs_logs), and injects the dynamic DocumentDB connection string.</li></ol><pre># 1. EC2 Launch Template &amp; Auto Scaling Group<br>data &quot;aws_ssm_parameter&quot; &quot;ecs_ami&quot; {<br>  name = &quot;/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id&quot;<br>}<br><br>resource &quot;aws_launch_template&quot; &quot;ecs_lt&quot; {<br>  name_prefix   = &quot;ecs-template-&quot;<br>  image_id      = data.aws_ssm_parameter.ecs_ami.value<br>  instance_type = &quot;t3.micro&quot;<br>  key_name      = var.key_name<br><br>  vpc_security_group_ids = [aws_security_group.ecs-sg.id]<br><br>  iam_instance_profile {<br>    arn = aws_iam_instance_profile.ecs_node.arn<br>  }<br><br>  user_data = filebase64(&quot;${path.module}/ecs.sh&quot;)<br>}<br><br>resource &quot;aws_autoscaling_group&quot; &quot;ecs_asg&quot; {<br>  vpc_zone_identifier = [aws_subnet.subnet-1.id, aws_subnet.subnet-2.id]<br>  desired_capacity    = 2<br>  max_size            = 3<br>  min_size            = 1<br><br>  launch_template {<br>    id      = aws_launch_template.ecs_lt.id<br>    version = &quot;$Latest&quot;<br>  }<br><br>  tag {<br>    key                 = &quot;AmazonECSManaged&quot;<br>    value               = true<br>    propagate_at_launch = true<br>  }<br>}<br><br># 2. ECS Cluster &amp; Capacity Provider<br>resource &quot;aws_ecs_cluster&quot; &quot;ecs_cluster&quot; {<br>  name = &quot;my-ecs-cluster&quot;<br>}<br><br>resource &quot;aws_ecs_capacity_provider&quot; &quot;ecs_capacity_provider&quot; {<br>  name = &quot;app-capacity-provider&quot;<br><br>  auto_scaling_group_provider {<br>    auto_scaling_group_arn = aws_autoscaling_group.ecs_asg.arn<br><br>    managed_scaling {<br>      maximum_scaling_step_size = 1000<br>      minimum_scaling_step_size = 1<br>      status                    = &quot;ENABLED&quot;<br>      target_capacity           = 100<br>    }<br>  }<br>}<br><br>resource &quot;aws_ecs_cluster_capacity_providers&quot; &quot;ecs_cluster_cp&quot; {<br>  cluster_name = aws_ecs_cluster.ecs_cluster.name<br><br>  capacity_providers = [aws_ecs_capacity_provider.ecs_capacity_provider.name]<br><br>  default_capacity_provider_strategy {<br>    base              = 1<br>    weight            = 100<br>    capacity_provider = aws_ecs_capacity_provider.ecs_capacity_provider.name<br>  }<br>}<br><br># 3. ECS Task Definition with CloudWatch Logging &amp; Dynamic DocumentDB URI<br>resource &quot;aws_ecs_task_definition&quot; &quot;ecs_task_definition&quot; {<br>  family             = &quot;my-ecs-task&quot;<br>  network_mode       = &quot;awsvpc&quot;<br>  execution_role_arn = aws_iam_role.ecs_task_execution_role.arn<br>  cpu                = 256<br>  memory             = 512<br><br>  container_definitions = jsonencode([<br>    {<br>      name      = &quot;go-todo-api&quot;<br>      image     = &quot;public.ecr.aws/x5g4c0f9/playground:v2&quot;<br>      cpu       = 256<br>      memory    = 512<br>      essential = true<br>      portMappings = [<br>        {<br>          containerPort = 80<br>          hostPort      = 80<br>          protocol      = &quot;tcp&quot;<br>        }<br>      ]<br>      logConfiguration = {<br>        logDriver = &quot;awslogs&quot;<br>        options = {<br>          &quot;awslogs-group&quot;         = aws_cloudwatch_log_group.ecs_logs.name<br>          &quot;awslogs-region&quot;        = &quot;us-east-1&quot;<br>          &quot;awslogs-stream-prefix&quot; = &quot;ecs&quot;<br>        }<br>      }<br>      environment = [<br>        {<br>          name  = &quot;PORT&quot;<br>          value = &quot;80&quot;<br>        },<br>        {<br>          name  = &quot;ENV&quot;<br>          value = &quot;production&quot;<br>        },<br>        {<br>          name  = &quot;MONGODB_URI&quot;<br>          value = &quot;mongodb://${var.docdb_username}:${var.docdb_password}@${aws_docdb_cluster.docdb_cluster.endpoint}:27017/golang_db?tls=true&amp;tlsInsecure=true&amp;tlsAllowInvalidCertificates=true&amp;replicaSet=rs0&amp;readPreference=secondaryPreferred&amp;retryWrites=false&quot;<br>        }<br>      ]<br>    }<br>  ])<br>}<br><br># 4. ECS Service<br>resource &quot;aws_ecs_service&quot; &quot;ecs_service&quot; {<br>  name            = &quot;my-ecs-service&quot;<br>  cluster         = aws_ecs_cluster.ecs_cluster.id<br>  task_definition = aws_ecs_task_definition.ecs_task_definition.arn<br>  desired_count   = 2<br><br>  network_configuration {<br>    subnets         = [aws_subnet.subnet-1.id, aws_subnet.subnet-2.id]<br>    security_groups = [aws_security_group.ecs-sg.id]<br>  }<br><br>  force_new_deployment = true<br><br>  capacity_provider_strategy {<br>    capacity_provider = aws_ecs_capacity_provider.ecs_capacity_provider.name<br>    weight            = 100<br>  }<br><br>  load_balancer {<br>    target_group_arn = aws_lb_target_group.ecs_tg.arn<br>    container_name   = &quot;go-todo-api&quot;<br>    container_port   = 80<br>  }<br><br>  depends_on = [<br>    aws_autoscaling_group.ecs_asg,<br>    aws_docdb_cluster_instance.docdb_cluster_instance<br>  ]<br>}<br><br># 5. CloudWatch Log Group<br>resource &quot;aws_cloudwatch_log_group&quot; &quot;ecs_logs&quot; {<br>  name              = &quot;/ecs/go-todo-api&quot;<br>  retention_in_days = 7<br>}</pre><h3>Step 5: Load Balancing &amp; Health Check Configuration</h3><p>The Application Load Balancer (aws_lb.ecs_alb) balances traffic across the Go containers.</p><pre>resource &quot;aws_lb&quot; &quot;ecs_alb&quot; {<br>  name               = &quot;ecs-alb&quot;<br>  internal           = false<br>  load_balancer_type = &quot;application&quot;<br>  security_groups    = [aws_security_group.ecs-sg.id]<br>  subnets            = [aws_subnet.subnet-1.id, aws_subnet.subnet-2.id]<br><br>  tags = {<br>    Name = &quot;ecs-alb&quot;<br>  }<br>}<br><br>resource &quot;aws_lb_target_group&quot; &quot;ecs_tg&quot; {<br>  name        = &quot;ecs-target-group&quot;<br>  port        = 80<br>  protocol    = &quot;HTTP&quot;<br>  target_type = &quot;ip&quot;<br>  vpc_id      = aws_vpc.main.id<br><br>  health_check {<br>    path                = &quot;/api/todos&quot; # Explicit valid endpoint<br>    matcher             = &quot;200,404&quot;<br>    interval            = 30<br>    timeout             = 5<br>    healthy_threshold   = 2<br>    unhealthy_threshold = 3<br>  }<br>}<br><br>resource &quot;aws_lb_listener&quot; &quot;ecs_alb_listener&quot; {<br>  load_balancer_arn = aws_lb.ecs_alb.arn<br>  port              = 80<br>  protocol          = &quot;HTTP&quot;<br><br>  default_action {<br>    type             = &quot;forward&quot;<br>    target_group_arn = aws_lb_target_group.ecs_tg.arn<br>  }<br>}</pre><h3>Step 6: Frontend Hosting &amp; Ingress with S3 and CloudFront</h3><h4>a. Private S3 Bucket with Origin Access Control (OAC)</h4><p>We provision an S3 bucket (aws_s3_bucket.react_bucket) with public access completely blocked, and attach a bucket policy allowing access <strong>strictly from our CloudFront distribution ARN</strong>:</p><pre># 1. S3 Bucket for React Static Assets<br>resource &quot;aws_s3_bucket&quot; &quot;react_bucket&quot; {<br>  bucket_prefix = &quot;react-frontend-&quot;<br>}<br><br>resource &quot;aws_s3_bucket_website_configuration&quot; &quot;static_hosting&quot; {<br>  bucket = aws_s3_bucket.react_bucket.id<br><br>  index_document {<br>    suffix = &quot;index.html&quot;<br>  }<br>}<br><br># 2. CloudFront Origin Access Control (OAC)<br>resource &quot;aws_cloudfront_origin_access_control&quot; &quot;default&quot; {<br>  name                              = &quot;react-s3-oac&quot;<br>  origin_access_control_origin_type = &quot;s3&quot;<br>  signing_behavior                  = &quot;always&quot;<br>  signing_protocol                  = &quot;sigv4&quot;<br>}<br><br># 3. S3 Bucket Policy (Restricts reads exclusively to CloudFront)<br>data &quot;aws_iam_policy_document&quot; &quot;origin_bucket_policy&quot; {<br>  statement {<br>    sid    = &quot;AllowCloudFrontServicePrincipalReadOnly&quot;<br>    effect = &quot;Allow&quot;<br><br>    principals {<br>      type        = &quot;Service&quot;<br>      identifiers = [&quot;cloudfront.amazonaws.com&quot;]<br>    }<br><br>    actions = [<br>      &quot;s3:GetObject&quot;<br>    ]<br><br>    resources = [<br>      &quot;${aws_s3_bucket.react_bucket.arn}/*&quot;,<br>    ]<br><br>    condition {<br>      test     = &quot;StringEquals&quot;<br>      variable = &quot;AWS:SourceArn&quot;<br>      values   = [aws_cloudfront_distribution.this.arn]<br>    }<br>  }<br>}<br><br>resource &quot;aws_s3_bucket_policy&quot; &quot;cf_policy&quot; {<br>  bucket = aws_s3_bucket.react_bucket.id<br>  policy = data.aws_iam_policy_document.origin_bucket_policy.json<br>}</pre><h4>b. CloudFront Multi-Origin Reverse Proxy</h4><p>A <strong>CloudFront multi-origin reverse proxy</strong> uses Amazon CloudFront to accept user requests globally and route them to different backend servers based on URL paths or routing rules. We will configure two origins:</p><ul><li><strong>Origin 1 (S3 — </strong><strong>s3-react-frontend)</strong>: Serves static React assets (/*) with global edge caching.</li><li><strong>Origin 2 (ALB — </strong><strong>alb-backend-api)</strong>: Routes API calls (/api/*) dynamically with min_ttl = 0 (no caching).</li></ul><pre>resource &quot;aws_cloudfront_distribution&quot; &quot;this&quot; {<br>  enabled             = true<br>  is_ipv6_enabled     = true<br>  default_root_object = &quot;index.html&quot;<br><br>  # Origin 1: S3 Bucket (React Frontend)<br>  origin {<br>    domain_name              = aws_s3_bucket.react_bucket.bucket_regional_domain_name<br>    origin_id                = &quot;s3-react-frontend&quot;<br>    origin_access_control_id = aws_cloudfront_origin_access_control.default.id<br>  }<br><br>  # Origin 2: Application Load Balancer (Go API Backend)<br>  origin {<br>    domain_name = aws_lb.ecs_alb.dns_name<br>    origin_id   = &quot;alb-backend-api&quot;<br><br>    custom_origin_config {<br>      http_port              = 80<br>      https_port             = 443<br>      origin_protocol_policy = &quot;http-only&quot;<br>      origin_ssl_protocols   = [&quot;TLSv1.2&quot;]<br>    }<br>  }<br><br>  # Rule 1: Route /api/* to the ALB Backend<br>  ordered_cache_behavior {<br>    path_pattern     = &quot;/api/*&quot;<br>    target_origin_id = &quot;alb-backend-api&quot;<br><br>    allowed_methods = [&quot;GET&quot;, &quot;HEAD&quot;, &quot;OPTIONS&quot;, &quot;PUT&quot;, &quot;POST&quot;, &quot;PATCH&quot;, &quot;DELETE&quot;]<br>    cached_methods  = [&quot;GET&quot;, &quot;HEAD&quot;]<br><br>    forwarded_values {<br>      query_string = true<br>      headers      = [&quot;Accept&quot;, &quot;Authorization&quot;, &quot;Content-Type&quot;, &quot;Origin&quot;, &quot;Referer&quot;]<br>      cookies {<br>        forward = &quot;all&quot;<br>      }<br>    }<br><br>    viewer_protocol_policy = &quot;redirect-to-https&quot;<br>    min_ttl                = 0<br>    default_ttl            = 0<br>    max_ttl                = 0<br>  }<br><br>  # Rule 2: Default Behavior: Route everything else to S3 (Frontend)<br>  default_cache_behavior {<br>    target_origin_id       = &quot;s3-react-frontend&quot;<br>    allowed_methods        = [&quot;GET&quot;, &quot;HEAD&quot;, &quot;OPTIONS&quot;]<br>    cached_methods         = [&quot;GET&quot;, &quot;HEAD&quot;]<br>    viewer_protocol_policy = &quot;redirect-to-https&quot;<br><br>    forwarded_values {<br>      query_string = false<br>      cookies {<br>        forward = &quot;none&quot;<br>      }<br>    }<br><br>    min_ttl     = 0<br>    default_ttl = 3600<br>    max_ttl     = 86400<br>  }<br></pre><h3>Step 7: Provisioning &amp; Deploying the Full Stack</h3><h4>a. Build and Sync the React Frontend to S3</h4><p>Compile the React bundle and synchronize the files to your S3 bucket:</p><pre>cd client<br>npm run build<br>aws s3 sync dist/ s3://&lt;s3_bucket_name&gt;/ --delete</pre><h4>b. Build and Push the Go Container Image to ECR</h4><p><a href="https://docs.aws.amazon.com/AmazonECR/latest/userguide/repository-create.html">Create an ECR repository</a> via the management console and follow the on-screen instructions to authenticate to ECR via your terminal.</p><p>Build and then push the image to the repository:</p><pre>docker build -t go-todo-api:v2 .<br>docker tag go-todo-api:v2 &lt;repository URI&gt;:v2<br>docker push &lt;repository URI&gt;:v2</pre><h4>c. Provision Infrastructure with Terraform</h4><pre>cd infra<br>terraform init<br>terraform plan<br>terraform apply -auto-approve<br><br># Destroy configuration after you are done<br>terraform destroy</pre><p>Once Terraform completes, it outputs your CloudFront distribution URL (https://d3lirmepdaw16x.cloudfront.net), delivering your React frontend and proxying API traffic directly to your Go containers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QUDYijMPT_jYvV0ZwGBipg.png" /><figcaption>Viola! The application works as intended</figcaption></figure><h3>Conclusion</h3><p>By building our deployment around clear architectural boundaries and Infrastructure as Code, we achieved:</p><ul><li><strong>Complete Database Isolation</strong>: Amazon DocumentDB lives in a private subnet, reachable strictly from container workloads on port 27017.</li><li><strong>Zero-CORS Ingress</strong>: A multi-origin CloudFront distribution unifies static React files and dynamic Go APIs under a single HTTPS domain.</li><li><strong>97% Image Footprint Reduction</strong>: Multi-stage Docker builds reduced our container size from 850MB to just 15MB.</li></ul><p>The complete code, Terraform configurations, and architecture files for this project are available on <a href="https://github.com/achenchi7/react-go-app">GitHub</a>.</p><p><em>If you found this guide valuable, feel free to share it, leave your feedback in the comments, or connect with me on </em><a href="https://www.linkedin.com/in/jully-achenchi"><strong><em>LinkedIn</em></strong></a><em>!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de4fc8932a77" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How To Deploy FastAPI to AWS EKS with Terraform and GitHub Actions]]></title>
            <link>https://medium.com/@jullyachenchi8/how-to-deploy-fastapi-to-aws-eks-with-terraform-3c31bc712845?source=rss-7a6e9f200238------2</link>
            <guid isPermaLink="false">https://medium.com/p/3c31bc712845</guid>
            <category><![CDATA[ek]]></category>
            <category><![CDATA[fastapi]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[amazon-eks]]></category>
            <dc:creator><![CDATA[Jully Achenchi]]></dc:creator>
            <pubDate>Thu, 21 May 2026 15:35:11 GMT</pubDate>
            <atom:updated>2026-06-23T17:53:22.781Z</atom:updated>
            <content:encoded><![CDATA[<h4>A practical guide to containerization, Terraform, Kubernetes, and CI/CD on AWS</h4><p>In this guide, I will walk you through how I took a FastAPI application from a starter repo to a fully deployed, production-style cloud infrastructure on AWS, with automated pipelines, including the decisions made, the trade-offs, and how to troubleshoot if anything goes down.</p><p>This was part of the <a href="https://learntocloud.guide/">Learn To Cloud</a> learning path that I have been following for the past 2 months</p><h3>What We’ll Cover</h3><ol><li>Introduction and Architecture</li><li>Getting the Starter Repo</li><li>Provisioning Infrastructure with Terraform</li><li>Containerizing the Application</li><li>Setting Up Kubernetes Manifests</li><li>Automating Deployments with GitHub Actions</li><li>Validation and Troubleshooting</li><li>Conclusion</li></ol><h3>1. Introduction and Architecture</h3><p>We are deploying a Journal API that allows users to create, retrieve, update, and delete journal entries. The data is stored in PostgreSQL. The API is built with FastAPI, which gives you async support out of the box and automatically generates interactive documentation at /docs That is useful for testing your endpoints without a frontend.</p><p>The API itself is not the focus of this article, as it comes ready to use when you fork the starter repo. What we are building is everything around it:<strong> the infrastructure to run it reliably, the pipeline to deploy it automatically, and the observability to understand what it’s doing in production.</strong></p><p>Here is the visual of what we are building:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WidXwtK29eJvnhHB8fNB6A.png" /><figcaption>Architecture diagram</figcaption></figure><h4><strong>How it all connects:</strong></h4><p>Every git push to main triggers GitHub Actions. The pipeline runs your tests, builds a Docker image, pushes it to Amazon ECR, and deploys the new version to EKS — automatically, without touching a server.</p><h4><strong>What you will need before starting:</strong></h4><ul><li>An active AWS account</li><li>AWS CLI installed and configured (aws configure)</li><li>Terraform &gt;= 1.13 installed</li><li>Docker installed</li><li>uv installed (pip install uv)</li><li>A GitHub account</li></ul><h3>2. Getting the Starter Repo</h3><p>The application code is provided by<em> Learn to Cloud.</em></p><p>Fork the starter repo from the Learn to Cloud GitHub page:</p><pre>https://github.com/learntocloud/journal-starter</pre><p>Clone your fork locally:</p><pre>git clone https://github.com/&lt;your-username&gt;/journal-starter.git<br>cd journal-starter</pre><p>Install dependencies and verify that the app runs locally. To make the build faster and more reproducible, use uv instead of pip. uv is a modern Python package manager and resolver that installs dependencies quickly and respects the locked dependency set:</p><pre>uv sync --all-extras<br># Start the app<br>uv run uvicorn api.main:app --reload --port 8000</pre><p>Open <a href="http://localhost:8000/docs.">http://localhost:8000/docs.</a>You should see the FastAPI Swagger UI. If it loads, you&#39;re ready to build infrastructure around it.</p><h3>3. Provisioning Infrastructure with Terraform</h3><p>Create aninfra/ directory. The configuration is split into four Terraform modules: networking, security, databases, and EKS. This separation means you can update one layer without touching the others.</p><pre>infra/<br><br>├── super-main.tf                  # Module calls and wiring<br>├── super-variables.tf             # Input declarations<br>├── super-outputs.tf               # Exported values (cluster name, DB endpoint, etc.)<br>├── terraform.tfvars         # Your actual values — never commit this<br>└── modules/<br>    ├── networking/          # VPC, subnets, IGW, NAT instance, route tables<br>    ├── security/            # Security groups<br>    ├── database/           # RDS PostgreSQL<br>    └── eks/                 # EKS cluster — uses the official terraform-aws-eks module</pre><p>The resource configurations for each module can be referenced <a href="https://github.com/achenchi7/journal-starter/tree/main/Infra">here</a>. Including the code in this article will increase its verbosity and render it unreadable.</p><blockquote><strong>Important: The Official EKS Module</strong></blockquote><blockquote>For the EKS cluster, I used the <a href="https://registry.terraform.io/modules/terraform-aws-modules/eks/aws/latest">official terraform-aws-eks module</a> rather than writing the resources from scratch. It handles the control plane, node groups, IAM roles, and OIDC provider configuration that significantly reduces the amount of Terraform code you need to write and maintain.</blockquote><blockquote><strong>A note on instance types:</strong> t2.micro supports a maximum of 4 pods due to ENI and IP address limits. EKS system pods (aws-node and kube-proxy) consume 2 of those slots, leaving no room for your application. Use at minimum t3.medium, which supports 17 pods.</blockquote><h4>Deploy the infrastructure</h4><pre>cd infra/<br>cp terraform.tfvars.example terraform.tfvars<br># Fill in your values<br><br>terraform init<br>terraform plan<br>terraform apply</pre><p>Store the outputs because you will need them throughout the rest of this guide:</p><pre>terraform output cluster_name<br>terraform output -raw ecr_repository_url<br>terraform output -raw db_connection_string</pre><h3>4. Containerize the Application</h3><p>The Dockerfile uses uv for fast, deterministic dependency installation:</p><pre>FROM python:3.11-slim<br><br>COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/<br><br>WORKDIR /app<br><br>ENV UV_COMPILE_BYTECODE=1 \<br>    UV_SYSTEM_PYTHON=1<br><br>COPY pyproject.toml uv.lock ./<br>RUN uv sync --frozen --no-install-project --no-dev<br># Copying pyproject.toml before application code lets Docker cache<br># this layer — rebuilds are faster when only app code changes<br><br>COPY . .<br><br>CMD [&quot;uv&quot;, &quot;run&quot;, &quot;uvicorn&quot;, &quot;api.main:app&quot;, &quot;--host&quot;, &quot;0.0.0.0&quot;, &quot;--port&quot;, &quot;8000&quot;]</pre><p>Build and test it locally:</p><pre>docker build -t journal-api:test .<br>docker run -p 8000:8000 journal-api:test<br>curl http://localhost:8000/health<br># expected: {&quot;status&quot;:&quot;healthy&quot;}</pre><h3>5. Set Up Kubernetes Manifests</h3><p>Kubernetes uses <strong>manifests</strong> to describe the desired state of resources in the cluster. A manifest is a YAML file that tells Kubernetes what should exist, how it should behave, and how it should be connected to other resources.</p><p>This project uses three manifest files that are defined in the k8s/ directory:</p><ul><li>deployment.yaml</li><li>service.yaml</li><li>secrets.yaml.example</li></ul><h4>What each file does</h4><ol><li><strong>deployment.yml</strong></li></ol><p>The Deployment defines how the Journal API runs inside the cluster. <strong>It specifies the container image, the number of replicas, resource requests and limits, environment variables, and health probes.</strong></p><p>A Deployment is the Kubernetes object responsible for keeping the application running. If a Pod fails, the Deployment controller replaces it. If the desired replica count changes, the Deployment adjusts the number of running Pods accordingly.</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: journal-api<br>spec:<br>  replicas: 2<br>  selector:<br>    matchLabels:<br>      app: journal-api<br>  template:<br>    metadata:<br>      labels:<br>        app: journal-api<br>    spec:<br>      containers:<br>        - name: journal-api<br>          image: IMAGE_PLACEHOLDER    # CI/CD pipeline replaces this with the real tag<br>          ports:<br>            - containerPort: 8000<br>          envFrom:<br>            - secretRef:<br>                name: journal-api-secrets   # all keys injected as environment variables<br>          resources:<br>            requests:<br>              cpu: &quot;250m&quot;       # guaranteed allocation for scheduling<br>              memory: &quot;256Mi&quot;<br>            limits:<br>              cpu: &quot;500m&quot;       # hard ceiling — exceed this and the container is killed<br>              memory: &quot;512Mi&quot;<br>          readinessProbe:<br>            httpGet:<br>              path: /health<br>              port: 8000<br>            initialDelaySeconds: 10   # give the app time to start before checking<br>          livenessProbe:<br>            httpGet:<br>              path: /health<br>              port: 8000<br>            initialDelaySeconds: 15</pre><p>2. <strong>service.yml</strong></p><p>The Service exposes the application to network traffic. In this project, the Service routes traffic from port 80 to the container’s port 8000.</p><p>A Service is needed because Pods are temporary and their IP addresses can change. The Service provides a stable endpoint so users and load balancers can reach the application consistently.</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: journal-api<br>spec:<br>  type: LoadBalancer        # provisions an AWS ELB automatically<br>  selector:<br>    app: journal-api<br>  ports:<br>    - port: 80              # external port<br>      targetPort: 8000      # container port</pre><p><strong>3. </strong><strong>secrets.yaml.example</strong></p><p>The example Secret file documents the sensitive environment variables required by the application, such as DATABASE_URL.</p><p>A Kubernetes Secret stores sensitive data separately from the application image and manifest logic. The .example file makes the expected structure clear without exposing real credentials.</p><pre>apiVersion: v1<br>kind: Secret<br>metadata:<br>  name: journal-api-secrets<br>type: Opaque<br>stringData:<br>  DATABASE_URL: &quot;postgresql://username:password@rds-endpoint:5432/dbname?sslmode=require&quot;</pre><h4>How they work together</h4><p>When these manifests are applied to the cluster, Kubernetes creates the application Pods from the Deployment, injects the required secrets as environment variables, and exposes the application through the Service. This gives the API a repeatable runtime setup that can be deployed consistently across environments.</p><h3>6. Automating Deployments with GitHub Actions</h3><p>Add these secrets to your GitHub repository under Settings → Secrets and variables → Actions:</p><ul><li>AWS_ACCESS_KEY_ID gotten from IAM → Users → Security credentials</li><li>AWS_SECRET_ACCESS_KEY Same as above</li><li>AWS_REGIONYour AWS region e.g. us-east-1</li><li>ECR_REPOSITORY_URL — terraform output -raw</li><li>ecr_repository_url — EKS_CLUSTER_NAMEterraform output -raw cluster_name</li></ul><p>A <strong>CI pipeline</strong> is an automated workflow that checks code changes before they are merged or deployed. CI stands for <strong>Continuous Integration</strong>, which means code is regularly integrated into the main branch and validated automatically so problems are caught early.</p><p>Define the pipeline in the .github/workflows/ci.yml It runs whenever code is pushed to the main branch or a pull request targets main. The workflow file can be referenced <a href="https://github.com/achenchi7/journal-starter/blob/main/.github/workflows/ci.yml">here</a></p><h3>What your pipeline does</h3><p>Your pipeline has three main responsibilities:</p><ul><li><strong>Linting</strong>: The code is checked for formatting and style issues.</li><li><strong>Testing</strong>: The application is tested against a PostgreSQL service container.</li><li><strong>Deployment preparation</strong>: After tests pass on a push to main, the pipeline builds the Docker image, pushes it to Amazon ECR, and deploys the updated manifests to Amazon EKS.</li></ul><h3>8. Validation and Troubleshooting</h3><p>Validation confirms that each layer of the stack is working as intended.</p><h4>a. Install and connect kubectl to Your Cluster</h4><p>Kubectl is the official command-line interface (CLI) used to communicate with the Kubernetes API and manage your cluster.</p><pre># Download kubectl<br>curl -LO &quot;https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl&quot;<br><br># Make it executable<br>chmod +x kubectl<br><br># Move it to your PATH<br>sudo mv kubectl /usr/local/bin/kubectl<br><br># Verify installation<br>kubectl version --client</pre><h4>b. Connect kubectl to your EKS Cluster</h4><p>Kubectl needs to know which cluster to talk to. This command fetches the cluster credentials from AWS and writes them to ~/.kube/config:</p><pre>aws eks update-kubeconfig \<br>  --region us-east-1 \<br>  --name &lt;your-cluster-name&gt; #check step 3 to fetch the cluster name</pre><p>Verify the connection worked:</p><pre>kubectl cluster-info</pre><p>You should see:</p><pre>Kubernetes control plane is running at https://...eks.amazonaws.com<br>CoreDNS is running at https://....eks.amazonaws.com/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy</pre><h4>c. Verify the Cluster is Healthy</h4><p>Nodes provide the CPU, memory, and runtime environment that the application container uses.</p><pre># Are the nodes ready?<br>kubectl get nodes</pre><p>Expected:</p><pre>NAME                         STATUS   ROLES    AGE   VERSION<br>ip-&lt;the-ip&gt;.ec2.internal    Ready    &lt;none&gt;   2d    v1.31.14-eks...<br>ip-&lt;the-ip&gt;.ec2.internal   Ready    &lt;none&gt;   2d    v1.31.14-eks...</pre><p>If status showsNotReady, your node group may still be scaling up. Wait 2-3 minutes and try again.</p><h4>d. Verify the Application is Running</h4><p>The FastAPI application runs inside pods, and those pods are scheduled onto Kubernetes worker nodes in the EKS cluster.</p><pre># Are the pods running?<br>kubectl get pods</pre><p>Expected:</p><pre>NAME                           READY   STATUS    RESTARTS   AGE<br>journal-api-658674d87d-nx5nl   1/1     Running   0          10m<br>journal-api-658674d87d-rgmvv   1/1     Running   0          10m</pre><p>Both pods should show 1/1 Running and 0 restarts. If not, run:</p><pre>kubectl describe pod &lt;pod-name&gt;</pre><p>Scroll to the Events Section at the bottom. It will tell you exactly what went wrong.</p><h4>e. Get the External IP</h4><p>An <strong>external IP</strong> is the public address used to reach an application from outside the cluster. It is usually attached to a Service or provided through a cloud networking component, and it acts as the entry point for outside traffic.</p><pre>kubectl get services</pre><p>Expected:</p><pre>NAME          TYPE           CLUSTER-IP    EXTERNAL-IP                          PORT(S)<br>journal-api   LoadBalancer   172.20.81.9   a9c93a55...us-east-1.elb.amazonaws.com   80:32359/TCP</pre><p>Store it as an environment variable for further testing:</p><pre>export EXTERNAL_IP=$(kubectl get service journal-api \<br>  -o jsonpath=&#39;{.status.loadBalancer.ingress[0].hostname}&#39;)</pre><pre>echo $EXTERNAL_IP</pre><p>If EXTERNAL-IP shows &lt;pending&gt;The load balancer is still provisioning. Wait 2-3 minutes and try again.</p><h4>f. Validate the Application</h4><p><strong>Health check:</strong></p><pre>curl http://$EXTERNAL_IP/health</pre><p>Expected:</p><pre>{&quot;status&quot;:&quot;healthy&quot;}</pre><p><strong>Swagger docs are accessible:</strong></p><p>You can access the Swagger docs through the browser by visiting: <a href="http://a9c93a552608546cf9f1425201cea799-551714117.us-east-1.elb.amazonaws.com/docs">http://&lt;EXTERNAL-IP-STRING&gt;/docs</a></p><p>Expected:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H4Lzvp_HStANidxApTUnZA.png" /><figcaption>Swagger docs</figcaption></figure><h4>g. Validate the CI/CD Pipeline</h4><p>Make a small harmless change and push it:</p><pre>echo &quot;&lt;!-- validation --&gt;&quot; &gt;&gt; README.md<br>git add README.md<br>git commit -m &quot;chore: trigger validation pipeline run&quot;<br>git push</pre><p>Go to your repoand watch the pipeline run. You should see all three jobs complete:</p><pre>✅ Lint &amp; Test<br>✅ Build &amp; Push to ECR<br>✅ Deploy to EKS</pre><p>After the deploy job completes, watch the rolling update:</p><pre>kubectl get pods -w</pre><p>You will see new pods come up before old ones terminate:</p><pre>journal-api-old-xxx   1/1     Running     0    5m<br>journal-api-new-yyy   0/1     Pending     0    0s<br>journal-api-new-yyy   0/1     Running     0    3s<br>journal-api-new-yyy   1/1     Running     0    13s   ← health check passed<br>journal-api-old-xxx   1/1     Terminating 0    5m    ← now safe to kill</pre><p>Press Ctrl+C to stop watching.</p><h4>h. Validate Resilience</h4><p>Delete a pod manually and confirm Kubernetes recreates it automatically:</p><pre># Delete one pod<br>kubectl delete pod $(kubectl get pod -l app=journal-api \<br>  -o jsonpath=&#39;{.items[0].metadata.name}&#39;)</pre><pre># Watch Kubernetes bring it back<br>kubectl get pods -w</pre><p>Within 30 seconds, you should see a new pod reach Running. This is Kubernetes self-healing.</p><h3>9. Conclusion</h3><p>You have made it this far, and I applaud 👏🏽 your commitment to learning.</p><p>What started as a FastAPI starter repo is now a fully automated, production-style deployment on AWS.</p><p>I would love to hear how it goes: drop a comment if something helped, or if you hit an error I didn’t cover. And if this was useful, share it with others.</p><p>The source code for this project, including all Terraform modules, Kubernetes manifests, and the CI/CD pipeline, is available at <a href="https://github.com/achenchi7/journal-starter">github.com/achenchi7/journal-starter</a>.</p><p>Connect with me on <a href="https://www.linkedin.com/in/jully-achenchi">LinkedIn</a>, where I write about cloud infrastructure and DevOps.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3c31bc712845" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Amazon S3 Files — Making S3 buckets accessible as File Systems]]></title>
            <link>https://medium.com/@jullyachenchi8/amazon-s3-files-making-s3-buckets-accessible-as-file-systems-d3ee55963bd6?source=rss-7a6e9f200238------2</link>
            <guid isPermaLink="false">https://medium.com/p/d3ee55963bd6</guid>
            <category><![CDATA[aws-s3]]></category>
            <category><![CDATA[s3-files]]></category>
            <category><![CDATA[aws-s3-files]]></category>
            <category><![CDATA[amazon-s3-files]]></category>
            <dc:creator><![CDATA[Jully Achenchi]]></dc:creator>
            <pubDate>Fri, 10 Apr 2026 14:21:10 GMT</pubDate>
            <atom:updated>2026-04-10T14:21:10.994Z</atom:updated>
            <content:encoded><![CDATA[<h3>Amazon S3 Files</h3><h4>Making S3 buckets accessible as File Systems</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hOY9-sindjmDVblSTjWSiw.png" /><figcaption>Amazon S3 files</figcaption></figure><p>Amazon S3 has existed for 20 years, and it has been the backbone of cloud storage: cheap, durable, and infinitely scalable. In its latest development, AWS has introduced Amazon S3 Files — a new feature that lets applications running on AWS access an S3 bucket as if it were a local file system, reading and writing data using standard file operations, without moving your data anywhere.</p><h3>What is Amazon S3 Files?</h3><p>Amazon S3 Files gives your S3 bucket a shared file system interface. It is the first and only cloud object store that provides fully-featured, high-performance file system access to your data. Beyond single-instance access, S3 Files can be attached to multiple compute resources simultaneously, enabling data sharing across clusters without duplication.</p><p>Under the hood, S3 Files is built on Amazon EFS (Elastic File System) and uses a ” stage and commit” model, a concept borrowed from version control systems like Git. File operations accumulate on the file system side and are committed back to S3 as whole objects. This preserves S3’s existing guarantees (durability, consistency, the object model that millions of applications depend on) while exposing the file interface on top.</p><h3>What problem is Amazon S3 Files solving?</h3><p>To understand why this new development matters, you need to understand a specific tension in how cloud storage is <em>accessed</em> — one that has frustrated engineers since S3 launched.</p><p>There are two dominant <em>storage access interfaces</em> in cloud infrastructure:</p><ol><li><strong>File systems </strong>— <strong>File system access</strong> works the way your operating system works. You mount a volume, navigate directories, open a file, modify it in place, and save it. The changes are immediately visible to other processes on the same mount. The vast majority of data tools — Python libraries, ML frameworks, Unix-native pipelines — speak this language natively, issuing calls like open(), read(), and write().</li><li><strong>Object storage (which is what S3 is)</strong> — This works differently. There are no folders, just a flat bucket of objects, each identified by a key. To retrieve data, you call GetObject. To &quot;update&quot; an object, you overwrite it by uploading a completely new version. It is an HTTP-based request-response model, purpose-built for scalability and durability at massive scale. It is not designed to behave like a mounted drive.</li></ol><p>The problem is that a lot of software still expects a file system, not an object store. If your data lives in S3 but your tools expect files and directories, you end up building around the problem: syncing data into another storage system, duplicating datasets, or maintaining a separate storage layer just so existing applications can do their job.</p><h4>A practical case scenario</h4><p>Amara is a machine learning engineer at a fintech company. Her team has 50TB of transaction data sitting in an S3 bucket, and she needs to run a data preprocessing pipeline on it before a model training job.</p><p>Here’s what her day looks like before S3 Files:</p><ol><li>She kicks off a script that copies the relevant data from S3 into an EFS (Elastic File System) volume — because her preprocessing tool only works with a file system.</li><li>She waits. It’s a lot of data.</li><li>The preprocessing job runs on EFS.</li><li>The cleaned data gets copied back to S3.</li><li>She kicks off the training job, which reads from S3.</li></ol><p>She’s now paying for:<br>- The S3 storage (where the original data lives)<br>- The EFS storage (a temporary duplicate)<br>- The data transfer costs in and out<br>- The engineering time to build and maintain the sync pipeline<br>- The risk of the two copies drifting out of sync</p><p><strong>After S3 Files, Amara’s workflow collapses to three steps:</strong></p><ol><li>She mounts the S3 bucket as a file system.</li><li>The preprocessing job runs directly against the data in S3 using open(), read(), write() — the same calls it has always used, with no code changes.</li><li>The training job reads from the same bucket via the S3 API.</li></ol><p>It also works in both directions simultaneously — you can mount the bucket as a file system in one application and still use the standard S3 API (GetObject, PutObject) in another, against the same underlying data, at the same time.</p><h3>Who will benefit from this?</h3><h4><strong>1. ML and data engineering teams</strong></h4><p>Running preprocessing, feature engineering, and training workloads directly against S3 data without staging it first removes an entire class of infrastructure overhead. Tools that previously required a mounted volume now work against S3 without modification.</p><h4><strong>2. AI agent pipelines</strong></h4><p>Agents that need to persist memory, share state across pipeline steps, or coordinate between instances can now read and write to S3 using standard file operations. No custom state management layer required, and the state lives in durable object storage by default.</p><h4><strong>3. Platform and DevOps teams</strong></h4><p>Every sync pipeline they no longer have to maintain is engineering time returned to actual product work. The operational surface area shrinks, and with it the failure modes.</p><h4><strong>4. Application teams</strong></h4><p>Application teams who always wanted to use S3 as a shared storage backend but couldn’t because their stack expected POSIX semantics can now do so without a rewrite. The application keeps issuing file system calls; S3 Files handles the translation.</p><h3>Conclusion</h3><p>S3 Files is the most significant change to how S3 is <em>accessed</em> since the service launched. It does not add a new storage tier or a new pricing model. It changes the answer to a foundational question that cloud engineers have been working around for two decades: <em>Can I use S3 as my primary storage, even for workloads that expect a file system?</em></p><p>For the first time, the answer is yes.</p><p>If you’ve got this far, <strong>thank you for reading!</strong> I hope you learnt something.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/949/1*iHfGEXA4J2-fB6sYKxkUcA.png" /></figure><p><a href="https://www.linkedin.com/in/jully-achenchi/">Jully Achenchi</a> is a 2X AWS Certified Cloud Engineer skilled in DevOps and Technical Writing. She is motivated by her eagerness to learn and develop, and thrives in collaborative environments. She is on a mission to empower and walk with other individuals on their cloud journeys.</p><p>If you’ve got this far, <strong>thank you for reading.</strong> I hope you learnt something.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3ee55963bd6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Set Up User Auth in React with AWS Amplify and Cognito]]></title>
            <link>https://aws.plainenglish.io/how-to-set-up-user-auth-in-react-with-aws-amplify-and-cognito-73c3072971fc?source=rss-7a6e9f200238------2</link>
            <guid isPermaLink="false">https://medium.com/p/73c3072971fc</guid>
            <category><![CDATA[aws-amplify]]></category>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[amazon-cognito]]></category>
            <category><![CDATA[terraform]]></category>
            <dc:creator><![CDATA[Jully Achenchi]]></dc:creator>
            <pubDate>Thu, 07 Aug 2025 00:10:17 GMT</pubDate>
            <atom:updated>2025-08-07T13:57:46.961Z</atom:updated>
            <content:encoded><![CDATA[<h4>Implementing secure authentication flows for your cloud-native web app.</h4><p>Welcome to the next part of the <a href="https://www.youtube.com/watch?v=zA8guDqfv40&amp;t=485s&amp;pp=ygUaQVdTIENsb3VkIFByb2plY3QgQm9vdGNhbXA%3D">Cloud Project Bootcamp series</a>. In this guide, we will implement a complete user authentication flow in a React application using <strong>Amazon Cognito</strong> and <strong>AWS Amplify</strong>.</p><blockquote>If you are just joining the series, you can find previous articles here:</blockquote><p><a href="https://medium.com/@jullyachenchi8/list/9dd06852b8be">Cloud Project Bootcamp</a></p><h4>What we’ll cover</h4><ol><li>Introduction</li><li>Set up the Frontend Project</li><li>Create a Cognito User Pool with Terraform</li><li>Install and configure Amplify</li><li>Implement Sign Up and Sign In Logic</li><li>Update the docker compose file</li><li>Verify functionality</li><li>Conclusion</li></ol><h3>1. Introduction</h3><p>Here’s a visual workflow of what we are building:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DRMShbhJpQdzh_4Qlir4Eg.png" /><figcaption>Amplify Cognito flow</figcaption></figure><h4>How Amplify and Cognito work</h4><ul><li>AWS Amplify is a comprehensive set of tools that lets you build modern, cloud-enabled mobile and web applications. It also allows developers to add features such as authentication, storage, and APIs to their apps, without the need to manage the underlying infrastructure.</li><li>Cognito is an AWS service that provides managed user sign-up, sign-in, and access control through a <strong>user pool</strong></li><li>A <strong>user pool</strong> is a directory where user information is stored for registered users.</li></ul><h4>Prerequisites</h4><p>Before you begin, ensure you have:</p><ul><li>An active AWS Account</li><li>Gitpod or VS code</li><li><a href="https://docs.npmjs.com/cli/v6/commands/npm-install">npm installed</a></li><li>Terraform installed</li></ul><h3>2. Set up the Frontend project</h3><p>The application, called <strong>Cruddur</strong>, is an ephemeral micro-blogging platform similar to X(Formerly Twitter). <a href="https://github.com/omenking/aws-bootcamp-cruddur-2023/tree/main">The application code was provided</a> as part of the bootcamp repo, so you’ll be building on something real.</p><ul><li><a href="https://github.com/omenking/aws-bootcamp-cruddur-2023/fork">Fork</a> the repo to have your own copy of the application code.</li><li>Change into the frontend-react-js directory and install the project dependencies using npm.</li></ul><pre>cd frontend-react-js<br>npm install</pre><ul><li>Start the development server to view the app:</li></ul><pre>npm start</pre><p>This web application has several pages but we’ll be focusing on the <strong>sign up and sign in pages.</strong></p><h4><strong>Folder structure</strong></h4><pre>Cruddur-app/<br>├── backend-flask<br>│   ├── app.py<br>│   ├── Dockerfile<br>│   ├── requirements.txt<br>│   └── services<br>│       ├──....<br>├── cruddur-IaC # Added in step 3 of this tutorial<br>│   ├── main.tf<br>│   ├── outputs.tf<br>│   ├── provider.tf<br>│   └── variables.tf<br>├── frontend-react-js<br>│   ├── Dockerfile<br>│   ├── node_modules/<br>|       ├──...<br>|   ├── src/<br>│   │   ├──...<br>└── docker-compose.yml</pre><h3>3. Create a Cognito User Pool with Terraform</h3><p>We will define our authentication infrastructure using <strong>Terraform</strong> for better reproducibility and infrastructure management.</p><p>In the root of the project, create a folder called cruddur-IaC and within the folders, create the following files; main.tf and outputs.tf files. In the files, input the following lines of code:</p><p><em>I have explained what the code does inline</em></p><p>Main.tf</p><pre># State the provider to use (AWS), where to download it from (source)<br># Specify the region (us-east-1)<br><br>terraform {<br>  required_providers {<br>    aws = {<br>        source = &quot;hashicorp/aws&quot;<br>    }<br>  }<br>}<br><br>provider &quot;aws&quot; {<br>  region = &quot;us-east-1&quot;<br>}<br><br>## Create a user-pool in Amazon Cognito. This is &#39;where&#39; users are stored<br>resource &quot;aws_cognito_user_pool&quot; &quot;cruddurpool&quot; {<br>  name = &quot;Cruddur-pool-1&quot; <br>  username_attributes = [ &quot;email&quot; ] # Specifies that users will log in with their emails<br>  auto_verified_attributes = [ &quot;email&quot; ] # Cognito will automatically verify the user&#39;s email (e.g., via confirmation code).<br><br>  # Specify the pasword rules to your users<br>  password_policy {<br>    minimum_length    = 8<br>    require_lowercase = true<br>    require_uppercase = true<br>    require_numbers   = true<br>    require_symbols   = true<br>  }<br>  <br>  # If someone forgets their password, they can reset it via their verified email<br>  account_recovery_setting {<br>    recovery_mechanism {<br>      name     = &quot;verified_email&quot;<br>      priority = 1<br>    }<br>  }<br><br>  mfa_configuration = &quot;OFF&quot;<br>  <br>}<br><br># Create an app client. This is &#39;how&#39; users are allowed in<br># App Client represents an application (In this case our Cruddur web app) that interacts with the user pool to authenticate users.<br>resource &quot;aws_cognito_user_pool_client&quot; &quot;cruddur_client&quot; {<br>  name = &quot;cruddur-pool-client&quot;<br>  user_pool_id = aws_cognito_user_pool.cruddurpool.id<br>  <br>  # An Auth flow refer to the methods Cognito uses to verify a user&#39;s identity when they try to sign in<br>  # ALLOW_USER_PASSWORD_AUTH- User logs in with username/email and password<br>  # ALLOW_REFRESH_TOKEN_AUTH- Allows app to use a refresh token to get new access tokens (used to keep users signed in)<br>  # ALLOW_USER_SRP_AUTH- Secure Remote Password protocol – a secure way to log in without sending the actual password<br>  explicit_auth_flows = [ &quot;ALLOW_USER_PASSWORD_AUTH&quot;, &quot;ALLOW_REFRESH_TOKEN_AUTH&quot;, &quot;ALLOW_USER_SRP_AUTH&quot; ] <br>  generate_secret = false<br>  prevent_user_existence_errors = &quot;LEGACY&quot;<br>  refresh_token_validity = 1<br>  id_token_validity = 1<br>  token_validity_units {<br>    access_token  = &quot;hours&quot;<br>    id_token      = &quot;hours&quot;<br>    refresh_token = &quot;hours&quot;<br>  }<br>}</pre><p>outputs.tf</p><pre>output &quot;user_pool_id&quot; {<br>  description = &quot;The ID of the Cognito User Pool.&quot;<br>  value       = aws_cognito_user_pool.cruddurpool.id<br>}<br><br>output &quot;user_pool_arn&quot; {<br>  description = &quot;The ARN of the Cognito User Pool.&quot;<br>  value       = aws_cognito_user_pool.cruddurpool.arn<br>}<br><br>output &quot;user_pool_endpoint&quot; {<br>  description = &quot;The endpoint of the Cognito User Pool.&quot;<br>  value       = aws_cognito_user_pool.cruddurpool.endpoint<br>}<br><br>output &quot;user_pool_client_id&quot; {<br>  description = &quot;The ID of the associated User Pool Client.&quot;<br>  value       = aws_cognito_user_pool_client.cruddur_client.id<br>}</pre><p>Deploy the configured infrastructure:</p><pre>terraform init<br>terraform plan<br>terraform apply</pre><blockquote><em>After deploying the resources, store the output values in a temporary file. You’ll need them shortly</em></blockquote><p>To verify, log into your AWS management console and navigate to Amazon cognito via the search bar to check the created user pool:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7bUuaV2h4mhrwJ5bg7po2g.png" /><figcaption>Confirmed user pool created</figcaption></figure><h3>4. Install and Configure AWS Amplify</h3><p>Navigate to the frontend-react-js directory and install the Amplify Auth module:</p><pre>npm install @aws-amplify/auth</pre><h4>Configure Amplify in App.js:</h4><p>You will then integrate Amplify to our frontend in the App.js file.</p><p>In the App.js file, add the following lines of code to the existing code.</p><pre>import { Amplify } from &#39;aws-amplify&#39;;<br><br>Amplify.configure({<br>  Auth: {<br>    region: process.env.REACT_APP_AWS_COGNITO_REGION,<br>    userPoolId: process.env.REACT_APP_AWS_USER_POOLS_ID,<br>    userPoolWebClientId: process.env.REACT_APP_AWS_USER_POOLS_WEB_CLIENT_ID,<br>    identityPoolId: process.env.REACT_APP_AWS_COGNITO_IDENTITY_POOL_ID,<br>  },<br>  oauth: {},<br>});</pre><ul><li>Auth: — This object is passed to Amplify.configure() and specifically targets the <strong>authentication module</strong> of Amplify.</li><li>region — This is the AWS region where your Cognito resources were created</li><li>userPoolId — The unique identifier for your <strong>User Pool</strong>.</li><li>userPoolWebClientId — This is the <strong>App Client ID</strong> from your User Pool, which represents the frontend application making auth requests.</li><li>oauth (optional) — Used if you’re integrating OAuth providers like Google, Facebook, or Apple.</li></ul><h4>5. Implement Sign Up and Sign In Logic</h4><p><strong>Update </strong><strong>SignupPage.js</strong></p><p>Navigate to the pages\SignupPage.js and update the file to use Amplify. Use the following code snippet:</p><pre>import { Auth } from &#39;aws-amplify&#39;;<br><br>const [cognitoErrors, setCognitoErrors] = React.useState(&#39;&#39;);<br><br>const onsubmit = async (event) =&gt; {<br>  event.preventDefault();<br>  setCognitoErrors(&#39;&#39;)<br>  try {<br>      const { user } = await Auth.signUp({<br>        username: email,<br>        password: password,<br>        attributes: {<br>            name: name,<br>            email: email,<br>            preferred_username: username,<br>        },<br>        autoSignIn: { // optional - enables auto sign in after user is confirmed<br>            enabled: true,<br>        }<br>      });<br>      console.log(user);<br>      window.location.href = `/confirm?email=${email}`<br>  } catch (error) {<br>      console.log(error);<br>      setCognitoErrors(error.message)<br>  }<br>  return false<br>}<br><br>let errors;<br>if (cognitoErrors){<br>  errors = &lt;div className=&#39;errors&#39;&gt;{cognitoErrors}&lt;/div&gt;;<br>}<br><br>//before submit component<br>{errors}</pre><p>Inside onsubmit:</p><ul><li>event.preventDefault()-Prevents the default page reload on form submission.</li><li>setCognitoErrors(&#39;&#39;):Clears previous errors before a new attempt.</li><li>Auth.signUp({ ... }) : Triggers user registration with the following:</li><li>username: the user’s email.</li><li>password: chosen password.</li><li>attributes: additional user info (name, email, and preferred username).</li><li>autoSignIn.enabled: attempts auto sign-in after confirmation (optional).</li></ul><p>console.log(user):Logs user data on successful sign-up.</p><p>window.location.href = /confirm?email=${email} : Redirects user to confirmation page, passing their email in the query string.</p><p>catch (error) {...} :Catches and logs any sign-up errors.</p><p>return false :Prevents any further default behavior.</p><p><strong>let errors; if (cognitoErrors) {...}</strong> : If there’s a Cognito error, renders a styled error message.</p><p><strong>{errors}</strong> :JSX placeholder to visually display the error message before the form submit button.</p><p><strong>Update the </strong><strong>SigninPage.js</strong></p><p>Navigate to the pages\SigninPage.js and update the file to use Amplify. Use the following code snippet:</p><pre>import { Auth } from &#39;aws-amplify&#39;;<br><br>const [cognitoErrors, setCognitoErrors] = React.useState(&#39;&#39;);<br><br>const onsubmit = async (event) =&gt; {<br>  setCognitoErrors(&#39;&#39;)<br>  event.preventDefault();<br>  try {<br>    Auth.signIn(username, password)<br>      .then(user =&gt; {<br>        localStorage.setItem(&quot;access_token&quot;, user.signInUserSession.accessToken.jwtToken)<br>        window.location.href = &quot;/&quot;<br>      })<br>      .catch(err =&gt; { console.log(&#39;Error!&#39;, err) });<br>  } catch (error) {<br>    if (error.code == &#39;UserNotConfirmedException&#39;) {<br>      window.location.href = &quot;/confirm&quot;<br>    }<br>    setCognitoErrors(error.message)<br>  }<br>  return false<br>}<br><br>let errors;<br>if (cognitoErrors){<br>  errors = &lt;div className=&#39;errors&#39;&gt;{cognitoErrors}&lt;/div&gt;;<br>}<br><br>// just before submit component<br>{errors}</pre><ul><li>import { Auth } from &#39;aws-amplify&#39;;: Imports the Auth module from AWS Amplify for user authentication.</li><li>const [cognitoErrors, setCognitoErrors] = React.useState(&#39;&#39;);: Initializes a state variable to store Cognito authentication error messages.</li><li>const onsubmit = async (event) =&gt; {...}: — Prevents the default form submission, clears previous errors and attempts to sign in the user with Auth.signIn(username, password)If successful, it stores the access token in localStorage and redirects the user to the homepage (/).</li><li>let errors; if (cognitoErrors) {...}: Conditionally renders error messages below the form if any exist.</li><li>{errors}: Displays the error message in the component just before the submit button or form end.</li></ul><h3>6. Update the Docker compose file</h3><p>To pass environment variables into the React frontend via Docker, add the following to the frontend service in your docker-compose.yml:</p><pre>REACT_APP_AWS_PROJECT_REGION: ${AWS_DEFAULT_REGION}<br>REACT_APP_AWS_COGNITO_IDENTITY_POOL_ID:<br>REACT_APP_AWS_COGNITO_REGION: ${AWS_DEFAULT_REGION}<br>REACT_APP_AWS_USER_POOLS_ID: &quot;&lt;your-user-pool-id&gt;&quot;<br>REACT_APP_CLIENT_ID: &quot;&lt;your-app-client-id&gt;&quot;</pre><h3>6. Verify</h3><p>Throughout this bootcamp series, we’ve been using <strong>Docker</strong> to run and manage our application. If you’re unfamiliar with how Docker Compose works in this context, check out my related <a href="https://medium.com/@jullyachenchi8/how-to-containerize-a-flask-and-react-app-with-docker-1c4089922c9f">article</a>.</p><p>To verify your authentication setup:</p><ul><li>Start the app</li></ul><pre>docker-compose up -d --build</pre><ul><li>Open the app in your browser on <strong>port 3000</strong></li><li>Register a new user and sign in</li><li>Confirm that user data appears in your <strong>Cognito User Pool &gt; Users</strong> section in the AWS Console</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xg9qOaSb2qCF9LdiAPZJoA.png" /><figcaption>Verified user in Cognito</figcaption></figure><p>Success! Your app is now using Cognito for secure user authentication</p><h3>8. Conclusion</h3><p>In this tutorial, we integrated <strong>Amazon Cognito</strong> and <strong>AWS Amplify</strong> into a React application to implement secure user authentication. We used <strong>Terraform</strong> to define infrastructure as code, and Docker for easy deployment.</p><p>I would love to hear your thoughts, drop a comment and a 👏🏽 if it resonated. You can also connect with me ✍🏽<a href="https://www.linkedin.com/in/jully-achenchi"><strong>Linkedin</strong></a></p><h3>A message from our Founder</h3><p><strong>Hey, </strong><a href="https://linkedin.com/in/sunilsandhu"><strong>Sunil</strong></a><strong> here.</strong> I wanted to take a moment to thank you for reading until the end and for being a part of this community.</p><p>Did you know that our team run these publications as a volunteer effort to over 200k supporters? <strong>We do not get paid by Medium</strong>!</p><p>If you want to show some love, please take a moment to <strong>follow me on </strong><a href="https://linkedin.com/in/sunilsandhu"><strong>LinkedIn</strong></a><strong>, </strong><a href="https://tiktok.com/@messyfounder"><strong>TikTok</strong></a><strong> and </strong><a href="https://instagram.com/sunilsandhu"><strong>Instagram</strong></a>. And before you go, don’t forget to <strong>clap</strong> and <strong>follow</strong> the writer️!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=73c3072971fc" width="1" height="1" alt=""><hr><p><a href="https://aws.plainenglish.io/how-to-set-up-user-auth-in-react-with-aws-amplify-and-cognito-73c3072971fc">How to Set Up User Auth in React with AWS Amplify and Cognito</a> was originally published in <a href="https://aws.plainenglish.io/">AWS in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Containerize a Flask and React App with Docker]]></title>
            <link>https://medium.com/@jullyachenchi8/how-to-containerize-a-flask-and-react-app-with-docker-1c4089922c9f?source=rss-7a6e9f200238------2</link>
            <guid isPermaLink="false">https://medium.com/p/1c4089922c9f</guid>
            <category><![CDATA[notesfromacloudproject]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Jully Achenchi]]></dc:creator>
            <pubDate>Thu, 24 Jul 2025 07:34:35 GMT</pubDate>
            <atom:updated>2025-07-24T07:34:35.222Z</atom:updated>
            <content:encoded><![CDATA[<h4>A beginner-friendly guide to running a full-stack application using Docker and Docker Compose.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*ZHwnJRzT4FHGCa6-03z3dw.png" /><figcaption>High level Containerization process</figcaption></figure><blockquote>This guide walks you through how to Containerize a Flask and React App with Docker , a process I completed as part of the <a href="https://www.youtube.com/watch?v=8b8SvQHc4Pk">AWS Cloud Project Bootcamp</a> by Andrew Brown.</blockquote><p>If you have ever tried to run an application that comes with both a backend and a frontend, you may have realized quickly: getting both parts to work seamlessly on your local machine can get quite messy.</p><p>That’s where <a href="https://www.docker.com/"><strong>Docker</strong></a> comes in. It lets you run each part of the application in its own environment (called a container), without worrying about conflicting software, system versions, or weird setup issues.</p><p>In this article, you will learn how to containerize a full-stack app with:</p><ul><li>A <strong>Flask (Python)</strong> backend</li><li>A <strong>React (Node.js)</strong> frontend</li><li>…and then connect them using <strong>Docker Compose</strong></li></ul><p>This setup was part of a larger cloud engineering project I’m working through — but everything here is beginner-friendly and useful even as a standalone learning experience.</p><h3>Pre-requisite</h3><p>Have Docker installed in your development environment</p><h3>Folder Structure</h3><p>You can follow along using the provided application code <a href="https://github.com/ExamProCo/aws-bootcamp-cruddur-2023">here</a>. <a href="https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/fork-a-repo">Fork</a> the repo and let’s get to work.</p><p>Before we dive into code, here’s how the project was organized:</p><pre>project-root/<br>├── backend-flask/<br>│   ├── app.py<br>│   ├── requirements.txt<br>│   └── ...<br>├── frontend-react-js/<br>│   ├── package.json<br>│   ├── src/<br>    ├── node_modules/<br>│   └── ...<br>└── docker-compose.yml  # added later</pre><h3>Step 1: Containerize the Backend (Flask + Python)</h3><p>Create a Dockerfile in the backend-flask folder.</p><p>A <strong>Dockerfile</strong> is a simple text file that tells Docker exactly how to build a container for your app; which base image to use, which dependencies to install, and how to start the app.</p><h4>Create the Dockerfile</h4><p>Inside backend-flask/, create a file named Dockerfile:</p><pre>cd backend-flask<br>touch Dockerfile</pre><p>Populate the following commands:</p><pre>FROM python:3.10-slim-buster<br>WORKDIR /backend-flask<br>COPY requirements.txt requirements.txt<br>RUN pip3 install -r requirements.txt<br>COPY . .<br>ENV FLASK_ENV=development<br>EXPOSE 4567<br>CMD [&quot;python3&quot;, &quot;-m&quot;, &quot;flask&quot;, &quot;run&quot;, &quot;--host=0.0.0.0&quot;, &quot;--port=4567&quot;]</pre><p><strong>Explanation</strong>:</p><ul><li>FROM: This defines the base image — in this case, a minimal version of Python 3.10.</li><li>WORKDIR: Sets the working directory inside the container.</li><li>COPY + RUN: Installs the app’s Python dependencies.</li><li>CMD: Tells the container how to start the Flask server.</li></ul><h4>Build the image</h4><p>In your terminal, from the project root:</p><pre>docker build -t backend-flask ./backend-flask</pre><ul><li>-t backend-flask gives the image a name.</li><li>./backend-flask points to the folder containing the Dockerfile.</li></ul><h4>Run the container</h4><pre>docker run --rm -d -p 4567:4567 -it -e FRONTEND_URL=&quot;*&quot; -e BACKEND_URL=&quot;*&quot; backend-flask</pre><p><strong>Flags explained</strong>:</p><ul><li>--rm: Automatically deletes the container when it stops.</li><li>-d: Runs it in the background.</li><li>-p: Maps port 4567 inside the container to port 4567 on your local machine.</li><li>-e: Sets environment variables.</li></ul><p>To confirm it’s running:</p><pre>docker ps           # View running containers<br>docker logs &lt;id&gt;    # View logs<br>docker exec -it &lt;id&gt; /bin/bash  # Enter the container<br></pre><p>On your ports tab on the terminal, make port 4567 public and visit the URL provided.</p><h3>Step 2: Containerize the Frontend (React + Node)</h3><pre>cd frontend-react-js</pre><h4>Install dependencies</h4><p>Navigate to frontend-react-js/ folder and run the following command.</p><pre>npm install</pre><p>npm install is a command used innode.js projects to install dependencies. It reads the package.json file and installs the specified packages, including their dependencies in the node_modules folder.</p><h4>Create the Dockerfile</h4><p>In the same frontend-react-js folder, create a Dockerfile</p><pre>cd frontend-react-js<br>touch Dockerfile</pre><p>Populate the following commands:</p><pre>FROM node:16.18<br>ENV PORT=3000<br>COPY . /frontend-react-js<br>WORKDIR /frontend-react-js<br>RUN npm install<br>EXPOSE 3000<br>CMD [&quot;npm&quot;, &quot;start&quot;]</pre><p><strong>Explanation</strong>:</p><ul><li>Uses node:16.18 as the base image.</li><li>COPY . /frontend-react-js : Copies everything from your local directory (where the Dockerfile lives) into the container’s /frontend-react-js directory.</li><li>WORKDIR /frontend-react-js : Changes the working directory to /frontend-react-js inside the container and all following commands will run from here.</li><li>RUN npm install: Installs app dependencies.</li><li>EXPOSE 3000 :Tells Docker that the container will listen on port 3000.</li></ul><h4>Build and run the container</h4><pre>docker build -t frontend-react-js ./frontend-react-js<br>docker run -d -p 3000:3000 frontend-react-js</pre><p>Visit http://localhost:3000 to view the app if running locally.</p><h3>Step 3: Connect Both Apps Using Docker Compose</h3><p>Managing multiple containers manually gets tedious. Docker Compose helps you define and run multi-container applications using a single file.</p><h4>Create docker-compose.yml</h4><p>At the root of your project create a file named docker-compose.yml</p><pre>version: &quot;3.8&quot;<br>services:<br>  backend-flask:<br>    build: ./backend-flask<br>    ports:<br>      - &quot;4567:4567&quot;<br>    environment:<br>      FRONTEND_URL: &quot;http://localhost:3000&quot;<br>      BACKEND_URL: &quot;http://localhost:4567&quot;<br>  frontend-react-js:<br>    build: ./frontend-react-js<br>    ports:<br>      - &quot;3000:3000&quot;<br>    environment:<br>      REACT_APP_BACKEND_URL: &quot;http://localhost:4567&quot;</pre><p>Then run everything with:</p><pre>docker-compose up --build</pre><p>You now have two containers running:</p><ul><li>Flask backend on port 4567</li><li>React frontend on port 3000</li></ul><p>And they’re configured to talk to each other.</p><p>On your ports tab, visit the URL provided on port 3000</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eOBRg_zL958AuoQuLOTFeA.png" /><figcaption>The working application website</figcaption></figure><h3>Conclusion</h3><p>Even though I didn’t write the application code myself, this process helped me understand how production-grade applications are packaged and run in cloud environments.</p><p><em>🔗 </em>This article is part of my ongoing “Notes from a Cloud Project” series. <em>You can follow along via my tag:</em> <strong>#NotesFromACloudProject. </strong><em>Check out the previous piece: </em><a href="https://medium.com/@jullyachenchi8/how-to-set-up-cost-controls-in-aws-using-the-aws-cli-b8feeda3d921"><strong><em>How to set up cost controls in AWS using the AWS CLI</em></strong></a></p><p>I would love to hear your thoughts, drop a comment and a 👏🏽 if it resonated. You can also connect with me ✍🏽<a href="https://www.linkedin.com/in/jully-achenchi"><strong>Linkedin</strong></a> | 🖊️<a href="https://medium.com/@jullyachenchi8"><strong>Medium</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1c4089922c9f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to set up cost controls in AWS using the AWS CLI]]></title>
            <link>https://aws.plainenglish.io/how-to-set-up-cost-controls-in-aws-using-the-aws-cli-b8feeda3d921?source=rss-7a6e9f200238------2</link>
            <guid isPermaLink="false">https://medium.com/p/b8feeda3d921</guid>
            <category><![CDATA[notesfromacloudproject]]></category>
            <category><![CDATA[aws-cli]]></category>
            <category><![CDATA[aws-sns]]></category>
            <category><![CDATA[cloudwatch]]></category>
            <category><![CDATA[aws-billing]]></category>
            <dc:creator><![CDATA[Jully Achenchi]]></dc:creator>
            <pubDate>Wed, 09 Jul 2025 17:20:45 GMT</pubDate>
            <atom:updated>2025-08-06T00:05:51.463Z</atom:updated>
            <content:encoded><![CDATA[<h4>AWS Budgets and Billing setup via AWS CLI</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5gZ5Fq8RW6wzfe-Rw6Ln5Q.jpeg" /><figcaption>Cloud costs can be a menace</figcaption></figure><h3>Introduction</h3><p>Whether you are working on a personal cloud project or managing infrastructure at scale, one truth remains: <strong>unmonitored cloud costs can get out of hand quickly</strong>. For AWS users, setting up cost controls early isn’t just best practice, it is essential.</p><p>This guide walks through how to programmatically set up cost controls in AWS using the AWS CLI , a process I completed as part of the <a href="https://www.youtube.com/watch?v=8b8SvQHc4Pk">AWS Cloud Project Bootcamp</a> by Andrew Brown.</p><p>In this post, you will learn how to:</p><ul><li>Install the AWS CLI in a cloud-based development environment (Gitpod)</li><li>Set up environment variables for secure access</li><li>Enable billing alerts through AWS CLI</li><li>Create an SNS topic and email subscription</li><li>Define a budget and CloudWatch alarm via JSON templates</li></ul><p>This setup ensures you get notified <em>before</em> unexpected charges build up and it is entirely automated.</p><h3>1. Install the AWS CLI (on Gitpod or Linux)</h3><p>I am using <a href="https://www.gitpod.io/">Gitpod</a>, a cloud-based dev environment. Here’s how to install the AWS CLI if it is not already present:</p><pre>curl &quot;https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip&quot; -o &quot;awscliv2.zip&quot;<br>unzip awscliv2.zip<br>sudo ./aws/install</pre><p>Verify the installation:</p><pre>aws --version</pre><h3>2. Configuring AWS Environment Variables</h3><p>To enable programmatic access, export your credentials and region:</p><pre>export AWS_ACCESS_KEY_ID=&quot;your-access-key&quot;<br>export AWS_SECRET_ACCESS_KEY=&quot;your-secret-key&quot;<br>export AWS_DEFAULT_REGION=us-east-1</pre><p>Gitpod is an ephemeral environment, meaning that when you exit a Gitpod session, all your set variables are deleted. Use Gitpod’s gp env command to persist these variables between sessions:</p><pre>gp env AWS_ACCESS_KEY_ID=&quot;your-access-key&quot;<br>gp env AWS_SECRET_ACCESS_KEY=&quot;your-secret-key&quot;<br>gp env AWS_DEFAULT_REGION=us-east-1</pre><p>Check your credentials:</p><pre>aws sts get-caller-identity</pre><p>Expected output:</p><pre>{<br>  &quot;UserId&quot;: &quot;...&quot;,<br>  &quot;Account&quot;: &quot;...&quot;,<br>  &quot;Arn&quot;: &quot;arn:aws:iam::...:user/your-user&quot;<br>}</pre><h3>3. Enabling Billing Alerts with SNS and CloudWatch</h3><p>AWS does not enable billing alerts by default. You will first need to enable them from the console using the <strong>root account</strong> (Settings → Billing Preferences → Turn on alerts).</p><h4>3.1 Create an SNS Topic</h4><p>This topic will handle your cost notification messages.</p><pre>aws sns create-topic --name billing-alarm</pre><p>You’ll receive a TopicArn in the output.</p><h4>3.2 Create an Email Subscription</h4><p>Use your email to subscribe to the topic:</p><pre>aws sns subscribe \<br>  --topic-arn &lt;topicARN&gt; \<br>  --protocol email \<br>  --notification-endpoint your-email@example.com</pre><p><strong>Confirm the subscription</strong> via the email AWS sends you.</p><h3>4. Create a CloudWatch Alarm for Billing</h3><p>Create a JSON file that defines your CloudWatch alarm config (e.g. billing threshold in USD):</p><pre>{<br>  &quot;AlarmName&quot;: &quot;DailyEstimatedCharges&quot;,<br>  &quot;AlarmDescription&quot;: &quot;Trigger when estimated charges exceed $2&quot;,<br>  &quot;ActionsEnabled&quot;: true,<br>  &quot;AlarmActions&quot;: [&quot;&lt;topicARN&gt;&quot;],<br>  &quot;MetricName&quot;: &quot;EstimatedCharges&quot;,<br>  &quot;Namespace&quot;: &quot;AWS/Billing&quot;,<br>  &quot;Statistic&quot;: &quot;Maximum&quot;,<br>  &quot;Dimensions&quot;: [{&quot;Name&quot;: &quot;Currency&quot;, &quot;Value&quot;: &quot;USD&quot;}],<br>  &quot;Period&quot;: 21600,<br>  &quot;EvaluationPeriods&quot;: 1,<br>  &quot;Threshold&quot;: 10.0,<br>  &quot;ComparisonOperator&quot;: &quot;GreaterThanThreshold&quot;<br>}</pre><p>Run the command to apply it:</p><pre>aws cloudwatch put-metric-alarm \<br>  --cli-input-json file://cloudwatch_alarm_config.json</pre><h3>5. Create an AWS Budget</h3><p>To set a budget and receive notifications, you will first need your AWS Account ID:</p><pre>aws sts get-caller-identity --query Account --output text</pre><p>Then prepare two JSON files:</p><p><strong>budget.json</strong></p><pre>{<br>  &quot;BudgetName&quot;: &quot;Cruddur Project Budget&quot;,<br>  &quot;BudgetLimit&quot;: {<br>    &quot;Amount&quot;: &quot;10.0&quot;,<br>    &quot;Unit&quot;: &quot;USD&quot;<br>  },<br>  &quot;TimeUnit&quot;: &quot;MONTHLY&quot;,<br>  &quot;BudgetType&quot;: &quot;COST&quot;,<br>  &quot;TimePeriod&quot;: {<br>    &quot;Start&quot;: &quot;2024-01-01T00:00:00Z&quot;,<br>    &quot;End&quot;: &quot;2087-01-01T00:00:00Z&quot;<br>  }<br>}</pre><p><strong>budget-notifications-with-subscribers.json</strong></p><pre>[<br>  {<br>    &quot;Notification&quot;: {<br>      &quot;NotificationType&quot;: &quot;ACTUAL&quot;,<br>      &quot;ComparisonOperator&quot;: &quot;GREATER_THAN&quot;,<br>      &quot;Threshold&quot;: 80.0,<br>      &quot;ThresholdType&quot;: &quot;PERCENTAGE&quot;<br>    },<br>    &quot;Subscribers&quot;: [<br>      {<br>        &quot;SubscriptionType&quot;: &quot;EMAIL&quot;,<br>        &quot;Address&quot;: &quot;your-email@example.com&quot;<br>      }<br>    ]<br>  }<br>]</pre><p>Then run:</p><pre>aws budgets create-budget \<br>  --account-id &lt;your-account-id&gt; \<br>  --budget file://budget.json \<br>  --notifications-with-subscribers file://budget-notifications-with-subscribers.json</pre><h3>Verify</h3><p>Log into your AWS account and verify that indeed the SNS topic has been created, the budget is present and the cloudwatch alarm has been set.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XQdf8zIRFSRt1RyWCNDFPQ.png" /><figcaption>SNS Topic successfully created</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sCx5T6y3KKMbHNjC54dNSA.png" /><figcaption>Budget successfully created</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HG4MQP4-3l_fCjJ0Mb-kpA.png" /><figcaption>CloudWatch alarm has been set</figcaption></figure><h3>Final Thoughts</h3><p>Setting up cost controls early in your AWS journey is essential. These guardrails will keep you accountable and alert.</p><p>And the best part? You can automate it all.</p><p>See you in the next one. Don’t forget to leave a comment and a 👏🏽. <em>You can connect with me on</em> ✍🏽<a href="https://www.linkedin.com/in/jully-achenchi"><strong>Linkedin</strong></a> | 🖊️<a href="https://medium.com/@jullyachenchi8"><strong>Medium</strong></a></p><p><em>This article is part of a hands-on series I’m writing as I work through the AWS Cloud Project Bootcamp. You can follow along via my tag:</em> <strong>#NotesFromACloudProject</strong></p><h3>A message from our Founder</h3><p><strong>Hey, </strong><a href="https://linkedin.com/in/sunilsandhu"><strong>Sunil</strong></a><strong> here.</strong> I wanted to take a moment to thank you for reading until the end and for being a part of this community.</p><p>Did you know that our team run these publications as a volunteer effort to over 200k supporters? <strong>We do not get paid by Medium</strong>!</p><p>If you want to show some love, please take a moment to <strong>follow me on </strong><a href="https://linkedin.com/in/sunilsandhu"><strong>LinkedIn</strong></a><strong>, </strong><a href="https://tiktok.com/@messyfounder"><strong>TikTok</strong></a><strong> and </strong><a href="https://instagram.com/sunilsandhu"><strong>Instagram</strong></a>. And before you go, don’t forget to <strong>clap</strong> and <strong>follow</strong> the writer️!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b8feeda3d921" width="1" height="1" alt=""><hr><p><a href="https://aws.plainenglish.io/how-to-set-up-cost-controls-in-aws-using-the-aws-cli-b8feeda3d921">How to set up cost controls in AWS using the AWS CLI</a> was originally published in <a href="https://aws.plainenglish.io/">AWS in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Step-by-Step Guide: Deploy a Scalable 3-Tier Architecture on AWS with Terraform Modules.]]></title>
            <link>https://awstip.com/how-to-deploy-a-highly-available-3-tier-architecture-in-aws-using-terraform-8ac25fe3910b?source=rss-7a6e9f200238------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ac25fe3910b</guid>
            <category><![CDATA[iac]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[terraform-modules]]></category>
            <dc:creator><![CDATA[Jully Achenchi]]></dc:creator>
            <pubDate>Tue, 11 Feb 2025 09:16:58 GMT</pubDate>
            <atom:updated>2025-02-18T15:22:41.901Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>Let Terraform do the heavy lifting while you sip coffee.</strong> ☕</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8h9P7I1fk7gUcm49KdW6ew.png" /></figure><pre>print (&quot;Hello there, Welcome back!&quot;)</pre><p>Welcome back to yet another moment of learning and building.</p><p>You can follow along for free <a href="https://medium.com/@jullyachenchi8/how-to-deploy-a-highly-available-3-tier-architecture-in-aws-using-terraform-8ac25fe3910b?source=friends_link&amp;sk=27ea311147511f79d7004251eefdef2d">here</a> 😉</p><h3>Objective</h3><p>This article aims to guide you step-by-step on how to design and deploy a secure, scalable, and highly available 3-tier architecture in AWS using Terraform modules.</p><h3>Introduction</h3><p>If you’ve worked with Terraform and AWS, you’ve heard the term“3-Tier architecture” being used several times. If you’ve not heard, well, that’s why I am here 😏.</p><p><strong>A 3-tier architecture</strong> is a widely used architectural pattern that splits an application into <strong>three distinct layers</strong>, each handling a different part of the workload. These three layers are:</p><p><strong>The Web/Presentation tier ( Front End) </strong>— This is the user-facing side of the application. It is the user interface of your application that allows people to interact with your application</p><p><strong>Application Tier</strong> — This tier is responsible for communication between the presentation and database tiers. Information or requests gathered from the web tier are processed here.</p><p><strong>The database Tier </strong>— This is the component that stores data, and it is built using a database management system</p><h4>Pre-requisites</h4><p>Before we dive in, make sure you have the following:</p><ul><li>An AWS Account with an IAM user with admin privileges. Here’s a guide</li><li>Terraform installed and configured</li><li>A Code editor, preferably Visual Studio Code.</li></ul><h3>Business use case — Scaling NoKnots E-commerce Platform</h3><p>You work at <strong>NoKnots Company</strong>, a startup specializing in E-commerce. NoKnots intends to launch its first product, an E-commerce store that is expected to handle hundreds of thousands of requests per month.</p><h4>The Challenge?</h4><ul><li>Traffic surges by 15% bi-monthly on Fridays due to Flash sales.</li></ul><p>You have been called in as a Cloud Engineer to build a highly available, scalable, and fault-tolerant infrastructure on which the application will thrive.</p><h4><strong>The twist?</strong></h4><p>The entire infrastructure must be deployed using Terraform.</p><h4>The Solution</h4><p>Build a highly available, scalable, and fault-tolerant 3-tier architecture on AWS. The composition of the 3 tiers will be:</p><p><strong>The web/presentation tier will consist of:</strong></p><ul><li>Two EC2 instances</li><li>Two public subnets for the EC2 instances</li><li>An auto-scaling group that will automatically launch more instances to handle traffic.</li><li>An internet gateway to allow inbound internet access to the EC2 instances</li><li>A public route table and associate the public subnets to it.</li></ul><p><strong>The application tier will consist of:</strong></p><ul><li>Two private subnets for security</li><li>Two private EC2 instances for processing user requests</li><li>An auto-scaling group to dynamically adjust capacity</li><li>Elastic IP address for consistent connectivity</li><li>NAT Gateway for internet access without exposing the private instances</li></ul><p><strong>The database tier will consist of:</strong></p><ul><li>MySQL RDS Database with Multi-az functionality for high availability</li><li>A security group to allow inbound traffic from the application tier</li><li>Private route table</li></ul><blockquote><strong>(Tip</strong>: It’s a long read, so why not take a cup of coffee, tea, or wine — whatever’s your favorite?)</blockquote><h4>Let’s Begin!</h4><p>But first, an Architectural diagram to visualize the solution:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*I-D4SGzloOdH8_FcL23d8g.png" /><figcaption>3- Tier Architecture</figcaption></figure><h3>Step 1: Setting up the Project Structure</h3><p>We will adhere to Terraform’s best practices and use modules in our configuration. The benefits of this is that modules are reusable and they can be versioned. To get started:</p><ul><li>Create a folder in your preferred file location and give it a unique name like 3-Tier arch-with-Terraform</li><li>Open the folder in VS code</li><li>Within the folder create another folder called modules</li><li>Within the modules folder, create the following folders: vpc, ec2, asg, database, elb</li><li>Within each module that you have created, create the following files: main.tf, variables.tf, outputs.tf. These files will contain the configuration code for each module as we will see in a short while.</li><li>In the main folder3-Tier arch-with-Terraform, create the following files: main.tf, variables.tf, outputs.tf, providers.tf, backend.tf. The folder 3-Tier arch-with-Terraform is the root module that we’ll use to ‘call’ and ‘put together’ our architecture as we’ll see below.</li></ul><p>Your file structure should look like this:</p><pre>   3-Tier-arch-with-Terraform/<br>   ├── modules/<br>   │   ├── vpc/<br>   │   ├── ec2/<br>   │   ├── asg/<br>   │   ├── database/<br>   │   ├── elb/<br>   ├── main.tf<br>   ├── variables.tf<br>   ├── outputs.tf<br>   ├── providers.tf<br>   ├── backend.tf</pre><p>With that established, let’s get busy!!</p><h3>Step 2: Create the Network Architecture</h3><p>The network is the foundation on which all other resources are built. The resources we will create here are:</p><p>The network architecture will be created in modules\vpc</p><ul><li>A custom VPC of CIDR block 10.0.0.0/16.</li><li>Public &amp; Private subnets (2 for each tier)</li><li>Public &amp; Private route tables (1 each)</li><li>Elastic IP address</li><li>A NAT Gateway for outbound traffic from the private instances</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*MOHcuOb5hRVEHYDiglxIgw.png" /><figcaption>Network architecture</figcaption></figure><p>To do so, navigate to modules\vpc\main.tf file.</p><p>The code:</p><p><strong>a. Custom VPC</strong></p><pre># Create custom vpc<br>resource &quot;aws_vpc&quot; &quot;main-vpc&quot; {<br>  cidr_block = var.vpc_cidr<br>  instance_tenancy = &quot;default&quot;<br>  enable_dns_hostnames = true<br>  enable_dns_support = true<br>  tags = {<br>    name = var.vpc_name<br>  }<br>}</pre><p><strong>b. Public &amp; Private subnets</strong></p><pre>#---------------------SUBNETS FOR THE 3 TIERS-------------------<br># Create tier 1 (web tier) subnets - Public subnets<br>resource &quot;aws_subnet&quot; &quot;publicsubnet1&quot; {<br>  vpc_id = aws_vpc.main-vpc.id<br>  cidr_block = var.websubnet1_cidr<br>  availability_zone = var.subnet1_az<br>  map_public_ip_on_launch = true<br><br>  tags = {<br>    Name = &quot;${var.vpc_name}-web-subnet1&quot;<br>  }<br>}<br><br>resource &quot;aws_subnet&quot; &quot;publicsubnet2&quot; {<br>  vpc_id = aws_vpc.main-vpc.id<br>  cidr_block = var.websubnet2_cidr<br>  availability_zone = var.subnet2_az<br>  map_public_ip_on_launch = true<br><br>  tags = {<br>    Name = &quot;${var.vpc_name}-web-subnet2&quot;<br>  }<br>}<br><br># Create tier 2 (app tier) subnets - Private subnets<br>resource &quot;aws_subnet&quot; &quot;app-privatesubnet1&quot; {<br>  vpc_id = aws_vpc.main-vpc.id<br>  cidr_block = var.appsubnet1_cidr<br>  availability_zone = var.subnet1_az<br>  map_public_ip_on_launch = false<br>  <br>  tags = {<br>    Name = &quot;${var.vpc_name}-app-subnet1&quot;<br>  }<br>}<br><br>resource &quot;aws_subnet&quot; &quot;app-privatesubnet2&quot; {<br>  vpc_id = aws_vpc.main-vpc.id<br>  cidr_block = var.appsubnet2_cidr<br>  availability_zone = var.subnet2_az<br>  map_public_ip_on_launch = false<br>  <br>  tags = {<br>    Name = &quot;${var.vpc_name}-app-subnet2&quot;<br>  }<br>}<br><br># Create tier 3 (db tier) subnets - Private subnets<br>resource &quot;aws_subnet&quot; &quot;db-privatesubnet1&quot; {<br>  vpc_id = aws_vpc.main-vpc.id<br>  cidr_block = var.dbsubnet1_cidr<br>  availability_zone = var.subnet1_az<br>  map_public_ip_on_launch = false<br>  <br>  tags = {<br>    Name = &quot;${var.vpc_name}-db-subnet1&quot;<br>  }<br>}<br><br>resource &quot;aws_subnet&quot; &quot;db-privatesubnet2&quot; {<br>  vpc_id = aws_vpc.main-vpc.id<br>  cidr_block = var.dbsubnet2_cidr<br>  availability_zone = var.subnet2_az<br>  map_public_ip_on_launch = false<br>  <br>  tags = {<br>    Name = &quot;${var.vpc_name}-db-subnet2&quot;<br>  }<br>}</pre><p><strong>c. Elastic IP Address, NAT Gateway, and Internet Gateway</strong></p><p>The code:</p><pre># Elastic IP for NAT Gateway<br>resource &quot;aws_eip&quot; &quot;nat&quot; {<br>  tags = {<br>    Name = &quot;${var.vpc_name}-nat-eip&quot;<br>  }<br>}<br><br># NAT Gateway for outbound traffic from private subnets<br>resource &quot;aws_nat_gateway&quot; &quot;nat&quot; {<br>  allocation_id = aws_eip.nat.id<br>  subnet_id = aws_subnet.publicsubnet1.id<br><br>  tags = {<br>    Name = &quot;${var.vpc_name}-nat-gateway&quot;<br>  }<br>}<br><br># Internet gateway for public subnet<br>resource &quot;aws_internet_gateway&quot; &quot;igw&quot; {<br>  vpc_id = aws_vpc.main-vpc.id<br>  tags = {<br>    Name = &quot;${var.vpc_name}-igw&quot;<br>  }<br>}</pre><p><strong>d. Public and Private Route tables</strong></p><p>The code:</p><pre>#--------------------PRIVATE ROUTE TABLE-----------------------<br># Private route table<br>resource &quot;aws_route_table&quot; &quot;private-rt&quot; {<br>  vpc_id = aws_vpc.main-vpc.id<br><br>  tags = {<br>    Name = &quot;${var.vpc_name}-private-rt&quot;<br>  }<br>}<br><br># Private route table routes<br>resource &quot;aws_route&quot; &quot;private-route&quot; {<br>  route_table_id = aws_route_table.private-rt.id<br>  destination_cidr_block = &quot;0.0.0.0/0&quot;<br>  nat_gateway_id = aws_nat_gateway.nat.id<br>}<br><br># Associate private subnet 1 (app tier) with private route table<br>resource &quot;aws_route_table_association&quot; &quot;app-privatesubnet1-association&quot; {<br>  subnet_id = aws_subnet.app-privatesubnet1.id<br>  route_table_id = aws_route_table.private-rt.id<br>}<br><br># Associate private subnet 2 (app tier) with private route table<br>resource &quot;aws_route_table_association&quot; &quot;app-privatesubnet2-association&quot; {<br>  subnet_id = aws_subnet.app-privatesubnet2.id<br>  route_table_id = aws_route_table.private-rt.id<br>}<br><br># Associate private subnet 1 (db tier) with private route table<br>resource &quot;aws_route_table_association&quot; &quot;db-privatesubnet1-association&quot; {<br>  subnet_id = aws_subnet.db-privatesubnet1.id<br>  route_table_id = aws_route_table.private-rt.id<br>}<br><br># Associate private subnet 2 (db tier) with private route table<br>resource &quot;aws_route_table_association&quot; &quot;db-privatesubnet2-association&quot; {<br>  subnet_id = aws_subnet.db-privatesubnet2.id<br>  route_table_id = aws_route_table.private-rt.id<br>}<br><br>#--------------------------PUBLIC ROUTE TABLE-----------------------------<br># Public route table<br>resource &quot;aws_route_table&quot; &quot;main-rt&quot; {<br>  vpc_id = aws_vpc.main-vpc.id<br>  <br>  route {<br>    cidr_block = &quot;0.0.0.0/0&quot;<br>    gateway_id = aws_internet_gateway.igw.id<br>  }<br>  <br>  tags = {<br>    Name = &quot;${var.vpc_name}-main-rt&quot;<br>  }<br>}<br><br># Associate Web tier public subnet 1 with main route table<br>resource &quot;aws_route_table_association&quot; &quot;publicsubnet1-association&quot; {<br>  subnet_id = aws_subnet.publicsubnet1.id<br>  route_table_id = aws_route_table.main-rt.id<br>}<br><br># Associate Web tier public subnet 2 with main route table<br>resource &quot;aws_route_table_association&quot; &quot;publicsubnet2-association&quot; {<br>  subnet_id = aws_subnet.publicsubnet2.id<br>  route_table_id = aws_route_table.main-rt.id<br>}</pre><p><strong>e. Security groups</strong></p><p>The code:</p><pre><br># Security group for app tier<br>resource &quot;aws_security_group&quot; &quot;app-tier-sg&quot; {<br>  name = &quot;${var.vpc_name}-app-tier-sg&quot;<br>  vpc_id = aws_vpc.main-vpc.id<br>  tags = {<br>    Name = &quot;${var.vpc_name}-app-tier-sg&quot;<br>  }<br><br>  ingress {<br>    from_port = 0<br>    to_port = 65535<br>    protocol = &quot;tcp&quot;<br>    cidr_blocks = [&quot;0.0.0.0/0&quot;]<br>  }<br>  egress {<br>    from_port = 0<br>    to_port = 0<br>    protocol = &quot;-1&quot;<br>    cidr_blocks = [&quot;0.0.0.0/0&quot;]<br>  }<br>}<br><br># Security group for web tier<br>resource &quot;aws_security_group&quot; &quot;web-subnet1-sg&quot; {<br>  name = &quot;${var.vpc_name}-web-tier-sg&quot;<br>  vpc_id = aws_vpc.main-vpc.id<br>  tags = {<br>    Name = &quot;${var.vpc_name}-web-tier-sg&quot;<br>  }<br>}<br><br>resource &quot;aws_security_group&quot; &quot;web-subnet2-sg&quot; {<br>  name = &quot;${var.vpc_name}-bastion-sg&quot;<br>  vpc_id = aws_vpc.main-vpc.id<br>  tags = {<br>    Name = &quot;${var.vpc_name}-bastion-sg&quot;<br>  }<br>}<br><br># Security group for db tier<br>resource &quot;aws_security_group&quot; &quot;db-sg&quot; {<br>  vpc_id = aws_vpc.main-vpc.id<br>egress {<br>    from_port = 0<br>    to_port = 0<br>    protocol = &quot;-1&quot;<br>    cidr_blocks = [&quot;0.0.0.0/0&quot;]<br>  }<br>   ingress {<br>    from_port = 3306<br>    to_port = 3306<br>    protocol = &quot;tcp&quot;<br>    security_groups = [aws_security_group.app-tier-sg.id]<br>  }<br><br>  tags = {<br>    Name = &quot;${var.vpc_name}-db-sg&quot;<br>  }<br>}</pre><p>Navigate to the modues\vpc\variables.tf file and define the variables we have declared above.</p><p>The code:</p><pre>variable &quot;vpc_name&quot; {<br>  description = &quot;Name of the Custom VPC&quot;<br>  type = string<br>}<br>variable &quot;vpc_cidr&quot; {<br>  description = &quot;CIDR Block for Custom VPC&quot;<br>  type = string<br>}<br><br>variable &quot;websubnet1_cidr&quot; {<br>  description = &quot;CIDR block for the web tier subnet1&quot;<br>  type = string<br>}<br><br>variable &quot;websubnet2_cidr&quot; {<br>  description = &quot;CIDR block for the web tier subnet2&quot;<br>  type = string<br>}<br><br>variable &quot;appsubnet1_cidr&quot; {<br>  description = &quot;CIDR block for the app tier subnet1&quot;<br>  type = string<br>}<br><br>variable &quot;appsubnet2_cidr&quot; {<br>  description = &quot;CIDR block for the app tier subnet2&quot;<br>  type = string<br>}<br><br>variable &quot;dbsubnet1_cidr&quot; {<br>  description = &quot;CIDR block for the db tier subnet1&quot;<br>  type = string<br>}<br>variable &quot;dbsubnet2_cidr&quot; {<br>  description = &quot;CIDR block for the db tier subnet2&quot;<br>  type = string<br>}<br>variable &quot;subnet1_az&quot; {<br>  description = &quot;Availability zone for subnet 1&quot;<br>  type = string<br>}<br>variable &quot;subnet2_az&quot; {<br>  description = &quot;Availability zone for subnet 2&quot;<br>  type = string<br>}</pre><p>Finally, navigate to the modules\vpc\outputs.tf file and define the outputs we’d want to see</p><p>The code:</p><pre>output &quot;websubnet1&quot; {<br>  value = aws_subnet.publicsubnet1.id<br>}<br><br>output &quot;websubnet2&quot; {<br>  value = aws_subnet.publicsubnet2.id<br>}<br><br>output &quot;db-sg&quot; {<br>  value = aws_security_group.db-sg.id<br>}<br><br>output &quot;db-privatesubnet1&quot; {<br>  value = aws_subnet.db-privatesubnet1.id<br>}<br><br>output &quot;db-privatesubnet2&quot; {<br>  value = aws_subnet.db-privatesubnet2.id<br>}<br><br>output &quot;websubnet1-sg&quot; {<br>  value = aws_security_group.web-subnet1-sg.id<br>}<br><br>output &quot;websubnet2-sg&quot; {<br>  value = aws_security_group.web-subnet2-sg.id<br>}<br><br>output &quot;vpc_id&quot; {<br>  value = aws_vpc.main-vpc.id<br>}<br><br>output &quot;app-tier-sg&quot; {<br>  value = aws_security_group.app-tier-sg.id<br>}</pre><h3>Step 3: Create the Web tier resources</h3><p>As mentioned before, the web tier is the environment where the application will be hosted and delivered for users to interact with.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z-ETSNQgZU5qZAXJN7g54Q.png" /><figcaption>Web-Tier Architecture</figcaption></figure><p>The resources we will build are:</p><ul><li>An auto-scaling group (ASG) that will dynamically provision EC2 instances depending on traffic. This will be created in modules\asg</li><li>A launch template to define the kind of EC2 instances that will be provisioned. This will be created in modules\asg</li><li>An application load balancer that will route incoming traffic. This will be created in modules\elb</li></ul><p>We use EC2 instances instead of S3 buckets because the ‘NoKnots’ application website is dynamic and not static.</p><p><strong>a. Create an autoscaling group and Launch Template</strong></p><p>An ASG will dynamically provision EC2 instances as needed across multiple availability zones in our public subnets.</p><p>Navigate to modules\asg\main.tf file</p><p>The code:</p><pre># A launch template for the EC2 instances - Web tier<br>resource &quot;aws_launch_template&quot; &quot;asg-launch-template&quot; {<br>  name = &quot;Launch-template&quot; <br>  image_id = var.ami_id<br>  instance_type = var.instance_type<br>  key_name = var.key_name<br>    network_interfaces {<br>      subnet_id = var.subnet_ids[0]<br>      associate_public_ip_address = true<br><br>    }<br><br>            tags = {<br>              Name = &quot;Launch-template-instance&quot;<br>            }<br><br>    <br>    user_data = &quot;${base64encode(data.template_file.test.rendered)}&quot;<br><br>    lifecycle {<br>      create_before_destroy = true<br>    }<br>}<br><br># An EC2 Auto Scaling Group for the Web tier<br>resource &quot;aws_autoscaling_group&quot; &quot;asg&quot; {<br>    name = &quot;3-tier-asg&quot;<br>    min_size = var.min_size <br>    max_size = var.max_size <br>    desired_capacity = var.desired_capacity<br>    health_check_grace_period = 300<br>    health_check_type = &quot;EC2&quot; <br>    vpc_zone_identifier = var.subnet_ids <br><br>    launch_template {<br>      id = aws_launch_template.asg-launch-template.id<br>      version = aws_launch_template.asg-launch-template.latest_version<br>    }<br><br>    target_group_arns = [ var.alb_target_group_arn ]<br><br>    lifecycle {<br>      create_before_destroy = true<br>    }<br>}<br><br>data &quot;template_file&quot; &quot;test&quot; {<br>  template = &lt;&lt;EOF<br>#!/bin/bash<br>sudo yum install wget unzip httpd -y<br>sudo mkdir -p /tmp/webfiles<br>cd /tmp/webfiles<br>sudo wget https://www.tooplate.com/zip-templates/2098_health.zip<br>sudo unzip 2098_health<br>sudo rm -rf 2098_health.zip<br>cd 2098_health<br>sudo cp -r * /var/www/html/<br>sudo systemctl start httpd<br>sudo systemctl enable httpd<br>sudo rm -rf /tmp/webfiles<br>EOF<br>}</pre><p>We’ll need to define the modules we have declared above. Navigate to modules\asg\variables.tf</p><p>The code:</p><pre>variable &quot;ami_id&quot; {<br>  description = &quot;The ami_id of the desired instance(s)&quot;<br>}<br><br>variable &quot;instance_type&quot; {<br>  description = &quot;The desired type of instance&quot;<br>}<br><br>variable &quot;key_name&quot; {<br>  description = &quot;The name of your SSH key to connect to the instances&quot;<br>}<br><br>variable &quot;subnet_id&quot; {<br>  type = string<br>}<br><br>variable &quot;min_size&quot; {<br>  description = &quot;The lowest number of instances that should run at all times&quot;<br>}<br><br>variable &quot;max_size&quot; {<br>  description = &quot;The maximum number of instances during spikes&quot;<br>}<br><br>variable &quot;desired_capacity&quot; {<br>  description = &quot;The median number of instances&quot;<br>}<br><br>variable &quot;subnet_ids&quot; {<br>  type = list(string)<br>}<br><br>variable &quot;alb_target_group_arn&quot; {<br>  <br>}</pre><p>Finally, define the outputs that will be used by other modules. Navigate to modules\asg\outputs.tf.</p><p>The code:</p><pre>output &quot;autoscaling_group_name&quot; {<br>  description = &quot;The name of the created auto scaling group&quot;<br>  value = aws_autoscaling_group.asg.name<br>}<br><br>output &quot;aws_launch_name&quot; {<br>  description = &quot;The name of the created Launch configuration&quot;<br>  value = aws_launch_template.asg-launch-template.name<br>}</pre><p><strong>b. Create an application Load balancer</strong></p><p>The Application load balancer (ALB) will distribute incoming HTTP traffic to proper targets (our EC2 instances in the web tier). The ALB will ‘listen’ over HTTP on port 80 and a target group that routes to the EC2 instances.</p><p>Navigate to modules\alb\main.tf to define the resources.</p><p>The code:</p><pre># Load balancer<br>resource &quot;aws_lb&quot; &quot;alb&quot; {<br>  name = &quot;web-load-balancer&quot;<br>  internal = false<br>  load_balancer_type = &quot;application&quot;<br>  subnets = var.subnet_ids<br>  security_groups = [ var.alb_security_group ]<br>  enable_deletion_protection = false<br>}<br><br># Target group<br>resource &quot;aws_lb_target_group&quot; &quot;web_target_group&quot; {<br>  name = &quot;web-target-group&quot;<br>  port = 80<br>  protocol = &quot;HTTP&quot;<br>  vpc_id = var.vpc_id<br>  target_type = &quot;instance&quot;<br><br>  health_check {<br>    path = &quot;/&quot;<br>    interval = 30<br>    timeout = 10<br>    healthy_threshold = 2<br>    unhealthy_threshold = 2<br>    matcher = &quot;200&quot;<br>    protocol = &quot;HTTP&quot;<br>  }<br>}<br><br>#Listener for LoadBalancer<br>resource &quot;aws_lb_listener&quot; &quot;web_listener&quot; {<br>  load_balancer_arn = aws_lb.alb.arn<br>  port = &quot;80&quot;<br>  protocol = &quot;HTTP&quot;<br><br>  default_action {<br>    type = &quot;forward&quot;<br>    target_group_arn = aws_lb_target_group.web_target_group.arn<br>  }<br>}</pre><p>Define the variables you have declared above in modules\alb\variables.tf</p><p>The code:</p><pre>variable &quot;vpc_id&quot; {<br>  description = &quot;The id of the VPC&quot;<br>}<br><br>variable &quot;elb_security_group&quot; {<br>  description = &quot;The security groups&quot;<br>  type = list()<br>}<br><br>variable &quot;subnet_ids&quot; {<br>  description = &quot;List of subnet IDs where the ALB will be deployed&quot;<br>}</pre><p>Define the outputs as well in modules\alb\outputs.tf</p><p>The code:</p><pre>output &quot;eld_dns_name&quot; {<br>  value = aws_lb.elb.dns_name<br>}<br><br>output &quot;target_group_arn&quot; {<br>  value = aws_lb_target_group.web_target_group.arn<br>  description = &quot;ARN of the target group associated with the ELB&quot;<br>}</pre><h3>Step 4: Create the App Tier Resources</h3><p>This tier handles tasks such as processing requests and data storage. To ensure reliability and scalability, we will implement a similar infrastructure as the web tier.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o5oLgUuhhqkLbWWmtIdkvQ.png" /><figcaption>App-Tier Architecture</figcaption></figure><p>Here’s what we will build:</p><ul><li>An auto-scaling group that will dynamically adjust the number of instances based on traffic and load</li><li>A launch template that specifies the type of EC2 instances that will be provisioned.</li><li>An application load balancer that will route traffic from the web tier to the app tier. The ALB will also receive incoming requests and distribute them to the appropriate instances</li></ul><p>a. Create an Auto Scaling group and Launch Template</p><p>Navigate to modules\asg\main.tf and include the block of code below.</p><p>The code:</p><pre># A launch template for the EC2 instances - App-tier<br>resource &quot;aws_launch_template&quot; &quot;app-tier-asg-launch-template&quot; {<br>  name = &quot;Launch-template-AppTier&quot; <br>  image_id = var.ami_id<br>  instance_type = var.instance_type<br>  key_name = var.key_name<br>    network_interfaces {<br>      subnet_id = var.subnet_ids[0]<br>      associate_public_ip_address = true<br><br>    }<br><br>            tags = {<br>              Name = &quot;Launch-template-instance&quot;<br>            }<br><br>    <br>    user_data = &quot;${base64encode(data.template_file.app-test.rendered)}&quot;<br><br>    lifecycle {<br>      create_before_destroy = true<br>    }<br>}<br><br># An EC2 Auto Scaling Group for the Apptier<br>resource &quot;aws_autoscaling_group&quot; &quot;app-tier-asg&quot; {<br>    name = &quot;3-tier-asg-app&quot;<br>    min_size = var.min_size <br>    max_size = var.max_size <br>    desired_capacity = var.desired_capacity<br>    health_check_grace_period = 300<br>    health_check_type = &quot;EC2&quot; <br>    vpc_zone_identifier = var.app-tier-subnet-ids <br><br>    launch_template {<br>      id = aws_launch_template.app-tier-asg-launch-template.id<br>      version = aws_launch_template.app-tier-asg-launch-template.latest_version<br>    }<br><br>    target_group_arns = [ var.app_tier_alb_target_group_arn ]<br><br>    lifecycle {<br>      create_before_destroy = true<br>    }<br>}<br><br>data &quot;template_file&quot; &quot;app-test&quot; {<br>  template = &lt;&lt;EOF<br>#!/bin/bash<br>sudo yum install mysql -y<br>EOF<br>}</pre><p>To your variables.tf file in modules\asg\variables.tf include the code shown below:</p><pre><br>variable &quot;app-tier-subnet-ids&quot; {<br>  type = list(string)<br>}<br></pre><h3>Step 5: Create the Database Tier Resources</h3><p>This is the tier where the application will store all the sensitive and/or important data, such as user login info, transactions, application content, and session data. We will use a Relational Database Service (RDS) with MySQL engine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vkXf06d7GyAnyuewLe51Zg.png" /><figcaption>Database Tier Architecture</figcaption></figure><p>Here’s what we will build:</p><ul><li>A DB subnet group to ensure the database is created in the proper subnets.</li><li>An RDS database with MySQL engine.</li><li>A database security group that allows inbound and outbound traffic to and from the app servers.</li></ul><p>Navigate to modules\database\main.tf and include the block of code below.</p><p>The code:</p><pre><br># Primary RDS Instance<br>resource &quot;aws_db_instance&quot; &quot;primary-RDS&quot; {<br>  allocated_storage = var.db-allocated-storage<br>  engine = var.db-engine<br>  engine_version = var.db-engine-version<br>  instance_class = var.db-instance-class<br>  db_name = var.db-name<br>  username = var.db-username<br>  password = var.db-password<br>  publicly_accessible = false<br>  multi_az = true<br>  storage_type = &quot;gp2&quot;<br>  vpc_security_group_ids = var.db-security-group-id <br>  db_subnet_group_name = aws_db_subnet_group.db-main-sg.id<br>  skip_final_snapshot = true<br>  backup_retention_period = 0<br>  tags = {<br>    Name = &quot;${var.db-name}-primary&quot;<br>  }<br>}<br><br># Create the subnet group<br>resource &quot;aws_db_subnet_group&quot; &quot;db-subnet-group&quot; {<br>  name = &quot;${var.db-name}-subnet-group&quot;<br>  subnet_ids = var.db-subnet-group-ids<br>  tags = {<br>    Name = &quot;${var.db-name}-subnet-group&quot;<br>  }<br>}<br><br></pre><p>Define the variables declared above in modules\database\variables.tf as shown below:</p><pre>variable &quot;db-engine&quot; {<br>  description = &quot;The database engine to use&quot;<br>  type = string<br>}<br><br>variable &quot;db-engine-version&quot; {<br>  description = &quot;The version of databse engine&quot;<br>  type = string<br>}<br><br>variable &quot;db-instance-class&quot; {<br>  description = &quot;The instance type of the RDS instance&quot;<br>  type = string<br>}<br><br>variable &quot;db-allocated-storage&quot; {<br>  description = &quot;The allocated storage in gigabytes&quot;<br>  type = number<br>  default = 20<br>}<br><br>variable &quot;db-name&quot; {<br>  description = &quot;The name of the database&quot;<br>  type = string<br>}<br><br>variable &quot;db-username&quot; {<br>  description = &quot;The master username for the database&quot;<br>  type = string<br>}<br><br>variable &quot;db-password&quot; {<br>  description = &quot;The master password of the database&quot;<br>  type = string<br>  sensitive = true<br>}<br><br>variable &quot;db-security-group-id&quot; {<br>  description = &quot;The security group ID for the RDS instance&quot;<br>  type = list(string)<br>}<br><br>variable &quot;skip-final-snapshot&quot; {<br>  description = &quot;Determines whether to skip the final snapshot before deletion&quot;<br>  type = bool<br>  default = true<br>}<br><br>variable &quot;db-subnet-group-ids&quot; {<br>  description = &quot;The security group ID for the RDS instance&quot;<br>  type = list(string)<br>}<br><br>variable &quot;db-subnet-group-name&quot; {<br>  description = &quot;The name allocated to the subnet group&quot;<br>  type = string<br>}<br><br><br><br>variable &quot;db-backup-retention&quot; {<br>  description = &quot;The number of days to retain backups for&quot;<br>  default = 7<br>  type = number<br>}</pre><p>Define the following variables in modules\database\outputs.tf file:</p><pre>output &quot;primary-db-endpoint&quot; {<br>  description = &quot;The endpoint of the primary DB instance&quot;<br>  value       = aws_db_instance.primary-RDS.endpoint<br>}<br><br>output &quot;primary-db-arn&quot; {<br>  description = &quot;The ARN of the primary database&quot;<br>  value       = aws_db_instance.primary-RDS.arn<br>}</pre><h3>Step 6: Putting it all together</h3><p>Now that we have created all the resources in modular format, it is now time to finalize on our architecture by putting all the pieces together.</p><p>To do so, navigate to the main folder 3-Tier arch-with-Terraform. We created these files main.tf, variables.tf, outputs.tf, providers.tf, and backend.tf.</p><p><strong>a. Define the provider</strong></p><p>Navigate to the providers.tf file.</p><p>The code:</p><pre>terraform {<br>  required_providers {<br>    aws = {<br>      source  = &quot;hashicorp/aws&quot;<br>      version = &quot;~&gt; 4.0&quot;<br>    }<br>  }<br>}<br><br><br>provider &quot;aws&quot; {<br>  region  = &quot;us-east-1&quot;<br>  profile = &quot;Jully_Admin_access&quot;<br>}</pre><p><strong>b. Define the main.tf file</strong></p><p>Navigate to the main.tf file. This is where we put the architecture all together.</p><pre><br>module &quot;vpc&quot; {<br>  source          = &quot;./modules/vpc&quot;<br>  vpc_cidr        = &quot;10.0.0.0/16&quot;<br>  vpc_name        = &quot;my-custom-vpc&quot;<br>  websubnet1_cidr = &quot;10.0.1.0/24&quot;<br>  websubnet2_cidr = &quot;10.0.2.0/24&quot;<br>  appsubnet1_cidr = &quot;10.0.3.0/24&quot;<br>  appsubnet2_cidr = &quot;10.0.4.0/24&quot;<br>  dbsubnet1_cidr  = &quot;10.0.5.0/24&quot;<br>  dbsubnet2_cidr  = &quot;10.0.6.0/24&quot;<br>  subnet1_az      = &quot;us-east-1a&quot;<br>  subnet2_az      = &quot;us-east-1b&quot;<br>}<br><br>module &quot;elb&quot; {<br>  source             = &quot;./modules/elb&quot;<br>  subnet_ids         = [module.vpc.websubnet1, module.vpc.websubnet2]<br>  vpc_id             = module.vpc.vpc_id<br>  elb_security_group = [module.vpc.websubnet1-sg, module.vpc.websubnet2-sg]<br>}<br><br>module &quot;asg&quot; {<br>  source               = &quot;./modules/asg&quot;<br>  ami_id               = data.aws_ami.instance-ami.id<br>  instance_type        = var.instance_type<br>  subnet_ids           = [module.vpc.websubnet1, module.vpc.websubnet2]<br>  app-tier-subnet-ids  = [module.vpc.privatesubnet1, module.vpc.privatesubnet2]<br>  key_name             = var.key_name<br>  desired_capacity     = 2<br>  max_size             = 3<br>  min_size             = 1<br>  alb_target_group_arn = module.elb.target_group_arn<br>}<br><br>module &quot;database&quot; {<br>  source               = &quot;./modules/database&quot;<br>  db-engine            = &quot;mysql&quot;<br>  db-engine-version    = &quot;8.0&quot;<br>  db-instance-class    = &quot;db.t3.micro&quot;<br>  db-allocated-storage = 20<br>  db-name              = &quot;mydb&quot;<br>  db-username          = var.db_username<br>  db-password          = var.db_password<br>  db-subnet-group-ids  = [module.vpc.db-privatesubnet1, module.vpc.db-privatesubnet2]<br>  db-security-group-id = [module.vpc.db-sg]<br>  db-subnet-group-name = [module.vpc.db-privatesubnet1, module.vpc.db-privatesubnet2]<br>}</pre><p><strong>Pro-tip</strong>: The arguments defined under each module are from the variables.tf file and output.tf file.</p><p><strong>c. Variables</strong></p><pre>variable &quot;instance_type&quot; {<br>  description = &quot;The type of instance&quot;<br>  default     = &quot;t2.micro&quot;<br>}<br><br>variable &quot;key_name&quot; {<br>  description = &quot;The name of the key to user for ssh access&quot;<br>}<br><br>variable &quot;instance_name&quot; {<br>  description = &quot;Unique name for your instance&quot;<br>  default     = &quot;Terraform-Instance&quot;<br>}<br><br>variable &quot;db_username&quot; {<br><br>}<br><br>variable &quot;db_password&quot; {<br><br>}</pre><p><strong>d. terraform.tfvars</strong></p><p>In the terraform.tfvars file, input the terraform variables</p><pre>instance_type = &quot;t2.micro&quot;<br>key_name      = &quot;Project2&quot;<br>db_username   = &quot;Admin&quot;<br>db_password   = &quot;He5n4rypha7r2m6a5cy51ED&quot;</pre><h3>Step 7: Let’s deploy</h3><p>It is now time to deploy the resources in AWS. To do so, run the 3 terraform commands.</p><pre>terraform init<br>terraform plan<br>terraform apply</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iifBoqDqRPIjlWAoW4uRgQ.png" /><figcaption>viola!</figcaption></figure><h3>Moment of Truth!</h3><p>Go to yourAWS console and verify that the resources have been deployed.</p><ol><li>VPC and its resources</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hsQut6ZFVad6D9WCOsbQ_g.png" /><figcaption>VPC, Subnets, Route-tables</figcaption></figure><p>2. Deployed instances</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4QmX8EIslpbQdGGAbtgB8g.png" /><figcaption>instances</figcaption></figure><p>3. RDS MySQL instance</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8CWZYLbul1LZtEYwbSiQkg.png" /><figcaption>RDS MySQL instance</figcaption></figure><h3>Conclusion</h3><p>Congratulations! You have successfully deployed a 3-tier architecture in AWS using Terraform. Thank you for being here!</p><blockquote>Remember to terminate ALL unused resources to avoid unnecessary cloud costs. When you are done, run the command terraform destroy</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/539/1*VWUkOMaEc6NOFUBbWb9R5g.gif" /><figcaption>High Five</figcaption></figure><p>See you in the next one. Don’t forget to leave a comment and a 👏🏽. <em>You can connect with me on</em> ✍🏽<a href="https://www.linkedin.com/in/jully-achenchi"><strong>Linkedin</strong></a> | 🖊️<a href="https://medium.com/@jullyachenchi8"><strong>Medium</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ac25fe3910b" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/how-to-deploy-a-highly-available-3-tier-architecture-in-aws-using-terraform-8ac25fe3910b">Step-by-Step Guide: Deploy a Scalable 3-Tier Architecture on AWS with Terraform Modules.</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use multiple AWS providers in Terraform.]]></title>
            <link>https://medium.com/@jullyachenchi8/how-to-use-multiple-aws-providers-in-terraform-f982a2f89160?source=rss-7a6e9f200238------2</link>
            <guid isPermaLink="false">https://medium.com/p/f982a2f89160</guid>
            <category><![CDATA[terraform-cloud]]></category>
            <category><![CDATA[infrastructure-as-code]]></category>
            <category><![CDATA[learning]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[terraform-provider]]></category>
            <dc:creator><![CDATA[Jully Achenchi]]></dc:creator>
            <pubDate>Mon, 20 Jan 2025 19:12:43 GMT</pubDate>
            <atom:updated>2025-01-20T19:12:43.442Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I0sOv8tLTX8xMbnWf9wtbQ.png" /></figure><p>There comes a time when you may need to use multiple versions of the same provider, e.g. deploying resources in different regions in AWS, deploying resources in multiple accounts, or deploying resources to multiple cloud providers.</p><p>In this article, we’ll take a deeper look at Terraform providers:</p><ul><li>Working with one provider</li><li>Working with multiple copies of the same provider</li></ul><h3>1. Working with one provider</h3><p>Let us take a closer look at a single provider to better understand how it works. But first:</p><h4>Back to basics</h4><p>Before diving into how we can use Terraform to achieve the above, let’s learn about what providers are, how to install providers, and how to use providers.</p><h4>What is a provider?</h4><p>A provider in Terraform is a plugin that enables interaction with an API.</p><p>Think of Terraform as a <strong>universal remote control</strong> for managing different things, like TVs, air conditioners, and speakers. But, here’s the trick: to <strong>control each device</strong>, you’ll need a specific <strong>adapter</strong> that connects your remote to that device. These ‘adapters’ are what we call the <strong>Terraform plugin</strong>. If you want to use/control AWS, you use the AWS plugin. Same with Google Cloud, and Azure.</p><p>Terraform communicates with plugins via <strong><em>remote procedure calls (RPCs),</em></strong> and those plugins in turn communicate with their corresponding platforms via the network (via HTTP calls).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fGf_L94UOH4VK36D9J2H5Q.png" /><figcaption>How Terraform works</figcaption></figure><p>Each provider claims a specific prefix:</p><ul><li><strong>aws_ </strong>prefix for AWS</li><li><strong>azurerm_</strong> prefix for Microsoft Azure</li><li><strong>googe_</strong> prefix for GCP</li></ul><h4>How to install an AWS provider</h4><p>To install the AWS provider, add a provider block to your code and runterraform init, as below:</p><pre>provider &quot;aws&quot; {<br>  region = &quot;us-east-1&quot;<br>}</pre><p>What if you want fine-grained control such as the version of the provider you want, or where to download the provider from? To do this, use a required_providers block as below:</p><pre>terraform {<br>  required_providers {<br>    aws = {<br>      source  = &quot;hashicorp/aws&quot;<br>      version = &quot;~&gt; 4.16&quot;<br>    }<br>  }<br>}</pre><h3>2. Working with multiple copies of the same provider</h3><p>Working with multiple copies of the same provider can be seen in 3 different forms:</p><ul><li>Working with multiple AWS regions</li><li>Working with multiple AWS accounts</li></ul><h4>Working with multiple AWS Regions</h4><p>A region is a data center. Suppose you want to deploy your resources in two regions, say us-east-1, and us-east-2. How would you go about specifying what resources should be in which region? The solution to this is demonstrated below:</p><pre>provider &quot;aws&quot; {<br>  region = &quot;us-east-1&quot;<br>  alias = &quot;region-1&quot;<br>}<br><br>provider &quot;aws&quot; {<br>  region = &quot;us-east-2&quot;<br>  alias = &quot;region-2&quot;<br>}</pre><p>Add an alias parameter. An alias is a custom name for the provider that you can explicitly pass to individual resources, data sources, and modules to get them to use the configuration in that particular provider.</p><p>For example, to deploy 2 instances in different AWS regions, here’s how you would go about it:</p><pre>data &quot;aws_ami&quot; &quot;instance-region-1&quot; {<br>  provider = aws.region-1<br><br>  most_recent = true<br>  owners = [&quot;amazon&quot;]<br><br>  filter {<br>    name = &quot;name&quot;<br>    values = [&quot;amzn2-ami-hvm*&quot;]<br>  }<br>}<br><br>data &quot;aws_ami&quot; &quot;instance-region-2&quot; {<br>  provider = aws.region-2<br><br>  most_recent = true<br>  owners = [&quot;amazon&quot;]<br><br>  filter {<br>    name = &quot;name&quot;<br>    values = [&quot;amzn2-ami-hvm*&quot;]<br>  }<br>}<br><br>resource &quot;aws_instance&quot; &quot;region-1&quot; {<br>  provider = aws.region-1<br><br>  ami = data.aws_ami.instance-region-1.id<br>  instance_type = &quot;t2.micro&quot;<br>}<br><br>resource &quot;aws_instance&quot; &quot;region-2&quot; {<br>  provider = aws.region-2<br><br>  ami = data.aws_ami.instance-region-2.id<br>  instance_type = &quot;t2.micro&quot;<br>}</pre><h4>Code explanation</h4><p>The data block is used to ‘look up’ the ami_id instead of manually getting the ID from the management console. Ain’t that cool?</p><h3>Working with multiple AWS Accounts</h3><p>Sometimes, you may use multiple accounts for your infrastructure such as production code: e.g., you put your staging environment in a staging account, your production environment in a production account, etc.</p><p>The main reasons for using multiple accounts include:</p><ul><li><strong>Isolation</strong></li></ul><p>You may use separate accounts to isolate different environments from each other to limit the ‘blast radius’ in case something goes wrong.</p><ul><li><strong>Authentication and authorization</strong></li></ul><p>Using multiple accounts makes it easier to have fine-grained control of your resources. If everything is in one account, it’s easy to grant access to resources not meant to be accessed.</p><ul><li><strong>Auditing and reporting</strong></li></ul><p>A properly configured account structure will allow you to maintain an audit trail of all the changes happening in your environment.</p><p>To work with multiple accounts, you’ll need to add two provider blocks each with a different alias.</p><p>Let’s assume you have two AWS accounts in the same organization with the following Account IDs:</p><ul><li>Parent AWS account ID: 983456239012</li><li>Child AWS account ID: 946734560934</li></ul><p>First, create the provider block for the parent AWS account:</p><pre>provider &quot;aws&quot; {<br>  region = &quot;us-east-1&quot;<br>  alias = &quot;parent-acc&quot;<br>}</pre><p>Next, create the provider block for the child AWS account</p><pre>provider &quot;aws&quot; {<br>  region = &quot;us-east-1&quot;<br>  alias = &quot;child-acc&quot;<br>}</pre><p>To be able to authenticate to the child AWS account, you’ll need to assume an IAM role as follows:</p><pre>provider &quot;aws&quot; {<br>  region = &quot;us-east-1&quot;<br>  alias = &quot;child-acc&quot;<br><br>  assume_role {<br>    role_arn = &quot;arn:aws:iam::946734560934:role/OrganizationAccountAccessRole&quot;<br>  }<br>}<br></pre><p>Where:</p><ul><li>946734560934 is the child account ID</li><li>OrganizationAccountAccessRole is the name of the assumed role</li></ul><h3>Conclusion</h3><p>At this point, you understand how to work with multiple providers in AWS at region level and in multiple AWS accounts.</p><p><strong>Thank you for being here</strong>! See you in the next one.</p><p>Adios 👋🏽</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f982a2f89160" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Managing secrets with Terraform]]></title>
            <link>https://awstip.com/managing-secrets-with-terraform-261276a0791f?source=rss-7a6e9f200238------2</link>
            <guid isPermaLink="false">https://medium.com/p/261276a0791f</guid>
            <category><![CDATA[infrastructure-as-code]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[secrets]]></category>
            <category><![CDATA[learning]]></category>
            <dc:creator><![CDATA[Jully Achenchi]]></dc:creator>
            <pubDate>Mon, 20 Jan 2025 13:13:18 GMT</pubDate>
            <atom:updated>2025-02-06T18:28:28.094Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fNPwsdgMBFSl5Cy7b4sfPg.png" /></figure><p>Got a secret? Most importantly, how securely can you keep it?</p><p>Managing your resources and keeping them secure is a crucial part of working with Terraform. Terraform provides several mechanisms and best practices to handle secrets effectively and securely.</p><p>In this article, we will explore ways of managing secrets in Terraform and with Terraform code. We will cover:</p><ul><li>What are secrets in Terraform?</li><li>Secrets management basics</li><li>Approaches to manage secrets in Terraform</li></ul><p>Let’s get started!</p><h3>What are secrets in Terraform?</h3><p>As the word suggests, secrets are sensitive information about an organization’s infrastructure and operations. Secrets constitute database passwords, API keys, TLS certificates, SSH keys, GPG keys, access tokens, and other forms of sensitive data.</p><p>Secrets secure such information by preventing unauthorized access, data breaches, or critical security incidents</p><h3>Secret Management Basics</h3><p>The <strong>first rule</strong> of secrets management is:</p><blockquote>Do not store secrets in plain text.</blockquote><p>The <strong>second rule</strong> of secrets management is: Check the first rule!</p><p>This is what I mean:</p><pre>resource &quot;aws_db_instance&quot; &quot;db_example&quot; {<br>  engine               = &quot;mysql&quot;<br>  engine_version       = &quot;5.7&quot;<br>  instance_class       = &quot;db.t2.micro&quot;<br>  name                 = &quot;example&quot;<br>  allocated_storage    = 10<br><br>  # DO NOT DO THIS!!!<br>  username = &quot;year&quot;<br>  password = &quot;of-the-lord-2025&quot;<br>  # DO NOT DO THIS!!!<br>}<br></pre><p>Storing secrets in plain text is a BAD IDEA. Anyone who gains access to your Terraform code gains access to your resources as a superuser. How then should we handle secrets in Terraform? Next, we will look at secret management tools in Terraform and how to use each one of them.</p><h3>Approaches to Securing Secrets in Terraform</h3><p>There are a few recommended approaches to handling secrets in Terraform:</p><h4>1. Using environment variables.</h4><p>Your first exposure to secrets when working with Terraform is when you have to authenticate to a provider by providing an access key and a secret key. Rather than hardcoding secrets directly into your code (which is a TERRIBLE IDEA btw), you can reference environment variables within your Terraform configuration files.</p><p>For example, here’s how to use environment variables to authenticate to AWS. Run the below commands in the terminal with the appropriate values:</p><pre>Using Linux<br>$ export AWS_ACCESS_KEY_ID=(YOUR_ACCESS_KEY_ID)<br>$ export AWS_SECRET_ACCESS_KEY=(YOUR_SECRET_ACCESS_KEY)<br><br>Using powershell<br>set AWS_ACCESS_KEY_ID=(YOUR_ACCESS_KEY_ID)<br>set AWS_SECRET_ACCESS_KEY=(YOUR_SECRET_ACCESS_KEY)</pre><h4>2. Using external secret management systems</h4><p>Another approach to managing secrets in Terraform is to leverage external secret management systems such as HashiCorp Vault or AWS Secrets Manager</p><h4>Integrating Terraform with Vault</h4><p>HashiCorp Vault is a widely used secret management solution that allows you to store, manage, and access secrets securely. To integrate Terraform with Vault, follow these steps:</p><ul><li><strong>Install and configure the vault.</strong> Follow this official <a href="https://developer.hashicorp.com/vault/docs?ref=blog.gitguardian.com">vault documentation</a> to install and set up vault in your local machine</li><li><strong>Enable the </strong><strong>kv secrets engine in vault</strong>. Run the following command which allows you to store key-value pair:</li></ul><p>vault secrets enable -path=my-secrets kv</p><ul><li><strong>Store secrets in your vault using the following command:</strong></li></ul><p>vault kv put my-secrets/aws aws_access_key_id=(YOUR_ACCESS_KEY_ID) aws_secret_access_key=(YOUR_SECRET_ACCESS_KEY)</p><ul><li><strong>Configure the Terraform provider vault provider </strong>to access your stored secrets in the vault</li></ul><pre>provider &quot;vault&quot; {<br>  address = &quot;https://vault.example.com:8200&quot;<br>  token = &quot;&lt;your_vault_token&gt;&quot;<br>}</pre><ul><li><strong>Access secrets from the vault</strong> in your Terraform code using the data block</li></ul><pre>data &quot;vault_generic_secret&quot; &quot;aws_credentials&quot; {<br>  path = &quot;my-secrets/aws&quot;<br>}<br><br>provider &quot;aws&quot; {<br>  access_key = data.vault_generic_secret.aws_credentials.data[&quot;aws_access_key_id&quot;]<br>  secret_key = data.vault_generic_secret.aws_credentials.data[&quot;aws_secret_access_key&quot;]<br>  region     = &quot;us-east-1&quot;<br>}</pre><h4>Integrating Terraform with AWS Secrets Manager</h4><p>AWS Secrets Manager is a managed service. To integrate Terraform with AWS Secrets Manager, here’s a step-by-step guide:</p><ul><li>Store secrets in AWS Secrets Manager. Log in to your AWS account and navigate to Secrets Manager, and create a new secret such as a database username and password.</li><li>Configure the Terraform provider with the appropriate credentials</li></ul><pre>provider &quot;aws&quot; {<br>  region = &quot;us-east-1&quot;<br>}</pre><ul><li>Access secrets from the AWS Secrets Manager in Terraform using the data block:</li></ul><pre>data &quot;aws_secretsmanager_secret_version&quot; &quot;example&quot; {<br>  secret_id = &quot;my-app/db_password&quot;<br>}<br><br>resource &quot;aws_db_instance&quot; &quot;example&quot; {<br>  // ...<br>  password = data.aws_secretsmanager_secret_version.example.secret_string<br>  // ...<br>}</pre><h4>3. Using input variables</h4><p>You can define input variables in your configuration files and provide the sensitive values when running the Terraform commands. This allows you to pass secrets as command-line arguments. For example:</p><ul><li>Create an input variable in a variables.tf file</li></ul><pre>variable &quot;db_password&quot; {<br>  description = &quot;The password to the database&quot;<br>  type = string<br>  sensitive = true<br>}</pre><ul><li>Reference the variable in your resource:</li></ul><pre>resource &quot;aws_db_instance&quot; &quot;name&quot; {<br>    //...<br>    password = var.db_password<br>    //...<br>}</pre><ul><li>When running terraform apply, pass the secret value in the command line:</li></ul><pre>terraform apply -var &quot;db_password=&lt;your-password&gt;&quot;</pre><h4>4. Terraform state encryption</h4><p>Sometimes, despite your best efforts, secrets may end up in your state file as plain text. As foolproof, it is essential that you securely store your state file at all times. Two ways to do that are:</p><ul><li><strong>Store Terraform state in the backend that supports encryption</strong></li></ul><p>Utilize the s3 backend and DynamoDB to securely store your state files and enable locking. This article goes in-depth on<a href="https://medium.com/@jullyachenchi8/how-to-manage-terraform-state-a-step-by-step-guide-and-best-practices-111d2c890dbd"> how to configure S3 and DynamoDB to securely store your state files.</a></p><ul><li><strong>Strictly control who has access to your Terraform backend</strong></li></ul><p>Use AWS IAM (Identity and Access Management) policies and users for fine-grained access to your backend.</p><h3>Conclusion</h3><p>Here are the key takeaways from this article: If you remember nothing else, please remember this: <strong><em>Do not store secrets in plain text.</em></strong></p><p>Secondly, you can use either environment variables and input variables to pass your secrets, or you can store your secrets in external sources such as HashiCorp vault or AWS Secrets Manager.</p><p>Finally, despite your best efforts, secrets may end up in your state file. Securely store your state files using S3 and DynamoDB, and use IAM policies and users for fine-grained access to your S3 bucket.</p><p><strong>Thank you for being here</strong>! I hope you learned something. See you in the next one.</p><p>Adios 👋🏽</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=261276a0791f" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/managing-secrets-with-terraform-261276a0791f">Managing secrets with Terraform</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>