微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AWS::ApplicationAutoScaling::ScalableTarget for Lambda ProvisionedConcurrency 未按预期工作

如何解决AWS::ApplicationAutoScaling::ScalableTarget for Lambda ProvisionedConcurrency 未按预期工作

我的 Lambda 每天都在同一时间出现流量高峰。我希望当时有一定数量的预配置实例可用,并在 5 分钟后缩减规模。

我认为这可以通过 CloudFormation 和 AWS::ApplicationAutoScaling::scalableTarget 实现,但结果并不如预期。

为了测试该配置项的工作原理,我创建了一个小的测试 Lambda 函数(只是一个 15 秒睡眠的 HelloWorld.java;底部代码)和下面的 CloudFormation 模板,它应该每 4 分钟启动 10 个实例,并每 6 分钟缩减一次。

我打开了在 AWS 控制台 Lambda 函数上打开的 19 个浏览器选项卡,并“测试”了我的函数。基本上快速命中我的函数,以便需要启动新实例(因为内部 15 秒睡眠)。

我使用 Cloudwatch 绘制了 ConcurrentExecutionsProvisionedConcurrentExecutions。我期待看到 ProvisionedConcurrentExecutions 在 14:28,32,36 上升到 20...并在 14:30,34,48 缩减...

我在指标中看不到任何 ProvisionedConcurrentExecutions

我是否误解了它的工作原理,还是我弄错了 CloudFormation?

enter image description here

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.

Lambda 函数代码

公共类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 举报,一经查实,本站将立刻删除。