Your cart is currently empty!
Tag: python3
-
EC2 Action Slack Notification
I took a brief break from my Lambda function creation journey to go on vacation but, now i’m back!
This function will notify a Slack channel of your choosing when an EC2 instance enters “Starting, Stopping, Stopped, or Shutting-Down” status. I thought this might be useful for instances that reside under a load balancer. It would be useful to see when your load balancer is scaling up or down in real-time via Slack notification.
In order to use this function, you will need to create a Slack Application with an OAuth key and set that key as an environment variable in your Lambda function. If you are unsure of how to do this I can walk you through it!
Please review the function below
import logging import requests import boto3 import os from urllib.parse import unquote_plus from slack import WebClient from slack.errors import SlackApiError logging.basicConfig(level=logging.DEBUG) # Check EC2 Status def lambda_handler(event, context): detail = event['detail'] ids = detail['instance-id'] eventname = detail['state'] ec2 = boto3.resource('ec2') # Slack Variables slack_token = os.environ["slackBot"] client = WebClient(token=slack_token) channel_string = "XXXXXXXXXXXXXXXXXXXX" # Post to slack that the instance is running if eventname == 'running': try: instance = ids response_string = f"The instance: {instance} has started" response = client.chat_postMessage( channel= channel_string, text="An Instance has started", blocks = [{"type": "section", "text": {"type": "plain_text", "text": response_string}}] ) except SlackApiError as e: assert e.response["error"] #Post to slack that instance is shutting down elif eventname == 'shutting-down': try: instance = ids response_string = f"The instance: {instance} is shutting down" response = client.chat_postMessage( channel= channel_string, text="An Instance is Shutting Down", blocks = [{"type": "section", "text": {"type": "plain_text", "text": response_string}}] ) except SlackApiError as e: assert e.response["error"] elif eventname == 'stopped': try: instance = ids response_string = f"The instance: {instance} has stopped" response = client.chat_postMessage( channel= channel_string, text="An Instance has stopped", blocks = [{"type": "section", "text": {"type": "plain_text", "text": response_string}}] ) except SlackApiError as e: assert e.response["error"] elif eventname == 'stopping': try: instance = ids response_string = f"The instance: {instance} is stopping" response = client.chat_postMessage( channel= channel_string, text="An Instance is stopping", blocks = [{"type": "section", "text": {"type": "plain_text", "text": response_string}}] ) except SlackApiError as e: assert e.response["error"]As always the function is available on GitHub as well:
https://github.com/avansledright/ec2ActionPostToSlackIf you find this function helpful please share it with your friends or repost it on your favorite social media platform!
-
Lambda Function Post to Slack
I wrote this script out of a need to practice my Python skills. The idea is that if a file gets uploaded to an S3 bucket then the function will trigger and a message with that file name will be posted to a Slack channel of your choosing.
To utilize this you will need to include the Slack pip package as well as the slackclient pip package when you upload the function to the AWS Console.
You will also need to create an OAuth key for a Slack application. If you are unfamiliar with this process feel free to drop a comment below and or shoot me a message and I can walk you through the process or write a second part of the guide.
Here is a link to the project:
https://github.com/avansledright/posttoSlackLambdaIf this helps you please share this post on your favorite social media platform!
-

Automatically Transcribing Audio Files with Amazon Web Services
I wrote this Lambda function to automatically transcribe audio files that are uploaded to an S3 bucket. This is written in Python3 and utilizes the Boto3 library.
You will need to give your Lambda function permissions to access S3, Transcribe and CloudWatch.
The script will create an AWS Transcribe job with the format:
'filetranscription'+YYYYMMDD-HHMMSSI will be iterating over the script to hopefully add in a web front end as well as potentially branching to do voice call transcriptions for phone calls and Amazon Connect.
You can view the code here
If you have questions or comments feel free to reach out to me here or on any Social Media.