lambda代码如下(Node.js 18.x)
[ol]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}`,
};
}
}
[/ol]复制代码
SKD下载:https://mega.nz/file/M38DwLKC#pundUKB_owHxxOa6Cm5LPf2TE9vpJHyRfGcQ6SGhsc4
将SDK插入到”层“
自行搭配cloudwatch的预警处理,流量几分钟内流量到多少(也可以选择请求或者其他的),然后触发lambda,关闭CloudFront