community icon indicating copy to clipboard operation
community copied to clipboard

Lambda Function zipFile Error

Open urton opened this issue 11 months ago • 1 comments

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-*****

urton avatar Dec 02 '24 20:12 urton

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

ack-bot avatar Jun 16 '25 20:06 ack-bot

@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:

  1. Write the python code to a file
cat <<EOF > app.py
def lambda_handler(event, context):
    return 'hello world'
EOF
  1. Zip the python file
zip -r function.zip app.py
  1. 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/*****"

knottnt avatar Jun 27 '25 23:06 knottnt

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.

urton avatar Jul 16 '25 23:07 urton

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.

rushmash91 avatar Jul 17 '25 01:07 rushmash91

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.

urton avatar Jul 17 '25 22:07 urton

No Problem! Feel free to reopen the issue if you have any further comments! /close

rushmash91 avatar Jul 21 '25 17:07 rushmash91

@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.

ack-prow[bot] avatar Jul 21 '25 17:07 ack-prow[bot]