-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to zip AWS Lambda function on the fly #8344
Comments
Closed if favour to GH-8144 |
For anyone else who finds this issue, I found a good solution to this, using the data "archive_file" "lambda_zip" {
type = "zip"
source_dir = "source"
output_path = "lambda.zip"
}
resource "aws_lambda_function" "my_lambda" {
filename = "lambda.zip"
source_code_hash = "${data.archive_file.lambda_zip.output_base64sha256}"
function_name = "my_lambda"
role = "${aws_iam_role.lambda.arn}"
description = "Some AWS lambda"
handler = "index.handler"
runtime = "nodejs4.3"
} This will zip up the Edit: I wanted to add one more note here. I thought about this, and realized terraform is not necessarily the ideal way to do this. Essentially, this is deploying code, and most of the time, you wouldn't want to do that via a configuration management system like terraform. So, for my project, I've decided to deploy the lambda using apex, and then reference it in terraform using a static ARN. |
D'oh! There's an Edit : why doesn't the archive_file task have output_path as an exported attribute? How do you establish dependency when using the outputted file? Edit : ah, ok, with output_sha |
Thanks @dkniffin This should be added into the official document https://www.terraform.io/docs/providers/aws/r/lambda_function.html |
Few notes on this (for the benefit of who's trying to achieve this):
I don't know of any way of running the |
Some other ideas here:
This prevents unnecessary deployments unless hash of the sources has changed, only works for simple configurations unless you add a couple more steps. 🤷♂️ |
@pecigonzalo whats this use for?
|
Just tracking the hashes. |
thats nearly what i have in mind...works but its a bit ugly:
|
Hi all, Thanks for sharing approaches with When AWS Lambda was first released, there was no means for varying settings between multiple deployments of the same function code (e.g. between staging and production environments) and so a common pattern was to construct the necessary zip file "just in time" in Terraform so that per-environment settings could be embedded in the generated zip file. With the addition of environment variables we now recommend adopting a more conventional strategy of building a single, environment-agnostic artifact zip file and uploading it to S3 as a separate step prior to running Terraform, and then use Terraform only to update the function to use the newly-created artifact. This is analogous to building immutable AMIs using A common pattern I have seen is for the build process (ideally running in a CI system) to produce an S3 object within a well-known artifact bucket using a systematic naming convention for each build:
Then pass a version number into the Terraform configuration somehow (e.g. via an input variable, via Consul, etc) and have the resource "aws_lambda_function" "api" {
function_name = "MyApplicationAPI"
s3_bucket = "mycompany-build-artifacts"
s3_key = "my-application/${var.app_version}/api-function.zip"
role = "${aws_iam_role.lambda.arn}"
handler = "main.handler"
runtime = "python2.7"
timeout = 120
environment {
variables = {
# For example, pass the ARN of a per-environment SNS topic created
# elsewhere in this config.
SNS_TOPIC_ARN = "${aws_sns_topic.example.arn}"
}
}
} By using a distinct S3 object path for each new build and treating existing artifacts as immutable, we avoid the need to track sha256 hashes of the builds: This gives the usual benefits of immutable build artifacts:
I'm glad that some of you have managed to achieve your goals with Terraform, but please note that Terraform is not intended to be a build tool and so this sort of use-case will always be a little clunky and limiting when implemented in Terraform. For most common situations I would strongly recommend following the conventional "build once, deploy many times" best-practice for Lambda code artifacts, and use Terraform only for deployment. |
Using Lambda@Edge support for environment variables can't come soon enough. |
I just realised the content in
For mac user, you may need create below file under folder
Refer: https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html |
This solution unfortunately results in a chicken-egg issue where I cannot create a bucket in the same terraform configuration that I create my lambda in since the bucket wouldn't yet have the lambda file in it. Choosing to upload the zip file directly from terraform lets me solve for this problem by no longer needing to rely on a bucket already existing outside of my current terraform apply context. |
Great it worked perfectly also with go:
|
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
Hi there,
We're using small AWS Lambda functions to perform routine operations on our AWS infrastructure. We have separate
aws_lambda
module which contains configuration & source code of all our functions.Currently the only way to upload new version of Lambda to AWS is to run some "packer" script before terraform launch to update zip file with function. I would like to do this without any pre-terraform scripts to keep TF usage simple.
Suggested steps
result
to thelocal-exec
provisioner so it can zip lambda source code and calc value forsource_code_hash
property ofaws_lambda_function
null_resource
on folder change. Right now it's possible to track changes only in specific files.Resulted TF config
Terraform Version
0.7.0
Affected Resource(s)
Please list the resources as a list, for example:
aws_lambda_function
null_resource
local-exec
Important Factoids
Also addition of
result
variable tolocal-exec
provider will add a huge amount of new ways to use terraformReferences
The text was updated successfully, but these errors were encountered: