部署到多个AWS Lambda的解决方法可以使用AWS CloudFormation和AWS Serverless Application Model (SAM)来实现。下面是一个示例代码:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
Function1:
Type: AWS::Serverless::Function
Properties:
FunctionName: myLambdaFunction1
Runtime: python3.8
Handler: lambda_function1.handler
CodeUri: .
Timeout: 10
Function2:
Type: AWS::Serverless::Function
Properties:
FunctionName: myLambdaFunction2
Runtime: python3.8
Handler: lambda_function2.handler
CodeUri: .
Timeout: 10
import json
def handler(event, context):
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda Function 1!')
}
import json
def handler(event, context):
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda Function 2!')
}
aws cloudformation package --template-file template.yaml --output-template-file packaged.yaml --s3-bucket
aws cloudformation deploy --template-file packaged.yaml --stack-name myLambdaStack --capabilities CAPABILITY_IAM
这将使用CloudFormation来创建一个包含两个Lambda函数的堆栈。Lambda函数被打包并部署到AWS Lambda,并且可以通过其各自的函数名称进行调用。