Question:
I’m trying to get provisioned concurrency to work with my API gateway backed Lambda function.
Following config does not work, AWS seems to completely ignore provisioned concurrency and will cold-start.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
resource "aws_lambda_function" "user_history" { publish = true filename = "../distribution/target/distribution/user-history.jar" function_name = "user-history_${var.user}_${terraform.workspace}" role = aws_iam_role.lambda.arn source_code_hash = filebase64sha256("../distribution/target/distribution/user-history.jar") runtime = "java11" timeout = 240 memory_size = 512 } resource "aws_api_gateway_integration" "user_history" { rest_api_id = aws_api_gateway_rest_api.vnm_api.id resource_id = aws_api_gateway_resource.user_history.id http_method = aws_api_gateway_method.user_history.http_method integration_http_method = "POST" type = "AWS_PROXY" uri = aws_lambda_function.user_history.invoke_arn } resource "aws_lambda_provisioned_concurrency_config" "user_history_provisioning" { function_name = aws_lambda_function.user_history.function_name provisioned_concurrent_executions = 2 count = var.provisioning == true ? 1 : 0 qualifier = aws_lambda_function.user_history.version } |
figuring it might’ve something to do with the version not being specified I tried adding config following the accepted answer here: Terraform – what is the URI to invoke lambda via alias?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
resource "aws_lambda_alias" "user_history_alias"{ name = "user_history_version" description = "Versioned alias" function_name = aws_lambda_function.user_history.arn function_version = aws_lambda_function.user_history.version } data "aws_lambda_function" "user_history" { function_name = aws_lambda_function.user_history.function_name qualifier = "user_history_version" } resource "aws_api_gateway_integration" "user_history" { uri = aws_lambda_function.user_history.user_history_version.invoke_arn } |
but now I’m getting an error that states This object has no argument, nested block, or exported attribute named "user_history_version"
Does anyone know how I can get my provisioned concurrency working?
Answer:
You’re on the right track – if you use the unqualified lambda arn, you reference the version at $LATEST, while your provisioned concurrency uses the latest published version.
You can fix your problem by either referencing the alias ARN (aws_lambda_alias. user_history_alias.arn
), or, if you don’t need the alias for something else, referencing aws_lambda_function.user_history.qualified_arn
, which will be the latest published version.