Reputation: 2464
I want to limit number of simultaneously running lambdas through cloudformation configuration file. I tried to search for it, but had no luck. On the documentation page there is not information about it. There are to approaches to set this limit: by console or by API. But how can I do it automatically on stack deploying?
Upvotes: 10
Views: 11501
Reputation: 1750
You can now set Per Function Concurrency using
ReservedConcurrentExecutions
This property allows you to set a concurrency limit to each of your Lambda functions.
Documentation for this property.
Upvotes: 8
Reputation: 2464
Following by @DrEigelb's advice I created 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
Upvotes: 0
Reputation: 588
I am guessing that, since this feature is relatively new (and there is no clue in the docs) there is no way to do it out-of-the-box in a cloudformation template. Your best bet if you want to use CF is a Custom Resource, where you set the concurrency via a lambda using e.g. boto3's put_function_concurrency
method.
Some resources on Custom Reources: - 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
Upvotes: 2