Created
October 31, 2024 17:05
-
-
Save bensie/ca68002fb52e106deaedfc9024b85896 to your computer and use it in GitHub Desktop.
Pre-warm Lambda functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Parameters: | |
PreWarmEnvironmentParameter: | |
Type: String # enabled or disabled, only enable for low-volume environments | |
PreWarmFunctionNamesParameter: | |
Type: String # JSON string like ["MyFunctionToKeepWarm1","MyFunctionToKeepWarm2"] | |
Conditions: | |
IsPreWarmEnvironmentEnabled: | |
!Equals [!Ref PreWarmEnvironmentParameter, enabled] | |
Resources: | |
PreWarmEnvironmentWorkflow: | |
Type: AWS::StepFunctions::StateMachine | |
Condition: IsPreWarmEnvironmentEnabled | |
Properties: | |
DefinitionString: !Sub | | |
{ | |
"Comment": "Pre-warm environment", | |
"StartAt": "PassFunctionsToPreWarm", | |
"States": { | |
"PassFunctionsToPreWarm": { | |
"Type": "Pass", | |
"Next": "PreWarmFunctionsMap", | |
"Result": { | |
"preWarmFunctionNames": ${PreWarmFunctionNamesParameter} | |
} | |
}, | |
"PreWarmFunctionsMap": { | |
"Type": "Map", | |
"ItemsPath": "$.preWarmFunctionNames", | |
"Iterator": { | |
"StartAt": "InvokeLambdaFunction", | |
"States": { | |
"InvokeLambdaFunction": { | |
"Type": "Task", | |
"Resource": "arn:aws:states:::lambda:invoke", | |
"Parameters": { | |
"FunctionName.$": "$", | |
"Payload": { | |
"preWarm": true | |
} | |
}, | |
"End": true | |
} | |
} | |
}, | |
"End": true | |
} | |
} | |
} | |
RoleArn: !GetAtt PreWarmEnvironmentRole.Arn | |
StateMachineType: EXPRESS | |
PreWarmEnvironmentRole: | |
Type: AWS::IAM::Role | |
Condition: IsPreWarmEnvironmentEnabled | |
Properties: | |
AssumeRolePolicyDocument: | |
Version: 2012-10-17 | |
Statement: | |
- Effect: Allow | |
Principal: | |
Service: | |
- !Sub states.${AWS::URLSuffix} | |
Action: sts:AssumeRole | |
Policies: | |
- PolicyName: InvokeFunctions | |
PolicyDocument: | |
Version: 2012-10-17 | |
Statement: | |
- Effect: Allow | |
Action: | |
- lambda:InvokeFunction | |
Resource: | |
- "*" # consider using a tag condition or list out PreWarmFunctionNamesParameter if you use this! | |
PreWarmEnvironmentSchedule: | |
Type: AWS::Scheduler::Schedule | |
Condition: IsPreWarmEnvironmentEnabled | |
Properties: | |
Description: Pre-warm environment | |
FlexibleTimeWindow: | |
Mode: "OFF" | |
ScheduleExpression: rate(1 minute) | |
State: ENABLED | |
Target: | |
Arn: !GetAtt PreWarmEnvironmentWorkflow.Arn | |
RoleArn: !GetAtt PreWarmEnvironmentScheduleRole.Arn | |
PreWarmEnvironmentScheduleRole: | |
Type: AWS::IAM::Role | |
Condition: IsPreWarmEnvironmentEnabled | |
Properties: | |
AssumeRolePolicyDocument: | |
Version: 2012-10-17 | |
Statement: | |
- Effect: Allow | |
Principal: | |
Service: | |
- !Sub scheduler.${AWS::URLSuffix} | |
Action: sts:AssumeRole | |
Policies: | |
- PolicyName: StartExecution | |
PolicyDocument: | |
Version: 2012-10-17 | |
Statement: | |
- Effect: Allow | |
Action: | |
- states:StartExecution | |
Resource: !GetAtt PreWarmEnvironmentWorkflow.Arn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment