Terraform-provider-aws: r/aws_lambda_function: support inline code

Created on 15 Aug 2019  ·  2Comments  ·  Source: hashicorp/terraform-provider-aws

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

With CloudFormation it's possible to define a Code block with a ZipFile property which contains your lambda function code. CloudFormation packages it up for you and deploys the lambda function.

It would be great if this feature could be supported by terraform as well.

New or Affected Resource(s)

  • aws_lambda_function

Potential Terraform Configuration

resource "aws_lambda_function" "test_lambda" {
  code          = <<EOF
exports.handler = async () => console.log(`foo: ${process.env.foo}`)
EOF
  function_name = "lambda_function_name"
  role          = "${aws_iam_role.iam_for_lambda.arn}"
  handler       = "index.handler"
  runtime       = "nodejs8.10"
  environment {
    variables = {
      foo = "bar"
    }
  }
}

References

  • https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-code

  • enhancement needs-triage serviclambda

    Most helpful comment

    Unless I misunderstand what you're asking for, you can already do this with TF:

    data "archive_file" "part_sync_initial_lambda_artifact" {
      type        = "zip"
      output_path = "${path.root}/.archive_files/part_sync.zip"
    
      # fingerprinter
      source {
        filename = "index.js"
        content  = <<CODE
    exports.snsChangeDataHandler = async (event) => { console.log('test!'); };
    CODE
      }
    }
    
    # setup the actual lambda resource
    resource "aws_lambda_function" "part_sync" {
      lifecycle {
        # make sure tf doesn't overwrite deployed code once we start deploying
        ignore_changes = [
          filename,
          source_code_hash,
          last_modified,
        ]
      }
    
      function_name = "part-sync-${local.lowered_env}"
      role          = aws_iam_role.sync_lambda_role.arn
      handler       = "index.snsChangeDataHandler"
      runtime       = "nodejs12.x"
      timeout       = 60
      memory_size   = 256
    
      filename         = "${path.root}/.archive_files/part_sync.zip"
      source_code_hash = data.archive_file.part_sync_initial_lambda_artifact.output_base64sha256
    
      ...
    }
    

    I always do this with my node and python lambdas.

    All 2 comments

    This would be a good gap to close with CDK as well — currently there's no direct counterpart to this:

    const lambdaFn = new lambda.Function(this, "Singleton", {
        functionName: "lambda_function_name",
        runtime: …,
        code: new lambda.InlineCode(
            fs.readFileSync("my_source_file", {
                encoding: "utf-8"
            })
        ),
        …
    });
    

    Unless I misunderstand what you're asking for, you can already do this with TF:

    data "archive_file" "part_sync_initial_lambda_artifact" {
      type        = "zip"
      output_path = "${path.root}/.archive_files/part_sync.zip"
    
      # fingerprinter
      source {
        filename = "index.js"
        content  = <<CODE
    exports.snsChangeDataHandler = async (event) => { console.log('test!'); };
    CODE
      }
    }
    
    # setup the actual lambda resource
    resource "aws_lambda_function" "part_sync" {
      lifecycle {
        # make sure tf doesn't overwrite deployed code once we start deploying
        ignore_changes = [
          filename,
          source_code_hash,
          last_modified,
        ]
      }
    
      function_name = "part-sync-${local.lowered_env}"
      role          = aws_iam_role.sync_lambda_role.arn
      handler       = "index.snsChangeDataHandler"
      runtime       = "nodejs12.x"
      timeout       = 60
      memory_size   = 256
    
      filename         = "${path.root}/.archive_files/part_sync.zip"
      source_code_hash = data.archive_file.part_sync_initial_lambda_artifact.output_base64sha256
    
      ...
    }
    

    I always do this with my node and python lambdas.

    Was this page helpful?
    0 / 5 - 0 ratings