1
votes

the lambda funciton size is over 4096 characters, so I can't deploy lambda function as inline codes in cloudformation template.

(https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html)

ZipFile

Your source code can contain up to 4096 characters. For JSON, you must escape quotes and special characters such as newline (\n) with a backslash.

I have to zip it first, upload to a s3 bucket, set s3 bucket and file details in cloudforamtion, and deploy it.

I can't find a way to deploy with one command. If I update the lambda code, I have to repead above steps

But with both AWS SAM or Serverless Framework, they can deploy lambda functions without inline codes.

The only issue is, aws SAM or serverless framework create API gateway as default, that I don't need it to be created

Any solution or recommends for me?

4
Can you clarify the issue? You don't know how to make the zip and upload it to s3? - Marcin
I can. But there is no way to do all in one - Bill
aws cli provides package command that can make the zip and upload it. - Marcin

4 Answers

4
votes

If you're managing your deployment with plain CloudFormation and the aws command line interface, you can handle this relatively easily using aws cloudformation package to generate a "packaged" template for deployment.

aws cloudformation package accepts a template where certain properties can be written using local paths, zips the content from the local file system, uploads to a designated S3 bucket, and then outputs a new template with these properties rewritten to refer to the location on S3 instead of the local file system. In your case, it can rewrite Code properties for AWS::Lambda::Function that point to local directories, but see aws cloudformation package help for a full list of supported properties. You do need to setup an S3 bucket ahead of time to store your assets, but you can reuse the same bucket in multiple CloudFormation projects.

So, let's say you have an input.yaml with something like:

  MyLambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code: my-function-directory

You might package this up with something like:

aws cloudformation package \
    --template-file input.yaml \
    --s3-bucket my-packaging-bucket \
    --s3-prefix my-project/ \
    --output-template-file output.yaml

Which would produce an output.yaml with something resembling this:

  MyLambdaFunction:
    Properties:
      Code:
        S3Bucket: my-packaging-bucket
        S3Key: my-project/0123456789abcdef0123456789abcdef
    Type: AWS::Lambda::Function

You can then use output.yaml with aws cloudformation deploy (or any other aws cloudformation command accepting a template).

To truly "deploy with one command" and ensure you always do deployments consistently, you can combine these two commands into a script, Makefile, or something similar.

0
votes

you can zip the file first then use aws cli to update your lambda function

zip function.zip lambda_function.py
aws lambda update-function-code --function-name <your-lambda-function-name> --zip-file fileb://function.zip
0
votes

Within CloudFormation (last 3 lines):

  BackupLambda:
    Type: "AWS::Lambda::Function"
    Properties:
      Handler: "backup_lambda.lambda_handler"
      Role: !Ref Role
      Runtime: "python2.7"
      MemorySize: 128
      Timeout: 120
      Code:
        S3Bucket: !Ref BucketWithLambdaFunction
        S3Key: !Ref PathToLambdaFile

0
votes

Re. your comment:

The only issue is, aws SAM or serverless framework create API gateway as default, that I don't need it to be created

For Serverless Framework by default that's not true. The default generated serverless.yml file includes config for the Lambda function itself but the configuration for API Gateway is provided only as an example in the following commented out section.

If you uncomment the 'events' section for http then it will also create an API Gateway config for your Lambda, but not unless you do.

functions:
  hello:
    handler: handler.hello
#    The following are a few example events you can configure
#    NOTE: Please make sure to change your handler code to work with those events
#    Check the event documentation for details
#    events:
#      - http:
#          path: users/create
#          method: get