Error creating application autoscaling target on AWS when using Terraform - Defining `scalable_resource` for custom `aws_appautoscaling_target`

3 min read 06-10-2024
Error creating application autoscaling target on AWS when using Terraform - Defining `scalable_resource` for custom `aws_appautoscaling_target`


Troubleshoot Terraform: Error Creating Application Autoscaling Target on AWS

Problem: You're trying to create an Application Autoscaling target in AWS using Terraform, but you encounter an error related to defining the scalable_resource block within the aws_appautoscaling_target resource.

Simplified: Imagine you want to automatically adjust the number of instances running your application based on its performance. Terraform helps you set this up, but you're facing a hurdle with configuring the specific resource you want to scale.

Scenario:

You're trying to configure autoscaling for an ElasticSearch domain. Here's a simplified version of your Terraform code:

resource "aws_elasticsearch_domain" "main" {
  # ... ElasticSearch domain configuration ...
}

resource "aws_appautoscaling_target" "es_cluster_target" {
  service_namespace = "elasticsearch"
  resource_id      = aws_elasticsearch_domain.main.arn
  scalable_dimension = "elasticsearch:es/InstanceCount"

  scalable_resource {
    # ... This is where the error occurs ...
  }
}

The Error:

The error message might look something like:

Error: Error creating Application Auto Scaling target: InvalidParameterValue: The specified scalable resource is not valid.

Analysis and Solutions:

The error indicates that the scalable_resource block in your aws_appautoscaling_target resource is not correctly defined. Here are the possible reasons and solutions:

  1. Incorrect resource_id: The resource_id within the scalable_resource block must match the actual resource ID of your ElasticSearch domain. In this case, you're referencing aws_elasticsearch_domain.main.arn, which is the ARN of the entire domain.

    Solution: The resource_id within scalable_resource should be the specific resource ID, not the ARN. You can find the correct ID in the AWS console or through the aws elasticsearch list-domain-names command. Update the scalable_resource block as follows:

    scalable_resource {
      resource_id = "your_elasticsearch_domain_resource_id"
    }
    
  2. Missing or Incorrect resource_type: The resource_type within the scalable_resource block needs to be specified correctly. For ElasticSearch domains, the resource_type is domain.

    Solution: Add the resource_type to your scalable_resource block:

    scalable_resource {
      resource_id = "your_elasticsearch_domain_resource_id"
      resource_type = "domain"
    }
    
  3. Missing properties: For some resource types, you might need to provide additional properties within the scalable_resource block. The required properties depend on the specific resource type.

    Solution: Consult the AWS documentation for your resource type to identify any required properties. For ElasticSearch, there are no additional properties required.

Example:

Here's the corrected aws_appautoscaling_target configuration for scaling the instance count of an ElasticSearch domain:

resource "aws_elasticsearch_domain" "main" {
  # ... ElasticSearch domain configuration ...
}

resource "aws_appautoscaling_target" "es_cluster_target" {
  service_namespace = "elasticsearch"
  resource_id      = aws_elasticsearch_domain.main.arn
  scalable_dimension = "elasticsearch:es/InstanceCount"

  scalable_resource {
    resource_id   = "your_elasticsearch_domain_resource_id"
    resource_type = "domain"
  }
}

Additional Notes:

  • Make sure the resource_id you use in your scalable_resource block is the specific resource ID for your ElasticSearch domain. You can obtain this from the AWS console or using the aws elasticsearch list-domain-names command.
  • The service_namespace and scalable_dimension values should match the specific service and scaling metric you want to use for your ElasticSearch domain. Refer to the AWS documentation for more details on these values.

References:

Conclusion:

By carefully defining the scalable_resource block within your aws_appautoscaling_target resource, you can effectively configure Application Autoscaling for your ElasticSearch domain. Understanding the specific resource ID and resource type for your target resource is crucial for avoiding this common Terraform error.