简体   繁体   中英

AWS::Lambda::Function, JS file as source

I'm trying to deploy lambda functions through cloudformation. My project is setup

-src
--lambdas
---lambda1.js
---lambda2.js
---lambda3.js

Then I'm trying to upload each lambda file as a lambda function

Type: AWS::Lambda::Function
    Properties:
      FunctionName: lambda1
      Role: !GetAtt LambdaAtEdgeRole.Arn
      Runtime: nodejs10.x
      Handler: index.handler

Now I'm trying to get the code, but I don't see anywhere where I can upload a JS file. I can see ZIP, but then I would have to add a build step for each file to zip it up. Where can I say lambda_src =./src/lambdas/lambda1.js

You can use aws cli cloudformation package for packaging your lambda code with cloudformation. But you need an S3 bucket beforehand for doing that.

    $ aws cloudformation package \
            --template-file template.yml \
            --output-template-file packaged-template.yml \
            --s3-bucket {s3bucket}

    $ aws cloudformation deploy \
            --template-file packaged-template.yml \
            --stack-name {s3bucket}

Following is the directory structure

    $ tree
    ├── template.yml
    ├── lambda_one.py
    └── lambda_two/
        ├── index.py
        └── some_module.py

and your cloudoformation definition should provide the directory path

   LambdaTwo:
        Type: AWS::Lambda::Function
        Properties:
          Handler: index.lambda_handler
          Code: lambda_two/   #lambda code directory
          Runtime: …
   LambdaOne:
        Type: AWS::Lambda::Function
        Properties:
          Handler: index.lambda_handler
          Code: lambda_one.py    #just one file
          Runtime: …

Post packaing the your tempalte will look like below:

    LambdaOne:
        Properties:
          Code:
              S3Bucket: cf-templates-abcdefghjklm-ap-southeast-2
              S3Key: 35f69109a3a3f87e999f028f034

     ..
   LambdaTwo:
     Properties:
       Code:
         S3Bucket: cf-templates-abcdefghjklm-ap-southeast-2
         S3Key: cca5b023ed6603eabf9421471b65d68b

For the simplicity, you can wrap this whole thing into a Makefile

And last but not the least if you dont wanna do all the plumbing, you can use AWS SAM. You can check this one Tutorial: Deploying a Hello World application to appreciate it.

Here is working example trying to reproduce your setup for aws cloudformation package execution.

directory structure :

.
├── src
│   └── lambdas
│       ├── lambda1.js
│       ├── lambda2.js
│       └── lambda3.js
└── template.yaml

template.yaml

Resources:

  MyFunction1:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: lambda1
      Runtime: nodejs10.x
      Handler: index.handler
      Code: ./src/lambdas/lambda1.js  
  
  MyFunction2:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: lambda1
      Runtime: nodejs10.x
      Handler: index.handler
      Code: ./src/lambdas/lambda2.js  


  MyFunction3:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: lambda1
      Runtime: nodejs10.x
      Handler: index.handler
      Code: ./src/lambdas/lambda3.js 

execute in root folder of the project

aws cloudformation package  --template-file template.yaml --output-template-file output.yml --s3-bucket <your-bucket-name> 

Then you can deploy your output.yml .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM