我正在尝试按需创建S3存储桶并监控其成本。 众所周知,Amazon的API很棒,所以我有点固执。
这就是我要执行的几个步骤
我创建了一个存储桶
s3.createBucket({
Bucket: bucket,
ACL: 'public-read',
}).promise();
我的第一个问题是:如何在此存储桶上(完全)启用cloudwatch?
我的第二个问题是:有人知道如何根据CloudWatch的指标完全计算成本吗?
然后,我需要从cloudwatch中获取一些数据,例如每周评估成本。
const date = new Date();
const StartTime = new Date(date.getFullYear(), date.getMonth(), 1);
const EndTime = new Date(date.getFullYear(), date.getMonth() + 1, 0);
const Period = Math.floor((EndTime.getTime() - StartTime.getTime()) / 1000);
cloudwatch
.getMetricData({
StartTime,
EndTime,
MetricDataQueries: [
{
Id: 'size',
MetricStat: {
Metric: {
Dimensions: [
{
Name: 'StorageType',
Value: 'StandardStorage',
},
{
Name: 'BucketName',
Value: 'my-bucket',
},
],
MetricName: 'BucketSizeBytes',
Namespace: 'AWS/S3'
},
Period,
Stat: 'Average',
Unit: 'Bytes'
},
ReturnData: true,
},
{
Id: 'count',
MetricStat: {
Metric: {
Dimensions: [
{
Name: 'StorageType',
Value: 'AllStorageTypes',
},
{
Name: 'BucketName',
Value: 'my-bucket',
},
],
MetricName: 'NumberOfObjects',
Namespace: 'AWS/S3'
},
Period,
Stat: 'Average',
Unit: 'Count',
},
ReturnData: true,
},
{
Id: 'downloaded',
MetricStat: {
Metric: {
Dimensions: [
{
Name: 'FilterId',
Value: 'EntireBucket',
},
{
Name: 'BucketName',
Value: 'my-bucket',
},
],
MetricName: 'BytesDownloaded',
Namespace: 'AWS/S3'
},
Period,
Stat: 'Average',
Unit: 'Bytes'
},
ReturnData: true,
},
],
}, handleResponse);
哪个会给我这样的信息
{
"ResponseMetadata": {
"RequestId": "588f3c78-bc39-11e8-a704-cbccb1b4e355"
},
"MetricDataResults": [
{
"Id": "size",
"Label": "StandardStorage BucketSizeBytes",
"Timestamps": [
"2018-08-31T22:00:00.000Z"
],
"Values": [
1030
],
"StatusCode": "Complete",
"Messages": []
},
{
"Id": "count",
"Label": "AllStorageTypes NumberOfObjects",
"Timestamps": [
"2018-08-31T22:00:00.000Z"
],
"Values": [
3
],
"StatusCode": "Complete",
"Messages": []
},
{
"Id": "downloaded",
"Label": "EntireBucket BytesDownloaded",
"Timestamps": [
"2018-08-31T22:00:00.000Z"
],
"Values": [
1342
],
"StatusCode": "Complete",
"Messages": []
}
]
}
考虑到AWS文档,我没有其他问题,主要是将这段代码留在某个地方,以帮助任何会经过这里的人...