Geet Choubey
Geet Choubey

Reputation: 1077

Deploy multiple lambda functions using terraform

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.

Upvotes: 7

Views: 9277

Answers (2)

Geet Choubey
Geet Choubey

Reputation: 1077

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

Upvotes: 2

victor m
victor m

Reputation: 2175

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.

Upvotes: 1

Related Questions