简体   繁体   中英

How can I troubleshoot my AWS Lambda function?

I created a lambda function in AWS and apparently it is throwing an error. Here it is:

import json
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen
import boto3
#Create a SSM Client to access parameter store
ssm = boto3.client('ssm')

def lambda_handler(event, context):
    # TODO implement
    #return {
    #    'statusCode': 200,
    #    'body': json.dumps('Hello from Lambda!')
    #}
    
    slack_message = {
        'text' = f'Hello World'
    }
    
    #retrieve webhook url from parameter store
    webhook_url = ssm.get_parameter(Name='slackwebhookurl', WithDecryption=True)
    
    #make  request to the API
    
    req = Request(webhook_url['Parameter']['Value'],
                    json.dumps(slack_message).encode('utf-8'))
                    
    try:
        response = urlopen(req)
        response.read()
        print("Messge posted to Slack")
    except HTTPError as e:
        print(f'Request failed: {e.code} {e.reason})
    except URLError as e:
        print(f'Server Connection failed:  {e.reason})

It's triggered by an AWS SNS notification. It's supposed to grab a webhook url for a slack channel and then send the notification to Slack.

Can anyone see what the problem is?

If it's not obvious, can someone direct me to a tutorial on how to test AWS Lambda functions?

Thanks.

AWS Lambda has an in-built Test functionality. You can click the Test button and configure an input in case the function uses values from event .

Logs will be displayed within the Lambda console.

Also, ensure that the IAM Role associated with the AWS Lambda function has the AWSLambdaBasicExecutionRole permission policy so that it can write to CloudWatch Logs. Then, you can go to the Monitoring tab of the function and click View logs in CloudWatch to see past logs.

You can add print() statements in your code, which will appear in the Logs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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