Home > Net >  Issue with Creating Application Auto Scaling with AWS Lambda using Terraform
Issue with Creating Application Auto Scaling with AWS Lambda using Terraform

Time:11-20

I'm converting some Cloudformation into Terraform that creates a Lambda and then sets up Provisioned Concurrency and Application Auto Scaling for the Lambda. When Terraform runs the aws_appautoscaling_target resource, it fails with the following message:

Error: Error creating application autoscaling target: ValidationException: Unsupported service namespace, resource type or scalable dimension

I haven't found too many examples of the aws_appautoscaling_target resource being used with Lambdas. Is this no longer supported? For reference, I'm running Terraform version 1.0.11 and I'm using AWS provider version 3.66.0. I'm posting my Terraform below. Thanks.

data "archive_file" "foo_create_dist_pkg" {
  source_dir  = var.lambda_file_location
  output_path = "foo.zip"
  type        = "zip"
}

resource "aws_lambda_function" "foo" {
  function_name = "foo"
  description   = "foo lambda"
  handler       = "foo.main"
  runtime       = "python3.8"
  publish       = true

  role        = "arn:aws:iam::${local.account_id}:role/serverless-role"
  memory_size = 256
  timeout     = 900

  depends_on       = [data.archive_file.foo_create_dist_pkg]
  source_code_hash = data.archive_file.foo_create_dist_pkg.output_base64sha256
  filename         = data.archive_file.foo_create_dist_pkg.output_path
}

resource "aws_lambda_provisioned_concurrency_config" "foo_provisioned_concurrency" {
  function_name                     = aws_lambda_function.foo.function_name
  provisioned_concurrent_executions = 15
  qualifier                         = aws_lambda_function.foo.version
}

resource "aws_appautoscaling_target" "autoscale_foo" {
  max_capacity       = var.PCMax
  min_capacity       = var.PCMin
  resource_id        = "function:${aws_lambda_function.foo.function_name}"
  scalable_dimension = "lambda:function:ProvisionedConcurrency"
  service_namespace  = "lambda"
}

CodePudding user response:

You need to publish your Lambda to get a new version. This can be done by setting publish = true in aws_lambda_function resource. This will give a numeric version for your function which can be used in the aws_appautoscaling_target:

resource "aws_appautoscaling_target" "autoscale_foo" {
  max_capacity       = var.PCMax
  min_capacity       = var.PCMin
  resource_id        = "function:${aws_lambda_function.foo.function_name}:${aws_lambda_function.foo.version}"
  scalable_dimension = "lambda:function:ProvisionedConcurrency"
  service_namespace  = "lambda"
}

Alternatively, you can create an aws_lambda_alias and use that in the aws_appautoscaling_target instead of the Lambda version. Nevertheless, this would require also the function to be published.

  • Related