Dimi
Dimi

Reputation: 379

Simple AWS Lambda Terraform plan fails with ValidationException Error 400

I am trying to apply the belowmentioned terraform plan, but it fails with a validation error.

I wrote a python function that exists in the dir lambda_function/sns_to_slack.py, I am zipping it through terraform and uploading it to AWS.

I get this error:

error creating Lambda Function (1): ValidationException: status code: 400

File: aws_lambda.tf

data "archive_file" "lambda_zip" {
    type          = "zip"
    source_file   = "lambda_functions/sns_to_slack.py"
    output_path   = "lambda_functions/zips/sns_to_slack.zip"
}

resource "aws_lambda_function" "sns_to_slack" {
  filename         = "lambda_functions/zips/sns_to_slack.zip"
  function_name    = "sns_to_slack"
  role             = aws_iam_role.iam_for_lambda_tf.arn
  source_code_hash = data.archive_file.lambda_zip.output_base64sha256
  runtime          = "python3.8"
}

resource "aws_iam_role" "iam_for_lambda_tf" {
  name = "iam_for_lambda_tf"
  assume_role_policy = <<POLICY
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": "sts:AssumeRole",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Effect": "Allow",
      "Sid": ""
    }
  ]
}
POLICY
}

Any ideas what is up?

Upvotes: 1

Views: 1784

Answers (1)

Marcin
Marcin

Reputation: 238837

You haven't specififed your handler as you use non-default file name (sns_to_slack.py):

resource "aws_lambda_function" "sns_to_slack" {
  filename         = "lambda_functions/zips/sns_to_slack.zip"
  function_name    = "sns_to_slack"
  
  handler          = "sns_to_slack.lambda_handler"
  
  role             = aws_iam_role.iam_for_lambda_tf.arn
  source_code_hash = data.archive_file.lambda_zip.output_base64sha256
  runtime          = "python3.8"
}

lambda_handler in "sns_to_slack.lambda_handler" must be changed to what you are actually using.

Upvotes: 2

Related Questions