要在AWS Lambda中发送定时HTTP请求,您可以使用以下步骤和代码示例来实现:
创建一个Lambda函数:
编写Lambda函数代码:
Node.js示例代码:
const https = require('https');
exports.handler = async (event, context) => {
const options = {
hostname: 'example.com',
port: 443,
path: '/api/endpoint',
method: 'GET'
};
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`);
res.on('data', (d) => {
process.stdout.write(d);
});
});
req.on('error', (error) => {
console.error(error);
});
req.end();
};
Python示例代码:
import http.client
def lambda_handler(event, context):
conn = http.client.HTTPSConnection("example.com")
conn.request("GET", "/api/endpoint")
res = conn.getresponse()
print(res.status, res.reason)
data = res.read()
print(data.decode("utf-8"))
完成上述步骤后,Lambda函数将在预定时间间隔内发送HTTP请求。您可以根据自己的需求调整代码中的请求参数和URL。
上一篇:AWS Lambda发送部分响应