Exploring the Power of Automation: Adding Triggers in Lambda Function Using Terraform

Introduction :-

So, you’ve heard of AWS Lambda and Terraform, right? Lambda allows you to run code without provisioning or managing servers — fantastic, isn’t it? Terraform, on the other hand, lets you treat your infrastructure as code, meaning you can automate the setup and management of your AWS resources. Put them together, and you have a powerhouse that can automate tasks, making your life much simpler and your workflow more efficient.

Pre-requisites :-

Before we dive into the nitty-gritty, let’s make sure you’re ready to follow along:

  • AWS Account: You’ll need an account with AWS. Don’t worry, you can use their Free Tier to play around.

  • Basic Knowledge of AWS Lambda and Terraform: Familiarity with the basic concepts of AWS Lambda and Terraform will go a long way.

  • Terraform Installed: Make sure Terraform is installed and ready to go on your machine.

  • An Editor: Any text editor will do, but something like Visual Studio Code can make your life easier with its syntax highlighting.

Ready? Let’s move on to the exciting part — the procedure!

Procedure :-

Now for the meat and potatoes of our blog post. I’ll guide you through setting up a simple Lambda function and then triggering it using Terraform. It’s like teaching your code to respond to a doorbell — every time the doorbell rings, your code wakes up and says, “I’ll handle this!”.

Step 1: Setting Up Terraform

First, create a folder with name lambda_trigger on your desktop.within the folder create terraform configuration files such as main.tf, variables.tf and provider.tf .

#main.tf
resource "aws_lambda_function" "triggers" {
  filename      = var.file_name
  function_name = "lambda-triggers"
  role          = aws_iam_role.lambda_role.arn
  handler       = var.handler
  source_code_hash = filebase64sha256("lambda_function.zip")
  runtime = "python3.9"
  memory_size = 1024
  timeout = 6
}

resource "aws_iam_role" "lambda_role" {
    name                  = "lambda-role"
    assume_role_policy    = jsonencode(
        {
            Statement = [
                {
                    Action    = "sts:AssumeRole"
                    Effect    = "Allow"
                    Principal = {
                        Service = "lambda.amazonaws.com"
                    }
                },
            ]
            Version   = "2012-10-17"
        }
    )
}
#variables.tf
variable "file_name" {
  type = string
  default = "lambda_function.zip"
}

variable "handler" {
  type = string
  default = "lambda_function.lambda_handler"
}

Step 3: Add a Trigger

Let’s add an S3 bucket trigger. Whenever a new object is added to the bucket, our Lambda function will run. Add the following to your main.tf:

resource "aws_s3_bucket" "my_bucket" {
  bucket = "my-unique-bucket-name"
}
resource "aws_lambda_permission" "allow_bucket" {
  statement_id  = "AllowExecutionFromS3Bucket"
  action        = "lambda:InvokeFunction"
  function_name = aws_lambda_function.my_lambda_function.function_name
  principal     = "s3.amazonaws.com"
  source_arn    = aws_s3_bucket.my_bucket.arn
}

Step 4: Apply Your Terraform Configuration

Finally, run terraform init to initialize your Terraform project, then terraform apply to apply your configuration. Terraform will do its magic, creating your Lambda function and setting up the trigger.

Conclusion :-

And there you have it — you’ve automated a task using a Lambda function and Terraform! This is just the tip of the iceberg. Imagine all the things you can automate in your workflow. The key takeaway here is not to be afraid of automation. Embrace it, and you’ll find yourself with more time to focus on what truly matters to you.

0
Subscribe to my newsletter

Read articles from Mahira Technology Private Limited directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mahira Technology Private Limited
Mahira Technology Private Limited

A leading tech consulting firm specializing in innovative solutions. Experts in cloud, DevOps, automation, data analytics & more. Trusted technology partner.