---
title: How to Use AWS Terraform Defaults Function
description: Unlock the power of AWS with ease! Learn how to utilize Terraform Defaults Function for efficient cloud management. Click to master AWS now!
image: https://stratusgrid.com/hubfs/How%20to%20Use%20AWS%20Terraform%20Defaults()%20Function.webp
---

# How to Use AWS Terraform Defaults() Function

Unlock the power of AWS with ease! Learn how to utilize Terraform Defaults Function for efficient cloud management. Click to master AWS now!

[ Chris Hurst ](https://stratusgrid.com/blog/author/chris-hurst)

 Aug 3, 2022

#### Other blogs you may be interested in:

[ AWS Cost Optimization Guide: Mistakes to Avoid & How to Reduce Cloud Costs ](https://stratusgrid.com/blog/aws-cost-optimization-guide)

[ AWS Lambda Rust: How to Deploy AWS Lambda Functions Built with Rust code ](https://stratusgrid.com/blog/aws-lambda-rust-how-to-deploy-aws-lambda-functions)

[ What is Terraform and Why Is It Used? ](https://stratusgrid.com/blog/an-introduction-to-terraform-from-stratusgrid)

### Share this article

<http://www.facebook.com/share.php?u=https://stratusgrid.com/blog/using-the-defaults-function-in-terraform&utm_medium=social&utm_source=facebook> <http://www.linkedin.com/shareArticle?mini=true&url=https://stratusgrid.com/blog/using-the-defaults-function-in-terraform&utm_medium=social&utm_source=linkedin> <https://twitter.com/intent/tweet?original_referer=https://stratusgrid.com/blog/using-the-defaults-function-in-terraform&utm_medium=social&utm_source=twitter&url=https://stratusgrid.com/blog/using-the-defaults-function-in-terraform&utm_medium=social&utm_source=twitter&source=tweetbutton&text=> [mailto:?subject=Check%20out%20https://stratusgrid.com/blog/using-the-defaults-function-in-terraform&utm_medium=social&utm_source=email%20&body=Check%20out%20https://stratusgrid.com/blog/using-the-defaults-function-in-terraform&utm_medium=social&utm_source=email](mailto:?subject=Check%20out%20https://stratusgrid.com/blog/using-the-defaults-function-in-terraform&utm_medium=social&utm_source=email%20&body=Check%20out%20https://stratusgrid.com/blog/using-the-defaults-function-in-terraform&utm_medium=social&utm_source=email)

### Subscribe

### Subscribe

![How to Use AWS Terraform Defaults() Function](https://stratusgrid.com/hubfs/How%20to%20Use%20AWS%20Terraform%20Defaults()%20Function.webp)

How to Use AWS Terraform Defaults Function

1:51

Official documentation: [** defaults - Functions - Configuration Language | Terraform by HashiCorp ** ](https://www.terraform.io/language/functions/defaults)

At the time of this writing, this is currently an [experimental function](https://stratusgrid.com/blog/building-a-private-terraform-provider-registry-in-s3). For this reason, we are only allowing its use in our parent repos for things like the variables on root repos; we are not allowing the use of the defaults() function until it is released as GA instead of experimental.

## ** Example: Using defaults() to Set the Default Values of Object Attributes Inside of a Map of Objects **

This example shows how to have an input variable that accepts a map of objects but allows the objects to only require some values vs. all because there are sane defaults. You can replicate this by copying the content of the two code blocks (test.tf and test.tfvars) into files in an empty folder and [running terraform](https://stratusgrid.com/blog/quickstart-guide-to-using-terraform) apply (because no resources are being created, you won’t need any credentials or state).

** test.tfvars **

```
1ecs_service_variables = {23  test = {45    security_groups        = ["string1","string2"]6    subnets                = ["string1","string2"]7    log_group_path         = "/"89    # Task Size10    taskdef_cpu                      = 25611    taskdef_memory                   = 10241213  }1415  test2 = {16    security_groups        = ["string3","string4"]17    subnets                = ["string3","string4"]18    log_group_path         = "/"19    assign_public_ip       = true2021    # Task Size22    taskdef_cpu                      = 51223    taskdef_memory                   = 102424  }2526}
```

**test.tf **

```
1terraform {2  # Optional attributes and the defaults function are3  # both are experimental, so we must opt into the experiment.4  experiments = [module_variable_optional_attrs]5}67variable "ecs_service_variables" {8  description = "A map of the standard values that need to be fed in as variables/adjusted per environment. These should be named to match the names in the map of ecs services"9  type        = map(object(10    {1112      security_groups        = list(string)13      subnets                = list(string)14      assign_public_ip       = optional(bool)15      propagate_tags         = optional(string)16      log_group_path         = string17      enable_execute_command = optional(bool)1819      codedeploy_termination_wait_time = optional(number)20      codedeploy_auto_rollback_enabled  = optional(bool)2122      # Task Size23      taskdef_cpu                      = number24      taskdef_memory                   = number2526      # Service Scale27      desired_count      = optional(number)28      min_capacity       = optional(number)29      max_capacity       = optional(number)30      scale_in_cooldown  = optional(number)31      scale_out_cooldown = optional(number)3233    }34  ))35}3637locals {38  ecs_service_variables = defaults(var.ecs_service_variables, {39    assign_public_ip = false40    enable_execute_command = true41    propagate_tags = "TASK_DEFINITION"4243    codedeploy_termination_wait_time = 30044    codedeploy_auto_rollback_enabled = true4546    desired_count = 247    min_capacity = 248    max_capacity = 349    scale_in_cooldown = 30050    scale_out_cooldown = 60051  })52}5354output "ecs_service_variables" {55  value = local.ecs_service_variables56}
```

Notice that we are referencing the local, not the variable. If you run terraform apply -var-file ./test.tfvars, you should see this:

```
1Apply complete! Resources: 0 added, 0 changed, 0 destroyed.23Outputs:45ecs_service_variables = tomap({6  "test" = {7    "assign_public_ip" = false8    "codedeploy_auto_rollback_enabled" = true9    "codedeploy_termination_wait_time" = 30010    "desired_count" = 211    "enable_execute_command" = true12    "log_group_path" = "/"13    "max_capacity" = 314    "min_capacity" = 215    "propagate_tags" = "TASK_DEFINITION"16    "scale_in_cooldown" = 30017    "scale_out_cooldown" = 60018    "security_groups" = tolist([19      "string1",20      "string2",21    ])22    "subnets" = tolist([23      "string1",24      "string2",25    ])26    "taskdef_cpu" = 25627    "taskdef_memory" = 102428  }29  "test2" = {30    "assign_public_ip" = true31    "codedeploy_auto_rollback_enabled" = true32    "codedeploy_termination_wait_time" = 30033    "desired_count" = 234    "enable_execute_command" = true35    "log_group_path" = "/"36    "max_capacity" = 337    "min_capacity" = 238    "propagate_tags" = "TASK_DEFINITION"39    "scale_in_cooldown" = 30040    "scale_out_cooldown" = 60041    "security_groups" = tolist([42      "string3",43      "string4",44    ])45    "subnets" = tolist([46      "string3",47      "string4",48    ])49    "taskdef_cpu" = 51250    "taskdef_memory" = 102451  }52})
```

If you want to get this in a format that can be machine interpreted (to import with jsondecode() for instance), you can get that like so with terraform output -json | jq .ecs_service_variables.value which should return:

```
1{2  "test": {3    "assign_public_ip": false,4    "codedeploy_auto_rollback_enabled": true,5    "codedeploy_termination_wait_time": 300,6    "desired_count": 2,7    "enable_execute_command": true,8    "log_group_path": "/",9    "max_capacity": 3,10    "min_capacity": 2,11    "propagate_tags": "TASK_DEFINITION",12    "scale_in_cooldown": 300,13    "scale_out_cooldown": 600,14    "security_groups": [15      "string1",16      "string2"17    ],18    "subnets": [19      "string1",20      "string2"21    ],22    "taskdef_cpu": 256,23    "taskdef_memory": 102424  },25  "test2": {26    "assign_public_ip": true,27    "codedeploy_auto_rollback_enabled": true,28    "codedeploy_termination_wait_time": 300,29    "desired_count": 2,30    "enable_execute_command": true,31    "log_group_path": "/",32    "max_capacity": 3,33    "min_capacity": 2,34    "propagate_tags": "TASK_DEFINITION",35    "scale_in_cooldown": 300,36    "scale_out_cooldown": 600,37    "security_groups": [38      "string3",39      "string4"40    ],41    "subnets": [42      "string3",43      "string4"44    ],45    "taskdef_cpu": 512,46    "taskdef_memory": 102447  }48}
```

## Master Cloud Management with StratusGrid's Terraform Defaults Function Expertise

Embrace the full potential of Terraform to streamline your AWS cloud management with StratusGrid's guidance on the Defaults Function. Whether you're new to Terraform or looking to refine your skills, StratusGrid is here to illuminate the path toward more robust and simplified cloud infrastructure management.

[Contact us today](https://stratusgrid.com/book-a-consultation?hsLang=en) to explore how our expertise can elevate your Terraform projects and unlock new levels of efficiency and control in your AWS environment.

## BONUS: Find Out if Your Organization is Ready for The Cloud ⤵️

[![FinOps](https://stratusgrid.com/hs-fs/hubfs/FinOps.webp?width=1280&height=720&name=FinOps.webp)](https://stratusgrid.com/cloud-maturity-assessment-quiz)

## Similar posts

<https://stratusgrid.com/blog/aws-cost-optimization-guide>

Cost Optimization

### [AWS Cost Optimization Guide: Mistakes to Avoid & How to Reduce Cloud Costs](https://stratusgrid.com/blog/aws-cost-optimization-guide)

Learn how you can slash your AWS bill with our comprehensive guide on AWS Cost Optimization. Perfect for FinOps engineers & cloud cost managers.

 StratusGrid  Feb 19, 2024

<https://stratusgrid.com/blog/aws-lambda-rust-how-to-deploy-aws-lambda-functions>

Modernization

### [AWS Lambda Rust: How to Deploy AWS Lambda Functions Built with Rust code](https://stratusgrid.com/blog/aws-lambda-rust-how-to-deploy-aws-lambda-functions)

Master the deployment of Rust functions on AWS Lambda & learn to set up your environment, use cargo-lambda, and launch your Rust code in the cloud.

 Trevor Sullivan  Nov 9, 2023

<https://stratusgrid.com/blog/an-introduction-to-terraform-from-stratusgrid>

Modernization

### [What is Terraform and Why Is It Used?](https://stratusgrid.com/blog/an-introduction-to-terraform-from-stratusgrid)

Get the simplest explanation of Terraform on AWS. Understand its benefits and how it revolutionizes infrastructure management in cloud environments.

 Alfredo Espejel  Aug 26, 2021

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Chris Hurst",
    "url" : "https://stratusgrid.com/blog/author/chris-hurst"
  },
  "dateModified" : "2024-09-04T16:21:35.845Z",
  "datePublished" : "2022-08-03T04:00:00.000Z",
  "headline" : "How to Use AWS Terraform Defaults Function",
  "image" : [ "https://stratusgrid.com/hubfs/How%20to%20Use%20AWS%20Terraform%20Defaults()%20Function.webp" ],
  "mainEntityOfPage" : {
    "@id" : "https://stratusgrid.com/blog/using-the-defaults-function-in-terraform",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://stratusgrid.com/hubfs/Vertical%20Black.png"
    },
    "name" : "StratusGrid"
  }
}
```

```json
{
  "@context" : "https://schema.org/",
  "@type" : "Product",
  "aggregateRating" : {
    "@type" : "AggregateRating",
    "bestRating" : "5",
    "ratingCount" : "1",
    "ratingValue" : "5",
    "reviewCount" : "1",
    "worstRating" : "0"
  },
  "brand" : {
    "@type" : "Brand",
    "name" : "StratusGrid"
  },
  "description" : "StratusGrid, a premier AWS Consulting Partner, specializes in AWS cloud migration, modernization, stabilization, and cloud cost optimization services.",
  "image" : "https://stratusgrid.com/",
  "name" : "StratusGrid",
  "review" : {
    "@type" : "Review",
    "author" : {
      "@type" : "Person",
      "name" : "G2"
    },
    "datePublished" : "2024-01-21",
    "name" : "G2 User",
    "publisher" : {
      "@type" : "Organization",
      "name" : "G2"
    },
    "reviewBody" : "Great Customer Service and Extremely Knowledgable Team",
    "reviewRating" : {
      "@type" : "Rating",
      "bestRating" : "5",
      "ratingValue" : "5",
      "worstRating" : "0"
    }
  }
}
```