Search code examples
aws-lambdaterraformaws-lambda-layers

How to get all the aliases of a lambda function using terraform?


How to get all the aliases of a lambda function hosted on a AWS Region (example eu-west-1) using terraform?


Solution

  • We can get all the aliases of a lambda function using the data external command.

    Example:

    data external "get_all_aliases_of_lambda" {
      program = [
        "bash", "-c",
        "aws lambda list-aliases --region ${var.aws_region_primary} --function-name ${module.lambda.lambda_function_name} --query \" { \"aliases\" : to_string( Aliases[*].Name) } \" --output json"
      ]
    }
    

    Furthermore, the aliases can be extracted from the JSON and represented as a set using toset() and jsondecode() methods of terraform.

    Example:

    locals {
      aliases_available = toset(jsondecode(data.external.get_all_aliases_of_lambda[0].result.aliases))
    }