本帖最后由 jiji 于 2023-7-23 17:49 编辑
lambda代码如下(Node.js 18.x)
- import AWS from 'aws-sdk';
- export async function handler(event) {
- const cloudFrontDistributionId = 'CloudFront分配的ID';
- try {
- const cloudFront = new AWS.CloudFront();
-
- const distribution = await cloudFront.getDistributionConfig({
- Id: cloudFrontDistributionId,
- }).promise();
- distribution.DistributionConfig.Enabled = false;
- await cloudFront.updateDistribution({
- DistributionConfig: distribution.DistributionConfig,
- Id: cloudFrontDistributionId,
- IfMatch: distribution.ETag,
- }).promise();
- return {
- statusCode: 200,
- body: 'CloudFront 关闭成功',
- };
- } catch (error) {
- console.error(error);
- return {
- statusCode: 500,
- body: `关闭 CloudFront 分配时出错:${error.message}`,
- };
- }
- }
复制代码
SKD下载:https://mega.nz/file/M38DwLKC#pundUKB_owHxxOa6Cm5LPf2TE9vpJHyRfGcQ6SGhsc4
将SDK插入到”层“
自行搭配cloudwatch的预警处理,流量几分钟内流量到多少(也可以选择请求或者其他的),然后触发lambda,关闭CloudFront
比如我设置的5分钟200M流量,30分钟内两次突破预警线,就会触发触发lambda,关闭CloudFront
|