3

I have a lambda deployed to AWS, integration is lambda-proxy, now I have an API Gateway which gives me an endpoint for calling the lambda. I need to get this endpoint URL in a terraform configuration, how can I do this?

Data sources don't expose any URL

data "aws_lambda_function" "myfunction" {
  function_name = "my-function-name"
}

data "aws_api_gateway_rest_api" "my_rest_api" {
  name = "my-rest-api"
}

api_gateway_deployment does expose an invoke_url, but infortunately it can't be imported, otherwise I could have declared the resources and then use terraform import ...

resource "aws_api_gateway_rest_api" "my_rest_api" {
  name        = "my-rest-api"
  tags = {
    STAGE = "prod"
  }
}

resource "aws_api_gateway_deployment" "my_rest_api" {
  rest_api_id = aws_api_gateway_rest_api.my_rest_api.id
  stage_name  = "prod"
}

EDIT: to clarify, the lambda is not deployed or managed by terraform.

3
  • You can definitely use the invoke_url exported attribute for what you need. I am unsure why you would need to use it with import unless you are doing something very unusual. Please share the code for where you need the endpoint and we can show you how to do it. Commented Jan 3, 2020 at 12:35
  • @MattSchuchard The amazon lambda is not managed by terraform at all, that's why I need the import or the data source. Commented Jan 3, 2020 at 12:44
  • I've struggled with this while trying to deploy an app with an AWS SAM template. The SAM takes care of creating the API Gateway and I wanted to reference that in a CloudFront configuraiton managed in Terraform. I've ultimately decided to configure a custom Domain in my SAM template and reference that explicitly in my CloudFront configuration. It doesn't offer the level resource checking I wanted but it seems to work. Commented Nov 7, 2020 at 19:23

1 Answer 1

0

I have a slightly different but similar setup working on my end.

I used:
- Terraform to provision AWS API Gateway Custom Domain
- Serverless Framework to deploy & link Lambdas to the API GW URL created above.

I exported API GW URL from Terraform as an Output Variable. And, passed it as an environment variable to Serverless Framework during function deployment.

Here it is how in action.

1/ main.tf responsible for exporting API GW URL

output "tf_output_aws_apigw_domain_name__domain_name"
{
  value = "${aws_api_gateway_domain_name.tf_aws_apigw_domain_for_apis.domain_name}"
}

2/ serverless.yml responsible for linking Lambda function to this API GW URL

[...]
  customDomain:
    domainName: ${env:APIGW_DOMAIN_NAME}
    basePath: /myapi
    stage: dev
    createRoute53Record: true
[...]

Here are the commands which glue them together (in my automation pipeline).

First part is responsible to read the terraform's output variable value (i.e. API GW URL that was created earlier) and sets itself to an environment variable APIGW_DOMAIN_NAME.

Later part initializes serverless-domain-manager plug-in responsible for mapping lambda function to APIGW URL and deploys a serverless setup.

terraform init ...
terraform refresh ...
export APIGW_DOMAIN_NAME=`terraform output tf_output_aws_apigw_domain_name__domain_name`

sls plugin install -n serverless-domain-manager
sls deploy --debug

cheers,
ram

Sign up to request clarification or add additional context in comments.

1 Comment

You used another TF resource, aws_api_gateway_domain_name, which sets a custom domain name. For those of us that don't/can't, I guess the best way is to just generate the output by constructing the string, something like "${API_ID}.execute-api.${REGION}.amazonaws.com/${STAGE}" and use as a template and replace the values for the output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.