简体   繁体   English

从 API Gateway 获取 terraform 中 AWS lambda 的端点

[英]Get endpoint from API Gateway for an AWS lambda in terraform

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.我有一个 lambda 部署到 AWS,集成是lambda-proxy ,现在我有一个 API 网关,它为我提供了一个调用 lambda 的端点。 I need to get this endpoint URL in a terraform configuration, how can I do this?我需要在 terraform 配置中获取此端点 URL,我该怎么做?

Data sources don't expose any URL数据源不公开任何 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 ... api_gateway_deployment确实公开了一个invoke_url ,但不幸的是它不能被导入,否则我可以声明资源然后使用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.编辑:澄清一下,lambda 不是由 terraform 部署或管理的。

I have a slightly different but similar setup working on my end.我有一个稍微不同但相似的设置。

I used:我用了:
- Terraform to provision AWS API Gateway Custom Domain - Terraform来配置 AWS API Gateway 自定义域
- Serverless Framework to deploy & link Lambdas to the API GW URL created above. -无服务器框架,用于部署 Lambda 并将其链接到上面创建的 API GW URL。

I exported API GW URL from Terraform as an Output Variable.我从Terraform导出 API GW URL 作为输出变量。 And, passed it as an environment variable to Serverless Framework during function deployment.并且,在功能部署期间将其作为环境变量传递给Serverless Framework

Here it is how in action.这是如何在行动。

1/ main.tf responsible for exporting API GW URL 1/ main.tf负责导出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 2/ serverless.yml负责将 Lambda 函数链接到这个 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 (ie API GW URL that was created earlier) and sets itself to an environment variable APIGW_DOMAIN_NAME.第一部分负责读取 terraform 的输出变量值(即之前创建的 API GW URL)并将自身设置为环境变量 APIGW_DOMAIN_NAME。

Later part initializes serverless-domain-manager plug-in responsible for mapping lambda function to APIGW URL and deploys a serverless setup.后面的部分初始化 serverless-domain-manager 插件,负责将 lambda 函数映射到 APIGW URL 并部署无服务器设置。

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内存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM