Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

json - Can't send running ec2 instance list from lambda to slack using webhook url

I have a lambda function which filters out running ec2 instances and stores them in an array, then it is sent as a payload to the slack channel. The problem is when I run the function it will not send the list but the print command will list it out on output.

import json
import boto3
import logging
import os
import urllib3
import urllib.parse

#from urlparse import parse_qs
from boto3 import resource

ec2 = boto3.resource('ec2')
region = 'us-east-1'



def lambda_handler(event, context):
 
    ec2 = boto3.resource('ec2', region)
    url = "https://hooks.slack.com/services/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"
    text = "hi"
    filters = [
        {
            'Name': 'instance-state-name', 
            'Values': ['running']
        }
    ]

    instances = ec2.instances.filter(Filters = filters)

    RunningInstances = []

    for instance in instances:
        RunningInstances.append(instance.id)

    instanceList = json.dumps(RunningInstances)
    
 
                                       #List out running ec2 instances

  
    print(instanceList) 
    http = urllib3.PoolManager()
  
   
    r = http.request("POST",
                  "https://hooks.slack.com/services/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",
                 body = json.dumps(instanceList),
                 headers = {"Content-Type":"application/json"})
                 ```

                 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...