5

I am attempting a project in Udacity's AWS Cloud Architect Nanodegree program. And am writing the code to deploy an AWS Lambda Function using Terraform. I believe I am supposed to use greet_lambda.py as the lambda function to be deployed through terraform. File is mentioned below and so far have executed the below steps:

greet_lambda.py

import os
def lambda_handler(event, context):
    return "{} from Lambda!".format(os.environ['greeting'])

Zipped the lambda file:

$zip ../greet_lambda.zip greet_lambda.py

Created a bucket in S3:

$aws s3api create-bucket --bucket=dirai-terraform-lambda --region=us-east-1

Uploaded the zip file into S3:

$aws s3 cp greet_lambda.zip s3://dirai-terraform-lambda/v1.0.0/greet_lambda.zip

main.tf file:

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
    }
  }
}

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


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

resource "aws_lambda_function" "test_lambda" {
   function_name = "greet_lambda"
   s3_bucket = "dirai-terraform-lambda"
   s3_key    = "v1.0.0/greet_lambda.zip"
   handler = "greet_lambda.lambda_handler"
   runtime = "python3.7"
   role = "aws_iam_role.lambda_role.arn" 
}

And I get the below error on $terraform apply:

Error: error creating Lambda Function (1): ValidationException: 
    status code: 400, request id: e6289eb7-40f5-4cf2-ba0a-e8b5ae656466

  on main.tf line 33, in resource "aws_lambda_function" "test_lambda":
  33: resource "aws_lambda_function" "test_lambda" {

Please help me here what I could be doing wrong. The lambda is successfully exported into S3. But the terraform apply is failing.

2 Answers 2

9

Your role will be literal string "aws_iam_role.greet_lambda.arn". It should be (no quotes):

role = aws_iam_role.greet_lambda.arn 
Sign up to request clarification or add additional context in comments.

2 Comments

Based on subsequent question edit, should be: aws_iam_role.lambda_role.arn
@BrentBradburn Yes, but not in quotes.
0

Yes role should be correct but in my case, I added lengthy lambda description and was causing issue. It should be not big.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.