Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Configure CloudWatch Lambda Insights in Terraform

I need to enable "CloudWatch Lambda Insights" for a lambda using Terraform, but could not find the documentation. How I can do it in Terraform?

Note: This question How to add CloudWatch Lambda Insights to serverless config? may be relevant.

like image 204
onkami Avatar asked Jan 15 '21 12:01

onkami


Video Answer


1 Answers

There is no "boolean switch" in the aws_lambda_function resource of the AWS Terraform provider that you can set to true, that would enable Cloudwatch Lambda Insights.

Fortunately, it is possible to do this yourself. The following Terraform definitions are based on this AWS documentation: Using the AWS CLI to enable Lambda Insights on an existing Lambda function

The process involves two steps:

  1. Add a layer to your Lambda
  2. Attach a AWS policy to your Lambdas role.

The Terraform definitions would look like this:

resource "aws_lambda_function" "insights_example" {
  [...]

  layers = [
    "arn:aws:lambda:us-east-1:580247275435:layer:LambdaInsightsExtension:14"
  ]
}

resource "aws_iam_role_policy_attachment" "insights_policy" {
  role       = aws_iam_role.insights_example.id
  policy_arn = "arn:aws:iam::aws:policy/CloudWatchLambdaInsightsExecutionRolePolicy"
}

Important: The arn of the layer is different for each region. The documentation I linked above has a link to a list of them. Furthermore, there is an additional step required if your Lambda is in a VPC, which you can read about in the documentation. The described "VPC step" can be put into Terraform as well.


For future readers: The version of that layer in my example is 14. This will change over time. So please do not just copy & paste that part. Follow the provided links and look for the current version of that layer.


Minimal, Complete, and Verifiable example

Tested with:

Terraform v0.14.4
+ provider registry.terraform.io/hashicorp/archive v2.0.0
+ provider registry.terraform.io/hashicorp/aws v3.24.0

Create the following two files (handler.py and main.tf) in a folder. Then run the following commands:

  1. terraform init
  2. terraform plan
  3. terraform apply

Besides deploying the required resources, it will also create a zip archive containing the handler.py which is the deployment artifact used by the aws_lambda_function resource. So this is an all-in-one example without the need of further zipping etc.

handler.py

def lambda_handler(event, context):
    return { 
        'message' : 'CloudWatch Lambda Insights Example'
    }

main.tf

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_lambda_function" "insights_example" {
  function_name = "insights-example"
  runtime       = "python3.8"
  handler       = "handler.lambda_handler"
  role          = aws_iam_role.insights_example.arn
  filename      = "${path.module}/lambda.zip"

  layers = [
    "arn:aws:lambda:us-east-1:580247275435:layer:LambdaInsightsExtension:14"
  ]

  depends_on = [
    data.archive_file.insights_example
  ]
}

resource "aws_iam_role" "insights_example" {
  name               = "InsightsExampleLambdaRole"
  assume_role_policy = data.aws_iam_policy_document.lambda_assume.json
}

resource "aws_iam_role_policy_attachment" "insights_example" {
  role       = aws_iam_role.insights_example.id
  policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}

resource "aws_iam_role_policy_attachment" "insights_policy" {
  role       = aws_iam_role.insights_example.id
  policy_arn = "arn:aws:iam::aws:policy/CloudWatchLambdaInsightsExecutionRolePolicy"
}

data "aws_iam_policy_document" "lambda_assume" {
  statement {
    effect  = "Allow"
    actions = ["sts:AssumeRole"]

    principals {
      type        = "Service"
      identifiers = ["lambda.amazonaws.com"]
    }
  }
}

data "archive_file" "insights_example" {
  type        = "zip"
  source_file = "${path.module}/handler.py"
  output_path = "${path.module}/lambda.zip"
}
like image 173
Jens Avatar answered Sep 28 '22 19:09

Jens