惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
有赞技术团队
有赞技术团队
V
Visual Studio Blog
T
The Blog of Author Tim Ferriss
爱范儿
爱范儿
Vercel News
Vercel News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
D
DataBreaches.Net
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
大猫的无限游戏
大猫的无限游戏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
The Cloudflare Blog
宝玉的分享
宝玉的分享
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
AWS Terraform Type Constarints
Brian Mengo · 2026-05-10 · via DEV Community
Cover image for AWS Terraform Type Constarints

Brian Mengo

Today, I learned about Terraform type constraints and why they are essential for writing safe, predictable, and maintainable infrastructure code.

Terraform variables are not just about passing values. With type constraints, variables become contracts that define what kind of data is allowed.

Topics Covered

  1. Primitive types: string, number, bool
  2. Collection types: list, set, map
  3. Structural types: tuple, object
  4. Type validation and constraints
  5. Defining complex variable structures

Why Type Constraints Matter
Without type constraints, Terraform treats variables as loosely typed. This can lead to:

  • Runtime errors during terraform apply
  • Unexpected values passed to resources
  • Hard-to-debug infrastructure issues
    Using type constraints gives you:

  • Early validation at terraform plan

  • Self-documenting variables

  • Safer and predictable infrastructure code

Primitive Type
Number
Supports both integers and floating-point values.
example

variable "instance_count" {
  type    = number
  default = 1
}

Enter fullscreen mode Exit fullscreen mode

Specifying the type as number restricts the variable to numeric values, matching Terraform’s expectations for count

String
A string is used for text-based values such as names, regions, or identifiers.
Example

variable "region" {
  type    = string
  default = "us-east-1"
}

Enter fullscreen mode Exit fullscreen mode

String values must be enclosed in double quotes and can contain spaces.

Boolean
A boolean represents true or false. It is commonly used for feature toggles.
example

variable "monitoring_enabled" {
  type    = bool
  default = true
}

Enter fullscreen mode Exit fullscreen mode

Complex Types in Terraform
List
A list is an ordered collection of values of the same type

variable "availability_zones" {
  type = list(string)
  default = ["us-east-1a", "us-east-1b"]
}

Enter fullscreen mode Exit fullscreen mode

Set
A set is similar to a list, but:

  • Values must be unique
  • Order is not guaranteed

Commonly used for Security-groups;

variable "allowed_ports" {
  type = set(number)
  default = [22, 80, 443]
}

Enter fullscreen mode Exit fullscreen mode

Map
A key-value structure frequently used for tagging AWS resources.

variable "tag" {
  type = map(string)
  default = {
    Environment = "Dev"
    Name = "Dev-EC2-instance"
  }

Enter fullscreen mode Exit fullscreen mode

Structural Types(Advanced)
Object

  • Object is a collection of named attributes with different data types, defined with keys.
  • Example object variable config with three attributes:
variable "config" {
  type = object({
    region         = string
    monitoring     = bool
    instance_count = number
  })
  default = {
    region         = "us-east-1"
    monitoring     = true
    instance_count = 1
  }
}

Enter fullscreen mode Exit fullscreen mode

Tuple

  • Tuple enables grouping multiple values with different data types in a fixed sequence.
  • Example tuple variable with three elements (number, string, number):
variable "ingress_values" {
  type = tuple([number, string, number])
  default = [443, "TCP", 443]
}

Enter fullscreen mode Exit fullscreen mode

Best practice I learnt when using type contraints;

  • Use primitives for simple values.
  • Use complex types to group multiple related values, especially when types vary.
  • Understand indexing rules: lists and tuples are indexed; sets are not.
  • Use objects for structured data with named fields of different types.
  • Use maps for homogeneous key-value pairs.