community
community copied to clipboard
Lambda Function zipFile Error
When trying to use Python code inline via zipFile, I get the following error:
The Function "myfunction" is invalid: spec.code.zipFile: Invalid value: "def lambda_handler(event, context):\n print('hello world')\n": spec.code.zipFile in body must be of type byte: "def lambda_handler(event, context):\n print('hello world')\n"
My Function YAML:
---
apiVersion: lambda.services.k8s.aws/v1alpha1
kind: Function
metadata:
name: myfunction
namespace: devops-automation
spec:
code:
zipFile: |
def lambda_handler(event, context):
print('hello world')
environment:
variables: {}
fileSystemConfigs:
- arn: "arn:aws:elasticfilesystem:us-west-1:*****:access-point/fsap-*****"
localMountPath: "/mnt/efs"
handler: "index.lambda_handler"
memorySize: 256
name: myfunction
publish: false
reservedConcurrentExecutions: 100
role: "arn:aws:iam::*****:role/*****"
runtime: python3.12
tags: {}
timeout: 30
vpcConfig:
securityGroupIDs:
- sg-*****
subnetIDs:
- subnet-*****
- subnet-*****
Issues go stale after 180d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 60d of inactivity and eventually close.
If this issue is safe to close now please do so with /close.
Provide feedback via https://github.com/aws-controllers-k8s/community.
/lifecycle stale
@urton The zipFile field expects a base64 encoding of the zip file containing your code package. Here's an example of deploying a Lambda function with zipFile:
- Write the python code to a file
cat <<EOF > app.py
def lambda_handler(event, context):
return 'hello world'
EOF
- Zip the python file
zip -r function.zip app.py
- Base64 encode the zip file
cat function.zip | base64
We can then the use the resulting base64 encoding in our Lambda function manifest
apiVersion: lambda.services.k8s.aws/v1alpha1
kind: Function
metadata:
name: sample-lambda
annotations:
services.k8s.aws/region: us-west-2
spec:
name: sample-lambda
packageType: Zip
runtime: python3.13
handler: app.lambda_handler
code:
zipFile: UEsDBAoAAAAAAHl/21qeD5FAPQAAAD0AAAAGABwAYXBwLnB5VVQJAANlIl9oZiJfaHV4CwABBPcBAAAEFAAAAGRlZiBsYW1iZGFfaGFuZGxlcihldmVudCwgY29udGV4dCk6CiAgICByZXR1cm4gJ2hlbGxvIHdvcmxkJwpQSwECHgMKAAAAAAB5f9tang+RQD0AAAA9AAAABgAYAAAAAAABAAAApIEAAAAAYXBwLnB5VVQFAANlIl9odXgLAAEE9wEAAAQUAAAAUEsFBgAAAAABAAEATAAAAH0AAAAAAA==
role: "arn:aws:iam::*****:role/*****"
Thanks @knottnt. I can certainly do that, but I guess I was expecting the same behavior as CloudFormation:
ZipFile
(Node.js and Python) The source code of your Lambda function. If you include your function source inline with this parameter, AWS CloudFormation places it in a file named index and zips it to create a [deployment package](https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-package.html). This zip file cannot exceed 4MB. For the Handler property, the first part of the handler identifier must be index. For example, index.handler.
Hi @urton ,
A quick note on using a zipFile. There is a 1.5MB size limit for direct code uploads - Kubernetes API server enforces a maximum request size for a single API object which is not the case for CloudFormation.
For production workloads, we recommend using S3-based deployment.
Thanks @rushmash91 I wasn't planning on doing this in production. I was just testing it out months ago and noticed it didn't work. If this is the expected behavior, that's totally fine.
No Problem! Feel free to reopen the issue if you have any further comments! /close
@rushmash91: Closing this issue.
In response to this:
No Problem! Feel free to reopen the issue if you have any further comments! /close
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.