Skip to content

Instantly share code, notes, and snippets.

@danilop
Last active February 29, 2024 12:50
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save danilop/75561c2660275fc328a68741f6d01066 to your computer and use it in GitHub Desktop.
Save danilop/75561c2660275fc328a68741f6d01066 to your computer and use it in GitHub Desktop.
Sample AWS SAM Template using Provisioned Concurrency with Application Auto Scaling
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Sample SAM Template using Application Auto Scaling + Provisioned Concurrency
Globals:
Function:
Timeout: 30
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: HelloWorldFunction
Handler: helloworld.App::handleRequest
Runtime: java11
MemorySize: 1024
AutoPublishAlias: live
DeploymentPreference:
Type: AllAtOnce # Or Canary10Percent5Minutes, Linear10PercentEvery1Minute, ...
ProvisionedConcurrencyConfig:
ProvisionedConcurrentExecutions: 1
Environment:
Variables:
ENVIRONMENT: test
Events:
HelloWorld:
Type: HttpApi # Using the new HTTP API here
MyScalableTarget:
Type: AWS::ApplicationAutoScaling::ScalableTarget
Properties:
MaxCapacity: 100
MinCapacity: 1
ResourceId: !Sub function:${MyFunction}:live # You need to specify an alis or version here
RoleARN: !Sub arn:aws:iam::${AWS::AccountId}:role/aws-service-role/lambda.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_LambdaConcurrency
ScalableDimension: lambda:function:ProvisionedConcurrency
ServiceNamespace: lambda
DependsOn: MyFunctionAliaslive # This is your function logical ID + "Alias" + what you use for AutoPublishAlias
MyTargetTrackingScalingPolicy:
Type: AWS::ApplicationAutoScaling::ScalingPolicy
Properties:
PolicyName: utilization
PolicyType: TargetTrackingScaling
ScalingTargetId: !Ref MyScalableTarget
TargetTrackingScalingPolicyConfiguration:
TargetValue: 0.70 # Any value between 0 and 1 can be used here
PredefinedMetricSpecification:
PredefinedMetricType: LambdaProvisionedConcurrencyUtilization
@daorte
Copy link

daorte commented Jul 14, 2020

Hi Danilo! great content, thanks for sharing.
One question, in line 41, the value for DependsOn can be replaced for a dynamic value? I know can not use !Ref or !Sub but there is no other option?

Thanks

@srowshanLoyalty
Copy link

Hi @daorte
Could you find a solution for line 41? the only workaround we could came up with was adding scaling through a new cfn which being called after sam goes in successfully... would appreciate if anyone has a cleaner approach to share

Thanks

@dhaveman
Copy link

I also would like to know if a solution was found..

@xai1983kbu
Copy link

xai1983kbu commented Apr 7, 2021

line 50 should be:
TargetValue: 0.70 # Any value between 0.1 and 0.9 can be used here
see https://aws.amazon.com/blogs/aws/new-provisioned-concurrency-for-lambda-functions/

@PNTrong
Copy link

PNTrong commented May 14, 2021

Here is my solution:
"DependsOn": ["MyFunctionAliaslive"]
I use the list of strings instead of string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment