rpc
rpc

Reputation: 97

AWS API Gateway Circular Dependence using Stage Variables for Lambda Integration with Terraform

I've got a working API Gateway with a hardcoded Lambda integration deployed using terraform. I want to replace the hardcoded Lambda ARN in the integration with a stage variable, which will open the door to canary usage and better flexibility. This seems to be well aligned with the intent of stage variables.

However when I implement I get a circular dependency I can't seem to break. It looks like:

The stage has the stage variable pointing to the Lambda and:

And round and round we go

Yet, as noted, this seems exactly what stage variables are for. Dual deploys won't do, need a seamless one pass deployment.

Has anyone solved this issue?

Upvotes: -1

Views: 74

Answers (1)

rpc
rpc

Reputation: 97

For others ... the problem appeared to be a circular dependency but was in fact a nuance in how stage variables are defined and accessed.

Turns out that when you set a stage variable with the parameter "lambda_function_name", they actually mean name, not ARN or some other more common designation. Who woulda thunk it.

So you wind up with something like:

resource "aws_api_gateway_stage" "tunes" {
  deployment_id = ...
  variables = {
    "lambda_function_name" = aws_lambda_function.somefunction.function_name
  }

Then you wrap and reference it in the integration like this:

resource "aws_api_gateway_integration" "lambda" {
  rest_api_id =...
  type                    = "AWS_PROXY"
  uri = "arn:aws:apigateway:${var.region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${var.region}:${data.aws_caller_identity.current.account_id}:function:$${stageVariables.lambda_function_name}/invocations"
}

Seems to work fine

Upvotes: 0

Related Questions