Skip to content

Commit 818b4ef

Browse files
authored
feat: adding support for provisioned concurrency (#1284) (#1285)
1 parent 69822c1 commit 818b4ef

15 files changed

+243
-18
lines changed

examples/2016-10-31/lambda_edge/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ LambdaEdgeFunctionSample:
1717
AutoPublishAlias: live
1818
```
1919
20+
This also uses `ProvisionedConcurrencyConfig` setting on the lambda function Alias created by AutoPublishAlias:
21+
22+
```yaml
23+
LambdaEdgeFunctionSample:
24+
Type: AWS::Serverless::Function
25+
Properties:
26+
CodeUri: src/
27+
Runtime: nodejs6.10
28+
Handler: index.handler
29+
Role: !GetAtt LambdaEdgeFunctionRole.Arn
30+
Timeout: 5
31+
# More info at https://github.com/awslabs/serverless-application-model/blob/master/docs/safe_lambda_deployments.rst
32+
AutoPublishAlias: live
33+
ProvisionedConcurrencyConfig:
34+
ProvisionedConcurrentExecutions: !If [AliasProvisionedConcurrencyEnabled, !Ref ProvisionedConcurrency, !Ref 'AWS::NoValue']
35+
```
36+
2037
We must also create a custom IAM Role which allows `lambda.amazonaws.com` and `edgelambda.amazonaws.com` services to assume the role and execute the function.
2138

2239
```yaml

examples/2016-10-31/lambda_edge/template.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
AWSTemplateFormatVersion: '2010-09-09'
22
Transform: AWS::Serverless-2016-10-31
33
Description: Sample SAM configuration for Lambda@Edge to ease deployment and further updates
4+
Parameters:
5+
6+
ProvisionedConcurrency:
7+
Type: String
8+
Default: 10
9+
EnableAliasProvisionedConcurrency:
10+
Type: String
11+
AllowedValues:
12+
- true
13+
- false
14+
Default: true
15+
16+
Globals:
17+
18+
Function:
19+
ProvisionedConcurrencyConfig:
20+
ProvisionedConcurrentExecutions: !If [AliasProvisionedConcurrencyEnabled, !Ref ProvisionedConcurrency, !Ref 'AWS::NoValue']
21+
422
Resources:
523

624
CFDistribution:

samtranslator/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.17.0'
1+
__version__ = '1.18.0'

samtranslator/model/lambda_.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class LambdaAlias(Resource):
5050
'Description': PropertyType(False, is_str()),
5151
'Name': PropertyType(False, is_str()),
5252
'FunctionName': PropertyType(True, one_of(is_str(), is_type(dict))),
53-
'FunctionVersion': PropertyType(True, one_of(is_str(), is_type(dict)))
53+
'FunctionVersion': PropertyType(True, one_of(is_str(), is_type(dict))),
54+
'ProvisionedConcurrencyConfig': PropertyType(False, is_type(dict))
5455
}
5556

5657
runtime_attrs = {

samtranslator/model/sam_resources.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class SamFunction(SamResourceMacro):
5555

5656
# Intrinsic functions in value of Alias property are not supported, yet
5757
'AutoPublishAlias': PropertyType(False, one_of(is_str())),
58-
'VersionDescription': PropertyType(False, is_str())
58+
'VersionDescription': PropertyType(False, is_str()),
59+
'ProvisionedConcurrencyConfig': PropertyType(False, is_type(dict)),
5960
}
6061
event_resolver = ResourceTypeResolver(samtranslator.model.eventsources, samtranslator.model.eventsources.pull,
6162
samtranslator.model.eventsources.push,
@@ -96,6 +97,11 @@ def to_cloudformation(self, **kwargs):
9697
lambda_function = self._construct_lambda_function()
9798
resources.append(lambda_function)
9899

100+
if self.ProvisionedConcurrencyConfig:
101+
if not self.AutoPublishAlias:
102+
raise InvalidResourceException(self.logical_id, "To set ProvisionedConcurrencyConfig "
103+
"AutoPublishALias must be defined on the function")
104+
99105
lambda_alias = None
100106
if self.AutoPublishAlias:
101107
alias_name = self._get_resolved_alias_name("AutoPublishAlias", self.AutoPublishAlias, intrinsics_resolver)
@@ -415,6 +421,8 @@ def _construct_alias(self, name, function, version):
415421
alias.Name = name
416422
alias.FunctionName = function.get_runtime_attr('name')
417423
alias.FunctionVersion = version.get_runtime_attr("version")
424+
if self.ProvisionedConcurrencyConfig:
425+
alias.ProvisionedConcurrencyConfig = self.ProvisionedConcurrencyConfig
418426

419427
return alias
420428

samtranslator/plugins/globals/globals.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class Globals(object):
3636
"Layers",
3737
"DeploymentPreference",
3838
"PermissionsBoundary",
39-
"ReservedConcurrentExecutions"
39+
"ReservedConcurrentExecutions",
40+
"ProvisionedConcurrencyConfig"
4041
],
4142

4243
# Everything except
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Parameters:
2+
FnName:
3+
Type: String
4+
ProvisionedConcurrency:
5+
Type: String
6+
Default: 10
7+
EnableAliasProvisionedConcurrency:
8+
Type: String
9+
AllowedValues:
10+
- true
11+
- false
12+
Default: true
13+
Conditions:
14+
AliasProvisionedConcurrencyEnabled: !Equals [!Ref EnableAliasProvisionedConcurrency, true]
15+
Resources:
16+
MinimalFunction:
17+
Type: 'AWS::Serverless::Function'
18+
Properties:
19+
CodeUri: s3://sam-demo-bucket/hello.zip
20+
Handler: hello.handler
21+
Runtime: python2.7
22+
DeploymentPreference:
23+
Type: Linear10PercentEvery3Minutes
24+
ProvisionedConcurrencyConfig:
25+
ProvisionedConcurrentExecutions: !If [AliasProvisionedConcurrencyEnabled, ProvisionedConcurrentExecutions: !Ref ProvisionedConcurrency, !Ref 'AWS::NoValue']

tests/translator/input/function_with_deployment_preference.yaml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
Parameters:
2+
FnName:
3+
Type: String
4+
ProvisionedConcurrency:
5+
Type: String
6+
Default: 10
7+
EnableAliasProvisionedConcurrency:
8+
Type: String
9+
AllowedValues:
10+
- true
11+
- false
12+
Default: true
13+
Conditions:
14+
AliasProvisionedConcurrencyEnabled: !Equals [!Ref EnableAliasProvisionedConcurrency, true]
115
Resources:
216
MinimalFunction:
317
Type: 'AWS::Serverless::Function'
@@ -7,4 +21,6 @@ Resources:
721
Runtime: python2.7
822
AutoPublishAlias: live
923
DeploymentPreference:
10-
Type: Linear10PercentEvery3Minutes
24+
Type: Linear10PercentEvery3Minutes
25+
ProvisionedConcurrencyConfig:
26+
ProvisionedConcurrentExecutions: !If [AliasProvisionedConcurrencyEnabled, !Ref ProvisionedConcurrency, !Ref 'AWS::NoValue']

tests/translator/output/aws-cn/function_with_deployment_preference.json

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
11
{
2+
"Conditions": {
3+
"AliasProvisionedConcurrencyEnabled": {
4+
"Fn::Equals": [
5+
{
6+
"Ref": "EnableAliasProvisionedConcurrency"
7+
},
8+
true
9+
]
10+
}
11+
},
12+
"Parameters": {
13+
"EnableAliasProvisionedConcurrency": {
14+
"Default": true,
15+
"Type": "String",
16+
"AllowedValues": [
17+
true,
18+
false
19+
]
20+
},
21+
"FnName": {
22+
"Type": "String"
23+
},
24+
"ProvisionedConcurrency": {
25+
"Default": 10,
26+
"Type": "String"
27+
}
28+
},
229
"Resources": {
330
"MinimalFunctionDeploymentGroup": {
431
"Type": "AWS::CodeDeploy::DeploymentGroup",
@@ -61,11 +88,11 @@
6188
"MinimalFunction": {
6289
"Type": "AWS::Lambda::Function",
6390
"Properties": {
91+
"Handler": "hello.handler",
6492
"Code": {
6593
"S3Bucket": "sam-demo-bucket",
6694
"S3Key": "hello.zip"
6795
},
68-
"Handler": "hello.handler",
6996
"Role": {
7097
"Fn::GetAtt": [
7198
"MinimalFunctionRole",
@@ -133,17 +160,30 @@
133160
}
134161
},
135162
"Properties": {
163+
"Name": "live",
136164
"FunctionVersion": {
137165
"Fn::GetAtt": [
138166
"MinimalFunctionVersion640128d35d",
139167
"Version"
140168
]
141169
},
170+
"ProvisionedConcurrencyConfig": {
171+
"ProvisionedConcurrentExecutions": {
172+
"Fn::If": [
173+
"AliasProvisionedConcurrencyEnabled",
174+
{
175+
"Ref": "ProvisionedConcurrency"
176+
},
177+
{
178+
"Ref": "AWS::NoValue"
179+
}
180+
]
181+
}
182+
},
142183
"FunctionName": {
143184
"Ref": "MinimalFunction"
144-
},
145-
"Name": "live"
185+
}
146186
}
147187
}
148188
}
149-
}
189+
}

tests/translator/output/aws-us-gov/function_with_deployment_preference.json

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
11
{
2+
"Conditions": {
3+
"AliasProvisionedConcurrencyEnabled": {
4+
"Fn::Equals": [
5+
{
6+
"Ref": "EnableAliasProvisionedConcurrency"
7+
},
8+
true
9+
]
10+
}
11+
},
12+
"Parameters": {
13+
"EnableAliasProvisionedConcurrency": {
14+
"Default": true,
15+
"Type": "String",
16+
"AllowedValues": [
17+
true,
18+
false
19+
]
20+
},
21+
"FnName": {
22+
"Type": "String"
23+
},
24+
"ProvisionedConcurrency": {
25+
"Default": 10,
26+
"Type": "String"
27+
}
28+
},
229
"Resources": {
330
"MinimalFunctionDeploymentGroup": {
431
"Type": "AWS::CodeDeploy::DeploymentGroup",
@@ -61,11 +88,11 @@
6188
"MinimalFunction": {
6289
"Type": "AWS::Lambda::Function",
6390
"Properties": {
91+
"Handler": "hello.handler",
6492
"Code": {
6593
"S3Bucket": "sam-demo-bucket",
6694
"S3Key": "hello.zip"
6795
},
68-
"Handler": "hello.handler",
6996
"Role": {
7097
"Fn::GetAtt": [
7198
"MinimalFunctionRole",
@@ -133,16 +160,29 @@
133160
}
134161
},
135162
"Properties": {
163+
"Name": "live",
136164
"FunctionVersion": {
137165
"Fn::GetAtt": [
138166
"MinimalFunctionVersion640128d35d",
139167
"Version"
140168
]
141169
},
170+
"ProvisionedConcurrencyConfig": {
171+
"ProvisionedConcurrentExecutions": {
172+
"Fn::If": [
173+
"AliasProvisionedConcurrencyEnabled",
174+
{
175+
"Ref": "ProvisionedConcurrency"
176+
},
177+
{
178+
"Ref": "AWS::NoValue"
179+
}
180+
]
181+
}
182+
},
142183
"FunctionName": {
143184
"Ref": "MinimalFunction"
144-
},
145-
"Name": "live"
185+
}
146186
}
147187
}
148188
}

0 commit comments

Comments
 (0)