How do you deploy cloudformation with a lambda function without inline code?
Asked Answered
W

4

5

the lambda function 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 cloudformation, and deploy it.

I can't find a way to deploy with one command. If I update the lambda code, I have to repeat the 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 recommendations for me?

Updates

SAM does support to deploy lambda functions without creating API gateway. So problem is fixed

Wershba answered 11/12, 2020 at 4:43 Comment(3)
Can you clarify the issue? You don't know how to make the zip and upload it to s3?Dreadfully
I can. But there is no way to do all in oneWershba
aws cli provides package command that can make the zip and upload it.Dreadfully
G
8

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.

Gibrian answered 12/12, 2020 at 18:1 Comment(1)
Yes, but you have to have a bucket first. And you can't define that bucket in the template file as you have to create it before the package command and then deploy the output yaml.Renowned
N
1

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
Nosh answered 11/12, 2020 at 8:58 Comment(1)
Thanks, I am working on cloudformation with a lambda function in it, not lambda directly.Wershba
S
0

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

Spoilt answered 11/12, 2020 at 13:18 Comment(1)
Yes, that's the problem, right? So everytime, when I update the lambda codes, I have manually generate the zip files, upload to s3 bucket , then run the deploy command. I can't do it in one command as SLS or SAM cli.Wershba
P
0

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
Pilot answered 10/7, 2021 at 0:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.