API 网关和 Lambda 函数:第一种方法
API Gateway and Lambda Function: first approach
我对 API 网关和 AWS Lambda 很陌生,我正尝试在具有以下元素的场景中使用它们:
- 具有私有和 public 子网的 VPC
- 安装了 Lamp 的 AMI EC2(免费套餐)
- 一个带有一些文本的简单 index.html 页面(说 "This is a test page")
我想做的就是能打http://myprivateIp/myexample/index.hml through the use of the API gateway and Lambda as it seems to be suggested in the AWS documentation. I have, then, re-used the basic Hello World lambda example (one of the AWS blueprints) for my first lambda function and included the VPC details (with the private subnet too) as requested in the wizard. I have also created a sample API with one resource (myexample, in this case) and the Get method with the Lambda Function Integration Type and the Hello World function. As per the documentation, I have created the correct permissions (http://docs.aws.amazon.com/apigateway/latest/developerguide/create-lambda-roles.html)。我已经从我的 API 测试了 GET 方法,它根据 AWS 预先存在的蓝图正确 return 响应 "Hello World"。我知道这听起来像是一个非常幼稚的问题,但我不确定我是否真的证明了我可以命中我的 VPC?例如,我希望能够 return 来自我的 index.html 页面的示例文本,这可能吗?在这种特定情况下,我是否误解了 AWS lambda 的用途?
感谢您的帮助,
编辑:
所以,我在 Node JS 4.3 中整理了以下内容:
'use strict';
console.log('We are about to send a Get Request');
exports.handler = function(event, context, callback) {
var http = require("http")
var request = http.get("http://domain/example/index.html")
console.log('"This is my request":"' + request + '"');
callback(null, "The URL is succesfully retrieved")
};
测试运行成功,我说的对不对,证明我可以在VPC上访问运行页面?
您可以在 Lambda 函数中将 EC2 连接到私有 IP。这意味着您可以从您的 Lambda 函数访问您的 VPC。
您是对的 - 只要您的 Lambda 函数配置为 运行 在同一 VPC 内,您就可以通过 Lambda 函数向 VPC 中的端点发出 HTTP 请求。
因此,您可以使用 API 网关调用 Lambda 并代理从 VPC 内的 HTTP 端点返回的响应。
API 网关无法直接调用 VPC 中的 HTTP 端点,因此建议您当前使用 Lambda 的方法。
我对 API 网关和 AWS Lambda 很陌生,我正尝试在具有以下元素的场景中使用它们:
- 具有私有和 public 子网的 VPC
- 安装了 Lamp 的 AMI EC2(免费套餐)
- 一个带有一些文本的简单 index.html 页面(说 "This is a test page")
我想做的就是能打http://myprivateIp/myexample/index.hml through the use of the API gateway and Lambda as it seems to be suggested in the AWS documentation. I have, then, re-used the basic Hello World lambda example (one of the AWS blueprints) for my first lambda function and included the VPC details (with the private subnet too) as requested in the wizard. I have also created a sample API with one resource (myexample, in this case) and the Get method with the Lambda Function Integration Type and the Hello World function. As per the documentation, I have created the correct permissions (http://docs.aws.amazon.com/apigateway/latest/developerguide/create-lambda-roles.html)。我已经从我的 API 测试了 GET 方法,它根据 AWS 预先存在的蓝图正确 return 响应 "Hello World"。我知道这听起来像是一个非常幼稚的问题,但我不确定我是否真的证明了我可以命中我的 VPC?例如,我希望能够 return 来自我的 index.html 页面的示例文本,这可能吗?在这种特定情况下,我是否误解了 AWS lambda 的用途?
感谢您的帮助,
编辑:
所以,我在 Node JS 4.3 中整理了以下内容:
'use strict';
console.log('We are about to send a Get Request');
exports.handler = function(event, context, callback) {
var http = require("http")
var request = http.get("http://domain/example/index.html")
console.log('"This is my request":"' + request + '"');
callback(null, "The URL is succesfully retrieved")
};
测试运行成功,我说的对不对,证明我可以在VPC上访问运行页面?
您可以在 Lambda 函数中将 EC2 连接到私有 IP。这意味着您可以从您的 Lambda 函数访问您的 VPC。
您是对的 - 只要您的 Lambda 函数配置为 运行 在同一 VPC 内,您就可以通过 Lambda 函数向 VPC 中的端点发出 HTTP 请求。
因此,您可以使用 API 网关调用 Lambda 并代理从 VPC 内的 HTTP 端点返回的响应。
API 网关无法直接调用 VPC 中的 HTTP 端点,因此建议您当前使用 Lambda 的方法。