首页 文章

如何在CloudFormation模板中设置Lambda并发限制

提问于
浏览
5

我想通过cloudformation配置文件限制同时运行lambdas的数量 . 我试图搜索它,但没有运气 . 在documentation page上没有关于它的信息 . 有设置此限制的方法:通过控制台或API . 但是如何在堆栈部署中自动执行此操作?

3 回答

  • 1

    您现在可以使用设置Per Function Concurrency

    ReservedConcurrentExecutions
    

    此属性允许您为每个Lambda函数设置并发限制 .

    Documentation for this property.

  • 0

    我猜测,因为这个功能相对较新(并且文档中没有任何线索),所以无法在 Cloud 形态模板中进行开箱即用 . 如果你想使用CF,你最好的选择是Custom Resource,你可以使用例如lambda来设置并发性 . boto3的 put_function_concurrency 方法 .

    自定义资源的一些资源: - http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cfn-customresource.html - http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/walkthrough-custom-resources-lambda-lookup-amiids.html - http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html

  • 2

    根据@ DrEigelb的建议,我创建了custom resource which does that

    # This stack contains basic components for custom resource which allows you to
    # configure lambda concurrency limit during stack creation. It contains lambda
    # function and a role for the lambda. To start you should deploy this stack to
    # the region and the account where you want to use it, then add custom resource
    # with two parameters (`LambdaArn` and `ReservedConcurrentExecutions`) into you
    # stack:
    #
    # LambdaConfigurator:
    #   Type: Custom::LambdaConfigurator
    #   Properties:
    #     ServiceToken: !ImportValue Custom--LambdaConfiguratorFunction--Arn
    #     Region: !Ref "AWS::Region"
    #     LambdaArn: !GetAtt TargetLambda.Arn
    #     ReservedConcurrentExecutions: 10
    #
    Description: Holds custom resource for changing configuration of lambda
    AWSTemplateFormatVersion: '2010-09-09'
    Resources:
      LambdaConfiguratorFunction:
        Type: AWS::Lambda::Function
        Properties:
          Code:
            ZipFile: |
              import re
              import boto3
              import cfnresponse
              def handler(event, context):
                  try:
                      if event['RequestType'] == 'Delete':
                          cfnresponse.send(event, context, cfnresponse.SUCCESS, {})
                          return
                      function_name = event['ResourceProperties']['LambdaArn']
                      concurrency = int(event['ResourceProperties']['ReservedConcurrentExecutions'])
                      print('FunctionName: {}, ReservedConcurrentExecutions: {}'.format(function_name, concurrency))
                      client = boto3.client('lambda')
                      client.put_function_concurrency(FunctionName=function_name, ReservedConcurrentExecutions=concurrency)
                      cfnresponse.send(event, context, cfnresponse.SUCCESS, {})
                  except Exception as e:
                      err = '{}: {}'.format(e.__class__.__name__, str(e))
                      print(err)
                      cfnresponse.send(event, context, cfnresponse.FAILED, {'Reason': err})
          Handler: index.handler
          Runtime: python3.6
          Timeout: 30
          Role:
            Fn::GetAtt: LambdaConfiguratorLambdaExecutionRole.Arn
      LambdaConfiguratorLambdaExecutionRole:
        Type: AWS::IAM::Role
        Properties:
          AssumeRolePolicyDocument:
            Version: '2012-10-17'
            Statement:
            - Effect: Allow
              Principal:
                Service:
                - lambda.amazonaws.com
              Action:
              - sts:AssumeRole
          Path: "/"
          Policies:
          - PolicyName: root
            PolicyDocument:
              Version: '2012-10-17'
              Statement:
              - Effect: Allow
                Action:
                - logs:CreateLogGroup
                - logs:CreateLogStream
                - logs:PutLogEvents
                Resource: arn:aws:logs:*:*:*
              - Effect: Allow
                Action:
                - lambda:*
                Resource: "*"
    Outputs:
      LambdaConfiguratorFunctionArnOutput:
        Value: !GetAtt LambdaConfiguratorFunction.Arn
        Export:
          Name: Custom--LambdaConfiguratorFunction--Arn
    

相关问题