【问题标题】:AWS CloudFormation is stuck at CREATE_IN_PROGRESSAWS CloudFormation 卡在 CREATE_IN_PROGRESS
【发布时间】:2021-10-24 05:15:36
【问题描述】:

我正在使用 AWS Lambda 构建一个相当大的 REST API。语言是 node.js。有超过 200 种功能,还有更多功能即将推出。这些函数中的每一个都是连接 RDS 数据库,获取数据或保存数据。

我正在使用aws sam 工具部署它。下面是template.yaml。请注意,我只发布一种方法,因为从外观上看,所有方法看起来都一样,除了它们指向的端点。

 WSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  xxx-restapi

  Sample SAM Template for xxx-restapi
  
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3   
    VpcConfig:
        SecurityGroupIds:
          - sg-041f2459dcd921e8e
        SubnetIds:
          - subnet-038xxx2d
          - subnet-c4dxxxcb
          - subnet-af5xxxc8

Resources:
  GetAllAccountingTypesFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: xxx-restapi/
      Handler: source/accounting-types/accountingtypes-getall.getallaccountingtypes
      Runtime: nodejs14.x
      Events:
        GetAllAccountingTypesAPIEvent:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /accountingtypes/getall
            Method: get
  GetAccountingTypeByIDFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: xxx-restapi/
      Handler: source/accounting-types/accountingtypes-byid.getbyid
      Runtime: nodejs14.x
      Events:
        GetAllAccountingTypesAPIEvent:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /accountingtypes/getbyid
            Method: get

LambdaRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - 'sts:AssumeRole'
      Path: /
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
      Policies:
        - PolicyName: root
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - ec2:DescribeNetworkInterfaces
                  - ec2:CreateNetworkInterface
                  - ec2:DeleteNetworkInterface
                  - ec2:DescribeInstances
                  - ec2:AttachNetworkInterface
                Resource: '*'

Outputs:
  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
  # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
  HelloWorldApi:
    Description: "API Gateway endpoint URL for Prod stage for functions"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"

我所有的方法都很好,它们按预期工作。但是,当我尝试部署时,这会卡在CREATE_IN_PROGRESS。但是,如果我减少函数的数量并尝试,它会起作用。

我检查了aws cloud trail 日志,发现如下所示。

ErrorCode: Client.RequestLimitExceeded
Resources: [{"resourceType":"AWS::EC2::SecurityGroup","resourceName":"sg-041f245xxxxd921e8e"},{"resourceType":"AWS::EC2::Subnet","resourceName":"subnet-af5xxxc8"}]

ErrorCode: Client.DryRunOperation
Resources: [{"resourceType":"AWS::EC2::SecurityGroup","resourceName":"sg-041f2459xxxx1e8e"},{"resourceType":"AWS::EC2::Subnet","resourceName":"subnet-axxxx3c8"}]

上面有多个事件。我该如何解决这个问题?

【问题讨论】:

    标签: amazon-web-services amazon-ec2 aws-lambda amazon-cloudformation aws-sam


    【解决方案1】:

    CloudFormation 可能一次创建了太多函数,因此您遇到了限制。您可能会将其归类为 CloudFormation 中的错误,因此我认为您应该确定将其报告给 AWS 或 CloudFormation 团队。

    话虽如此,一个可能的解决方法是分步部署。在每次更新时添加一些 Lambda 函数。这将是一件非常麻烦的事情,但我没有看到其他方法。

    您总是可以通过使用嵌套堆栈来简化此过程(然后您可以一一取消注释)。也许您甚至可以用它绕过整个节流限制,这取决于 CloudFormation 如何处理它。但我不确定。

    如果您在一个堆栈中管理如此多的资源,那么您也有可能达到其他 CloudFormation 限制(尤其是因为 SAM 将多个资源抽象为单一类型之后)。因此,使用嵌套堆栈还可以防止您在(不久的)将来达到这些限制。

    【讨论】:

    • 终于!有云知识的人!谢谢你的回复。我想我设法通过为每个 lambda 函数添​​加 DependsOn 选项来解决问题。现在一个功能一个接一个地安装。你能告诉我为什么我要点击Client.DryRunOperation 以及如何解决它吗?对于我拥有的每个子网,都会生成此错误。在拥有DependsOn 之后,看起来DryRun 错误是在我拥有的最后一种方法中引起的。如果您想单独回答这个问题,请在此处查看 - stackoverflow.com/questions/68906072/…
    • 我还有更多问题要问。我知道DependsOn 是膏药而不是修复剂。我的问题是(1)如何在每次更新时添加 lambda 函数?评论和取消评论? aws 不会删除认为不再需要的注释函数吗?
    • 什么是嵌套堆栈?你能给我指出一个合适的template.yaml 例子吗?
    • 您可以找到更多关于嵌套堆栈的信息in the user guidecfn documentation 中也有一个示例。
    • 谢谢。您也可以评论我的其他问题吗?评论 - 取消评论等?
    猜你喜欢
    • 2020-07-11
    • 2015-12-20
    • 2021-10-03
    • 2018-04-21
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 2019-08-19
    相关资源
    最近更新 更多