首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >aws lambda put-metric-data超时

aws lambda put-metric-data超时
EN

Stack Overflow用户
提问于 2021-03-12 08:40:01
回答 1查看 195关注 0票数 0

我需要从RDS获取一些数据,并将其作为cloudwatch指标进行推送。

我在AWS中创建了一个lambda,它与一个可以访问互联网的VPC相关联。但是指标的推送超时了。

为了调试这个,我做了一个dns解析,它工作正常。

有什么想法吗?

代码如下:

代码语言:javascript
复制
    print('Loading function')

    try:
        conn = pymysql.connect(host="kmydb.something.eu-west-1.rds.amazonaws.com", user="myuser", passwd="mypass", db="mydb", connect_timeout=5)
        print("SUCCESS: Connection to RDS MySQL instance succeeded")
        item_count = 0

        with conn.cursor() as cur:
            print("Executing SQL command")
            cur.execute("select count(*) from something")
            for row in cur:
                item_count = row[0]
            print("DONE: Executing SQL command -> Rows: {}".format(item_count) )
        conn.commit()
        print("DEBUG: Going to push now.")

        hostName    = "something.something.com"
        ipAddress   = socket.gethostbyname(hostName)
        print("IP address of the host name {} is: {}".format(hostName, ipAddress))

        if pushToCloudwatch(item_count):
            print("DEBUG: Pushed sucessfull.")
        else:
            print("ERROR: Pushed NOT sucessfull.")

    except pymysql.MySQLError as e:
        print("ERROR: Unexpected error: Could not connect to MySQL instance.")
        print("error: {}".format(e))
        sys.exit()


def pushToCloudwatch(channels):
    print("ERROR: Creating client.")
    client = boto3.client('cloudwatch',region_name="us-west-1")
    print("ERROR: client created... pushing.")

    response = client.put_metric_data(
        Namespace='INFRA-VOICE',
        MetricData=[
            {
                'MetricName': 'CurrentP',
                'Dimensions': [
                    {
                        'Name': 'INFRA',
                        'Value': 'CurrentP'
                    },
                ],
                'Value': 0,
                'Unit': 'Count'
            }
        ]
    )
    print("ERROR: Response received.")
    print( "Response: {}".format(response) )
    return True
EN

回答 1

Stack Overflow用户

发布于 2021-03-13 05:50:53

所以重点是,你必须:

  • 将您的lambda放在私有子网(在您现有的vpc中),如果您必须创建它,请执行事件。
  • 在公有子网上创建NatGateway。
  • 然后在私有子网上,您需要添加指向NatGateway (在公有vpc上)的路由表0.0.0.0/0的路由
  • NatGateway会将您的流量通过其自己的网关

<代码>F29

如下所示:

这对我帮助很大:

Amazon AWS NAT Gatway not working, EC2 doesn't register in ECS Cluster

解决了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66592586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档