Aaron VanSledright

Tag: aws

  • Check EC2 Instance Tags on Launch

    In my ever-growing quest to automate my AWS infrastructure deployments, I realized that just checking my tags wasn’t good enough. I should force myself to put tags in otherwise my instances won’t launch at all.

    I find this particularly useful because I utilize AWS Backup to do automated snapshots nightly of all of my instances. If I don’t put the “Backup” tag onto my instance it will not be included in the rule. This concept of forced tagging could be utilized across many different applications including tagging for development, production, or testing environments.

    To do this I created the Lambda function below. Utilizing EventBridge I have this function every time there is an EC2 instance that enters the “running” state.

    import json
    import boto3
    
    def lambda_handler(event, context):
        detail = event['detail']
        ids = detail['instance-id']
        eventname = detail['state']
        ec2 = boto3.resource('ec2')
        
        while eventname == 'Running':
            print(ids)       
        #Check to see if backup tag is added to the instance
            tag_to_check = 'Backup'
            instance = ec2.Instance(ids)
            for tag in instance.tags:
                if tag_to_check not in [t['Key'] for t in instance.tags]:
                    instance.stop()
                    print("Stopping Instance: ", instance)
        #Get instance state to break the infinite loop
                    state = instance.state['Name']          
                    if state == "shutting-down":
                        print("instance is shutting-down")
                        break
                    elif state == "stopped":
                        print("Instance is already stopped")
                        break
                    elif state == "stopping":
                        print("instance is stopping")
                        break
            break
                

    The function then will check the status of the instance to ensure that it is stopped and then break the loop.

    You can clone the repository from GitHub here:
    https://github.com/avansledright/aws-force-ec2-launch-tags

    If you utilize the script please share it with your friends. Feel free to modify it as you please and let me know how it works for you! As always, if you have any questions feel free to reach out here or on any other 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/posttoSlackLambda

    If this helps you please share this post on your favorite social media platform!

  • Automatically Transcribing Audio Files with Amazon Web Services

    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-HHMMSS

    I 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.

  • The Security Specialty Certification

    Today I sat the AWS Security Specialty Exam. While I didn’t pass I thought to provide some commentary on the experience in relation to the training that I sought out to assist myself in the process.

    I have been a big fan of ACloudGuru. They helped me pass my Solutions Architect exam last year so naturally, I returned to train and learn from them again. Much of the content that I found in this course I found to be a repeat of what I saw in the Solutions Architect material. I didn’t think much of it because I assumed this to be the correct curriculum.

    Boy was I wrong.

    Upon sitting down at the exam center I utilized my standard method of test taking. Answer the questions that you know the answer to first and then go back and hammer out the harder ones using the process of elimination and your knowledge.

    Ryan Kroonenburg does a great job of explaining all the features of AWS and how to utilize them in a lab environment, we miss the actual application level that AWS is asking for in the exam. Now, I’m not saying that Ryan doesn’t know what he is talking about. Quite the contrary. Nor am I blaming my failure on ACloudGuru.

    Advice

    On top of learning all the content outlined in ACloudGuru or LinuxAcademy or whichever training resource you want to utilize, you really need to seek out real life application to these topics. 

    I will be going back over all the labs in the training material and applying them into my product environments (after testing). I think that this is the only way to truly learn what is needed.

    Current Exam Rankings

    Hardest to Easiest (based on what I’ve taken):

    1. Security Specialty
    2. Solutions Architect Associate
    3. SysOps Associate

    If you have any questions regarding the exams feel free to reach out!