7

Don't mark this as duplicate due to this SO Answer

I have a "aws_lambda_function" resource and it works fine.

Now I want to deploy another lambda function, I tried copying the entire block with a different handler and alias but it throws an error. Is there any other way to do it.

Thanks in advance.

Update

Here is the terraform code:

resource "aws_lambda_function" "api_service" {
  function_name = "${substr("${local.api_artifact_name}", 0, min(64, length(local.api_artifact_name)))}"

  # Artifacts bucket
  s3_bucket = "${local.artifacts_bucket_name}"
  s3_key    = "${module.artifact-upload.artifact_key}"

  # "index" is the filename within the zip file (main.js) and "handler"
  # is the name of the property under which the handler function was
  # exported in that file.
  handler = "index.api"

  runtime = "nodejs8.10"
  role    = "${module.api-service-iam.iam_role_arn}"

  # Optional, but ensures that things don't constantly refresh during local development
  source_code_hash = "${base64sha256(file("${local.api_dist_dir}"))}"

  environment {
    variables  =  {
      ...
    }
  }
}

Now the resource api_service deploys one Lambda function successfully but how can I go about to deploy, say, 5 such functions?

All these Lambda functions will be invoked by an API Gateway later.

6
  • 4
    What does your Terraform code look like and what is the exact error you get when you run either terraform plan or terraform apply? Commented Nov 9, 2018 at 10:06
  • That is a good question, I will update my post. Commented Nov 10, 2018 at 17:00
  • 1
    that's a good question and I am trying to figure this out too. I have 20 something Lambda functions to deploy. The only way i could think of is using vars as type=map and then list every individual Lambda function config in the map then use count=?? in the tf script and ${count.index} to loop through the map. Haven't tried it though, I am still working on it. Commented Nov 21, 2018 at 1:07
  • @Geet Choubey did you found a way to do it? Commented Dec 11, 2018 at 11:43
  • @dege yes it worked. Commented Dec 12, 2018 at 13:10

2 Answers 2

2

So basically the answer was staring right at my face the whole time.

I copied the entire resource block and made the following changes:

resource "aws_lambda_function" "lambda-1" {
  function_name = "lambda-1-${substr("${local.api_artifact_name}", 0, min(64, length(local.api_artifact_name)))}"

  # Artifacts bucket
  s3_bucket = "${local.artifacts_bucket_name}"
  s3_key    = "${module.artifact-upload.artifact_key}"

  # "index" is the filename within the zip file (main.js) and "handler"
  # is the name of the property under which the handler function was
  # exported in that file.
  handler = "lambda-1/index.api"

  runtime = "nodejs8.10"
  role    = "${module.api-service-iam.iam_role_arn}"

  # Optional, but ensures that things don't constantly refresh during local development
  source_code_hash = "${base64sha256(file("${local.api_dist_dir}"))}"

}

resource "aws_lambda_function" "lambda-2" {
  function_name = "lambda-2-${substr("${local.api_artifact_name}", 0, min(64, length(local.api_artifact_name)))}"

  # Artifacts bucket
  s3_bucket = "${local.artifacts_bucket_name}"
  s3_key    = "${module.artifact-upload.artifact_key}"

  # "index" is the filename within the zip file (main.js) and "handler"
  # is the name of the property under which the handler function was
  # exported in that file.
  handler = "lambda-2/index.api"

  runtime = "nodejs8.10"
  role    = "${module.api-service-iam.iam_role_arn}"

  # Optional, but ensures that things don't constantly refresh during local development
  source_code_hash = "${base64sha256(file("${local.api_dist_dir}"))}"

}

Make sure they have different function names

Sign up to request clarification or add additional context in comments.

Comments

1

I basically create one directory per lambda, with a naming convention for artifacts like policy.json, ssm_parameters.json.

1) I use an external data source to get a list lambda functions in the directory, and get all the meta-data necessary for each Lambda 2) I use count="N" to deploy each lambda resource.

1 Comment

This was the second approach that I was going to follow but since the number of lambdas are less, I am able to deploy it using multiple blocks only. Thanks though.

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.