Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
      • Pod
    • Helm Chart
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[AWS] Filtering Subnets in Different Availability Zones for EFS Mount Targets with Terraform

Posted on December 10, 2024December 12, 2024 By nim No Comments on [AWS] Filtering Subnets in Different Availability Zones for EFS Mount Targets with Terraform

Bài toán ở đây là client sẽ đưa ra cho chúng ta 1 list subnet. Mục đích của chúng ta là chỉ lựa ra mỗi Zone chúng ta sẽ lấy ra 1 subnet để sử dụng để cài đặt hệ thông:

Contents

Toggle
    • The Solution:
    • Step-by-Step Example
  • Breakdown:

The Solution:

  1. Check the AZ of each subnet: We’ll fetch the AZ for each subnet.
  2. Only apply EFS Mount Targets to subnets in different AZs: Ensure we create EFS Mount Targets in unique AZs.

Step-by-Step Example

  1. List of Subnets: Let’s assume you provide a list of subnets like this:
variable "eks_private_subnets" {
  type = list(string)
  default = ["subnet-abc123", "subnet-def456", "subnet-ghi789"]
}

2. Fetch Subnet Information: You want to check the availability zone of each subnet. To do this, we use a data "aws_subnet" block to get the AZ of each subnet.

3. Create EFS Mount Targets in Different AZs: Now, we will apply the EFS Mount Target only in subnets that are in unique AZs.

# Fetch the AZ of each subnet
data "aws_subnet" "subnets" {
  for_each = toset(var.eks_private_subnets)
  id       = each.value
}

# Extract the AZ for each subnet
locals {
  subnet_azs = { for s in data.aws_subnet.subnets : s.id => s.availability_zone }
}

# Create EFS Mount Targets in different AZs only
resource "aws_efs_mount_target" "efs_mount_target" {
  count = length(distinct(local.subnet_azs))  # Only count unique AZs

  file_system_id  = aws_efs_file_system.efs_file_system.id
  subnet_id       = element(var.eks_private_subnets, count.index)
  security_groups = [aws_security_group.efs_allow_access.id]
}

Breakdown:

  1. data "aws_subnet": This block fetches the AZ for each subnet in var.eks_private_subnets. It returns the AZ (like us-east-1a, us-east-1b, etc.).
  2. locals { subnet_azs = ... }: We create a local map subnet_azs to store the AZ for each subnet. It looks something like this:
{
  "subnet-abc123" = "us-east-1a",
  "subnet-def456" = "us-east-1b",
  "subnet-ghi789" = "us-east-1a"
}

3. resource "aws_efs_mount_target": In this block, we create an EFS Mount Target. The count is determined by the number of unique AZs in local.subnet_azs using distinct(). So, if two subnets are in the same AZ, it will only count once.

  • If subnet-abc123 and subnet-ghi789 are in the same AZ (us-east-1a), then only 1 EFS Mount Target will be created for AZ us-east-1a and another for AZ us-east-1b.
AWS - Amazon Web Service

Post navigation

Previous Post: [AWS] Using FSx Lustre to enhance disk performance for large-scale applications.
Next Post: [Grafana] Easily install Grafana on Kubernetes using a Helm chart.

More Related Articles

[AWS] Demo “code build” with experiment easily on AWS AWS - Amazon Web Service
[EKS] Checking your EKS cluster that is working efficiently. AWS - Amazon Web Service
How to aws cli authenticate with AWS AWS - Amazon Web Service
[EKS/Pods] Why can not the pod on EKS call http://169.254.169.254/latest/api/token AWS - Amazon Web Service
[Keda] Auto-scaling is so easy when you use Keda AWS - Amazon Web Service
[EKS/IPs] Increase most many IPs as possible on each Node of your EKS. AWS - Amazon Web Service

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [WordPress] Hướng dấn gửi mail trên WordPress thông qua gmail. June 15, 2025
  • [Bitbucket] Git Clone/Pull/Push with Bitbucket through API Token. June 12, 2025
  • [Teamcity] How to transfer the value from pipeline A to pipeline B June 9, 2025
  • [Windows] Remove the process that consumes too much CPU. June 3, 2025
  • Deploying Web-Based File Managers: File Browser and KubeFileBrowser with Docker and Kubernetes June 3, 2025

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2025 NimTechnology.