问题描述
我的 Lambda 每天都在同一时间出现流量高峰。我希望当时有一定数量的预配置实例可用,并在 5 分钟后缩减规模。
我认为这可以通过 CloudFormation 和 AWS::ApplicationAutoScaling::scalableTarget
实现,但结果并不如预期。
为了测试该配置项的工作原理,我创建了一个小的测试 Lambda 函数(只是一个 15 秒睡眠的 HelloWorld.java;底部的代码)和下面的 CloudFormation 模板,它应该每 4 分钟启动 10 个实例,并每 6 分钟缩减一次。
我打开了在 AWS 控制台 Lambda 函数上打开的 19 个浏览器选项卡,并“测试”了我的函数。基本上快速命中我的函数,以便需要启动新实例(因为内部 15 秒睡眠)。
我使用 Cloudwatch 绘制了 ConcurrentExecutions
和 ProvisionedConcurrentExecutions
。我期待看到 ProvisionedConcurrentExecutions
在 14:28,32,36 上升到 20...并在 14:30,34,48 缩减...
我在指标中看不到任何 ProvisionedConcurrentExecutions
。
我是否误解了它的工作原理,还是我弄错了 CloudFormation?
CloudFormation 模板
---
AWstemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Provision test
Globals:
Function:
AutopublishAlias: live
DeploymentPreference:
Type: AllAtOnce
Parameters:
Stage: {Type: String,Default: ''}
UseBatsKeyParam: {Type: String,Default: 'false',AllowedValues: ['true','false']}
DeploymentBucketImportName:
Type: String
Default: "DeploymentBucket"
Conditions:
UseBatsKey:
'Fn::Equals':
- {Ref: UseBatsKeyParam}
- 'true'
Resources:
# Lambda function
HelloWorld:
Type: AWS::Serverless::Function
Properties:
Handler: 'com.example.lambda.demo.Hello::handleRequest'
Runtime: java8.al2
Description: "Hello World"
MemorySize: 128
Timeout: 900
CodeUri:
# Why are we using this instead of BATS::SAM::CodeS3Bucket?
# Simply put,chicken and egg.
# BATS::SAM::S3Bucket just falls back to a pipeline parameter. Problem is,we can't kNow that bucket name at synthesis time.
# So we just import it.
Bucket: {'Fn::If' : ['UseBatsKey','BATS::SAM::CodeS3Bucket',{"Fn::ImportValue" : {Ref: 'DeploymentBucketImportName'}}]}
Key: BATS::SAM::CodeS3Key
Role:
Fn::GetAtt: [LambdaRole,Arn]
LambdaRole:
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Statement:
- Action: ['sts:AssumeRole']
Effect: Allow
Principal:
Service: [lambda.amazonaws.com]
Version: '2012-10-17'
Type: AWS::IAM::Role
TestFunctionConcurrency:
Type: 'AWS::ApplicationAutoScaling::scalableTarget'
Properties:
MaxCapacity: 100
MinCapacity: 3
ResourceId: !Sub 'function:${HelloWorld}:live'
RoleARN:
Fn::Sub: 'arn:${AWS::Partition}:iam::${AWS::AccountId}:role/aws-service-role/lambda.application-autoscaling.amazonaws.com/ AWSServiceRoleForApplicationAutoScaling_LambdaConcurrency'
scalableDimension: 'lambda:function:ProvisionedConcurrency'
ServiceNamespace: lambda
ScheduledActions:
- scalableTargetAction:
MinCapacity: 20
Schedule: 'cron(*/4 * * * ? *)'
ScheduledActionName: scale-out
- scalableTargetAction:
MinCapacity: 3
MaxCapacity: 3
Schedule: 'cron(2-59/4 * * * ? *)'
ScheduledActionName: scale-in
DependsOn: HelloWorldaliaslive
Outputs:
StackArn:
Value:
Ref: AWS::StackId
Description: Use this as the stack_arn in your cloud_formation_deployment_stack override.
公共类Hello实现RequestHandler
@Override
public String handleRequest(Object input,Context context) {
try {
Thread.sleep(20000);
} catch (Exception e) {
}
context.getLogger().log("Input: " + input);
return "Hello from Lambda asfter 30 seconds: " + input;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)