Tag: terraform

  • Cloudwatch Alarm AI Agent

    I think one of the biggest time sucks is getting a vague alert or issue and not having a clue on where to start with troubleshooting.

    I covered this in the past when I built an agent that can review your AWS bill and find practical ways to save money within your account. This application wasn’t event driven but rather a container that you could spin up when you needed a review or something you could leave running in your environment. If we take a same read-only approach to building an AWS Agent we can have have a new event driven teammate that helps us with our initial troubleshooting.

    The process flow is straight forward:

    1. Given a Cloudwatch Alarm
    2. Send a notification to SNS
    3. Subscribe a Lambda function to the topic (this is our teammate)
    4. The function utilizes the AWS Nova Lite model to investigate the contents of the alarm and utilizes its read only capabilities to find potential solutions
    5. The agent sends its findings to you on your preferred platform

    For my environment I primarily utilize Slack for alerting and messaging so I built that integration. Here is an architecture diagram:

    When the alarm triggers we should see a message in Slack like:

    The AI is capable of providing you actionable steps to either find the root cause of the problem or in some cases, present you with steps to solve the problem.

    This workflow significantly reduces your troubleshooting time and by reducing the troubleshooting time it reduces your downtime.

    So, if this is something you are interested in deploying I have created a Terraform module so you can quickly deploy it into your own environment to reduce your troubleshooting steps!

    Check it out here: https://aiopscrew.com

    If you have questions feel free to reach out to me at anytime!

  • Deploying a Strands Agent on AWS Lambda using Terraform

    Recently I’ve been exploring the AI space a lot more as I’m sure a lot of you are doing as well. I’ve been looking at the Strands Agent SDK. I see this SDK as being very helpful in building out agents in the future (follow the blog to see what I come up with!).

    One thing that is not included in the SDK is the ability to deploy with Terraform. The SDK includes examples of how to package and deploy with Amazon Web Services CDK so I adapted that to utilize Terraform.

    I took my adaptation a step further and added an API Gateway layer so that you have the beginnings of a very simple AI agent deployed with the Strands SDK.

    Check out the code here: https://github.com/avansledright/terraform-strands-agent-api

    The code in the repository is fairly simple and includes everything you need to build an API Gateway, Lambda function, and some other useful resources just to help out.

    The key to all of this is packaging the required dependencies inside of the Lambda Layer. Without this the function will not work.

    File structure:
    terraform-strands-agent-api/
    └── lambda_code/
    │ ├── lambda_function.py # Your Strands agent logic
    │ └── requirements.txt # strands-agents + dependencies
    ├── api_gateway.tf # API Gateway configuration
    ├── iam.tf # IAM roles and policies
    ├── lambda.tf # Lambda function setup
    ├── locals.tf # Environment variables
    ├── logs.tf # CloudWatch logging
    ├── s3.tf # Deployment artifacts
    ├── variables.tf # Configurable inputs
    └── outputs.tf # API endpoints and resource IDs

    You shouldn’t have to change much in any of these files until you want to fully start customizing the actual functionality of the agent.

    To get started follow the instructions below!

    git clone https://github.com/avansledright/terraform-strands-agent-api
    cd terraform-strands-agent-api
    
    # Configure your settings. Add other values as needed
    echo 'aws_region = "us-west-2"' > terraform.tfvars
    
    # Deploy everything
    terraform init
    terraform plan
    terraform apply

    If everything goes as planned you should see the output of a curl command which will give you the ability to test the demo code.

    If you run into any issues feel free to let me know! I’d be happy to help you get this up and running.

    Github

    If this has helped you in any way, please share it on your social media and with any of your friends!

  • Creating a List of API Gateway resources using Terraform

    For some reason when you utilize Terraform with AWS, specifically when you want to get a list of API Gateway resources, that data element simply does not exist. Below is a relatively quick solution that will create a comma separated list of API Gateways so that you can iterate through them.

    In order to execute this element you need to have the AWS CLI setup within your preferred deployment method. Personally, I love GitHub Actions so I needed to add another stage in my deployment to install the CLI.

    The way this work is to create a data element that you can trigger as needed to execute a simple shell script.

    data "external" "apis" {
       program = ["sh", "-c", "aws apigateway get-rest-apis    --query 'items[?starts_with(name,`${var.prefix}`)].name' --output json | jq -r '{\"names\": (. | join(\",\"))}'"]
    }

    We also are creating a variable called “prefix” so that you can filter as required by your project. Personally, I used this to create Cloudwatch Dashboards so I can easily monitor my resources.

    If this is helpful for you, please share it on your social media!

  • Automating Proper Terraform Formatting using Git Pre-Hooks

    I’ve noticed lately that a lot of Terraform is formatted differently. Some developers utilize two indents, others one indent. As long as the Terraform as functional most people overlook the formatting of their infrastructure as code files.

    Personally I don’t think we should ever push messy code into our repositories. How could we solve this problem? Well, Terraform has a built in formatter the terraform fmt command will automatically format your code.

    #!/usr/bin/env bash
    
    # Initialize variables
    EXIT_CODE=0
    AFFECTED_FILES=()
    
    # Detect OS for cross-platform compatibility
    OS=$(uname -s)
    IS_WINDOWS=false
    if [[ "$OS" == MINGW* ]] || [[ "$OS" == CYGWIN* ]] || [[ "$OS" == MSYS* ]]; then
        IS_WINDOWS=true
    fi
    
    # Find all .tf files - cross-platform compatible method
    if [ "$IS_WINDOWS" = true ]; then
        # For Windows using Git Bash
        TF_FILES=$(find . -type f -name "*.tf" -not -path "*/\\.*" | sed 's/\\/\//g')
    else
        # For Linux/Mac
        TF_FILES=$(find . -type f -name "*.tf" -not -path "*/\.*")
    fi
    
    # Check each file individually for better reporting
    for file in $TF_FILES; do
        # Get the directory of the file
        dir=$(dirname "$file")
        filename=$(basename "$file")
        
        # Run terraform fmt check on the specific file - handle both OS formats
        terraform -chdir="$dir" fmt -check "$filename" >/dev/null 2>&1
        
        # If format check fails, record the file
        if [ $? -ne 0 ]; then
            AFFECTED_FILES+=("$file")
            EXIT_CODE=1
        fi
    done
    
    # If any files need formatting, list them and exit with error
    if [ $EXIT_CODE -ne 0 ]; then
        echo "Error: The following Terraform files need formatting:"
        for file in "${AFFECTED_FILES[@]}"; do
            echo " - $file"
        done
        echo ""
        echo "Please run the following command to format these files:"
        echo "terraform fmt -recursive"
        exit 1
    fi
    
    echo "All Terraform files are properly formatted"
    exit 0

    Put this code inside your “.git/hooks/” directory so that it automatically runs when someone does a push. If there is badly formatted Terraform you should see something like:

    Running Terraform format check...
    Error: The following Terraform files need formatting:
      - ./main.tf
    
    Please run the following command to format these files:
    terraform fmt -recursive
    error: failed to push some refs to 'github.com:avansledright/terraform-fmt-pre-hook.git'

    After running the <code>terraform fmt -recursive</code> it should push successfully!

    If this was helpful to your or your team please share it across your social media!

    YouTube video of this script in action