繁体   English   中英

使用 lambda 使用 boto3 创建 cloudwatch 仪表板

[英]create cloudwatch dashboard with boto3 using lambda

我想为多个 EC2 创建 cloudwatch 仪表板我有问题将 JSON 仪表板放在 bady 并使区域和 id 实例值可变

编码

from __future__ import print_function

import json

import boto3

def lambda_handler(event, context):

    ec2_client = boto3.client('ec2')
    CW_client = boto3.client('cloudwatch', region_name='eu-west-1')

    regions = [region['RegionName']
               for region in ec2_client.describe_regions()['Regions']]
    for region in regions:
        print('Instances in EC2 Region {0}:'.format(region))
        ec2 = boto3.resource('ec2',region_name=region)

        instances = ec2.instances.filter(
            Filters=[
                {'Name': 'tag:backup', 'Values': ['true']}
            ]
        )
        for i in instances.all():
            #for ID in i.InstanceId.all():
            print(i.id)
            instance_id = i.id
            response = CW_client.put_dashboard(DashboardName='test', DashboardBody='{"widgets": [{"type": "metric", "x": 12, "y": 0, "width": 12, "height": 6, "properties": {"metrics": [[ "AWS/EC2", "CPUUtilization", "InstanceId", instance_id ]], "view": "timeSeries", "stacked": false, "region": region, "stat": "Average", "period": 60, "title": "CPUUtilization" }}]}')

错误


  "errorMessage": "An error occurred (InvalidParameterInput) when calling the PutDashboard operation: The field DashboardBody must be a valid JSON object",
  "errorType": "DashboardInvalidInputError",

您的DashboardBody包含文字字符串instance_idregion (不带引号)。 这就是 JSON 无效的原因。 您需要instance_idregion,而不是简单的词instance_idregion

您应该使用字符串插值。 例如:

region = "us-east-1"
instance_id = "i-1234567890"

partbody = '{"region":"%s", "instance_id":"%s"}' % (region, instance_id)

或者您可以使用 f 字符串,但随后您必须引用大括号,如下所示:

partbody = f'{{"region":"{region}", "instance_id":"{instance_id}"}}'

这两个选项都会产生如下所示的字符串:

{"region":"us-east-1", "instance_id":"i-1234567890"}

请注意,我正在向您展示如何使用字符串插值将变量值注入到字符串中。 现在您需要执行此操作并将regioninstance_id注入DashboardBody字符串。 例如:

DashboardBody='{"widgets": [{"type": "metric", "x": 12, "y": 0, "width": 12, "height": 6, "properties": {"metrics": [[ "AWS/EC2", "CPUUtilization", "InstanceId","%s" ]], "view": "timeSeries", "stacked": false, "region":"%s", "stat": "Average", "period": 60, "title": "CPUUtilization" }}]}' % (instance_id, region)
Your json body for 'DashboardBody' should be like this:

    {
       "widgets": [
          {
             "type": "metric",
             "x": 12,
             "y": 0,
             "width": 12,
             "height": 6,
             "properties":{
                "metrics":[
                   [
                      "AWS/EC2",
                      "CPUUtilization",
                      "InstanceId",
                      instance_id
                   ]
                ],
                "view": "timeSeries",
                "stacked": false,
                "region": regions,
                "stat": "Average", 
                "period": 60, 
                "title": "CPUUtilization"

             }
          }
       ]
    }

Also You were passing incorrect value for "region" parameter, it should be regions (as declared in your code) and not region.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM