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.
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"
}
}
}
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-code
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.
Most helpful comment
Unless I misunderstand what you're asking for, you can already do this with TF:
I always do this with my node and python lambdas.