Skip to main content
 首页 » 编程设计

python之使用 python 的 Lambda@edge 函数

2025年05月04日11mengfanrong

我们正在尝试编写一个 lambda@edge 函数来触发 viewer-request。我能够找到很多关于使用 JavaScript 的示例。

例如

'use strict'; 
 
exports.handler = (event, context, callback) => { 
    console.log('Event: ', JSON.stringify(event, null, 2)); 
    console.log('Context: ', JSON.stringify(context, null, 2)); 
    const request = event.Records[0].cf.request; 
 
    // You can also store and read the redirect map 
    // in DynamoDB or S3, for example. 
    const redirects = { 
        '/r/music':    '/card/bcbd2481', 
        '/r/tree':     '/card/da8398f4', 
    }; 
 
    if (redirects[request.uri]) { 
        return callback(null, { 
            status: '302', 
            statusDescription: 'Found', 
            headers: { 
                'location': [{  
                    key: 'Location', 
                    value: redirects[request.uri] }] 
            } 
        }); 
    } 
 
    callback(null, request); 
}; 

以上代码将重定向匹配特定路径的请求。任何人都可以建议如何将类似的代码移植到 python 吗?或共享有关部署 python lambda@edge 函数的资源/信息。

谢谢

请您参考如下方法:

Lambda@Edge 以前 只允许 Node.js,所以在最初提出这个问题时,无法在 Python 或除 Node 之外的任何其他语言中创建 Lambda@Edge 函数。 js(或在 Node.js 包装器内)。

截至 2018 年 8 月,Lambda@Edge 仅支持 Node.js 6.10 和 8.10 运行时环境。

Lambda@Edge 现在支持 Node.js 8.10、Node.js 10.x 和 Python 3.7 ,截至 2019 年 8 月。

边缘环境在许多方面与一般的 Lambda 产品明显不同:参见 Lambda Function Configuration and Execution Environment在 CloudFront 开发人员指南中。