How to set Lambda concurrency limit in CloudFormation template

I want to limit the number of concurrently running lambdas through the cloud information configuration file. I tried to find him, but no luck. There is no information on this on the documentation page . There are approaches to setting this limit: console or API. But how can I do this automatically when I deploy the stack?

+4
source share
3 answers

Now you can install Per Function Concurrency with

ReservedConcurrentExecutions

This property allows you to set a Concurrency limit for each of your lambda functions.

The documentation for this property.

+1
source

@DrEigelb, , :

# 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
0
source

Source: https://habr.com/ru/post/1690713/


All Articles