Reputation: 781
I'm trying to create an AWS Lambda function using the AWS CLI. So I generated the JSON input skeleton for use with the aws lambda create-function
function by passing in the --generate-cli-skeleton
parameter and then substituting the values accordingly.
The problem is that when I execute the command to create the function it gives me this error:
A client error (InvalidParameterValueException) occurred when calling the CreateFunction operation: Could not unzip uploaded file. Please check your file, then try to upload again.
This is how I run the command:
aws lambda create-function --cli-input-json file://C:\Projects\Automated_Deployment\lambda_function_deploy.json
The contents of the .json file are:
{
"FunctionName": "MyFunction",
"Runtime": "nodejs",
"Role": "arn:aws:iam::------------:role/lambda_dynamo",
"Handler": "index.handler",
"Code": {
"ZipFile": "fileb://C:/Projects/src/zip/MyFunction.zip"
},
"Description": "description goes here",
"Timeout": 10,
"MemorySize": 128,
"Publish": true
}
Surprisingly if I try to create the function without using the JSON file and providing all the parameters in the command-line then it works. So for instance, this works without any issues at all:
aws lambda create-function --function-name MyFunction --runtime nodejs --role arn:aws:iam::------------:role/lambda_dynamo --handler index.handler --zip-file fileb://C:/Projects/src/zip/MyFunction.zip
This is the same .zip file. Any hints as to what am I doing wrong here? I have tried replacing "ZipFile": "fileb://C:/Projects/src/zip/MyFunction.zip"
with:
"ZipFile": "file://C:/Projects/src/zip/MyFunction.zip"
and "ZipFile": "C:/Projects/src/zip/MyFunction.zip"
but the issue remains.
EDIT: I found out the --debug option which we can pass to the CLI command. The problem is that the tool is not reading the ZipFile properly when we provide the input from JSON. So for instance if I run the command providing all the parameters in the command itself, I can see the zip file properly base64 encoded i.e.:
2016-02-08 10:43:59,831 - MainThread - botocore.endpoint - DEBUG - Making request for <botocore.model.OperationModel object at 0x0000000004149F28> (verify_ssl=True) with params: {'body': '{"Code": {"ZipFile": "UEsDBBQAAAAIAFiGPUiLOeW/nwcAANsdAAAIAAAAaW5kZXguanO1Gdtu2zb0PUD+gdVDI2OOmrZrBzgICi9OC29FYsRpX4pCUCQ65iJLqk
....
redacted
But when I provide the parameters from the JSON file i.e. --cli-input-json then the command base64 encodes the value of the ZipFile key in the JSON (which is the URI to that file i.e. fileb://C:/Projects/src/zip/MyFunction.zip)
Now I'm not sure what value we need to provide to the ZipFile key, or is it a bug in the tool/command?
EDIT2: If I provide the base64 encoded string of my zip file, the command will base64 encode that string again which will gave the same error (i.e. cannot unzip.) So my question is what does the field ZipFile require? A URL with file:// protocol or fileb:// ? Zip file's contents base-64 encoded?
Upvotes: 3
Views: 7828
Reputation: 1
I got the solution for it. It happened because you are probably giving the wrong extension to your lambda handler file in the filename in lambda function. it should be ../../xyz.zip not ../../xyz.py.
E.g -
data "archive_file" "this" {
type = "zip"
source_file = ../../xyz.py
output_path = ../../xyz.zip
}
resource "aws_lambda_function" "this" {
filename = ../../xyz.zip
}
It would be great if you can add your lambda function code as well to better understand.
Upvotes: 0
Reputation: 1229
The aws cli docs for lambda create-function don't actually list ZipFile
as part of the --code
option.
The Python AWS SDK docs do list ZipFile
- it should be bytes, and be 'The contents of your zip file containing your deployment package'.
In python you can do this by reading the contents of the zip file, but I'm not sure how you would do this in a suitable format to go into a json file - I'm not certain that you can.
I wonder if perhaps the cli docs are accurate, and the output of --generate-cli-skeleton
for this command is not, and that the lambda deployment package must either be stored in s3 to be referenced in the file passed to --cli-input-json
, or passed as a cmd line option, as you ended up doing.
Upvotes: 2
Reputation: 4533
don't know if you has already solved this. One possible solution is to get contents from the zip file previously. In Python you can do something like this:
...
{
"FunctionName": "MyFunction",
"Runtime": "nodejs",
"Role": "arn:aws:iam::------------:role/lambda_dynamo",
"Handler": "index.handler",
"Code": {
"ZipFile": "fileb://" + file_get_contents(C:/Projects/src/zip/MyFunction.zip)
},
"Description": "description goes here",
"Timeout": 10,
"MemorySize": 128,
"Publish": true
}
...
def file_get_contents(filename):
with open(filename) as f:
return f.read()
I guess you can do something similar in nodejs (https://docs.nodejitsu.com/articles/file-system/how-to-read-files-in-nodejs)
Hope it helps!!
Upvotes: 2