首页 > 解决方案 > 在 aws.com 上手动删除后,无服务器 API 网关资源未恢复

问题描述

我试图删除GET我在 AWS API Gateway 上手动创建的测试资源,但它删除了我POST在同一资源路径中的资源,现在当我sls deploy的 lambda 资源没有返回 API Gateway 的资源列表时:

functions:
  updateLibrary:
    environment:
      ...
    handler: updates/lambda.handler
    events:
      - http:
          path: library/updated
          method: post
          private: true

缺少library/updated POST资源

AWS API Gateway 资源,缺少资源

使用资源的开发阶段的旧部署是什么样的library/updated POST

在此处输入图像描述

sls deploy日志的结果:

Serverless: Stack update finished...
Serverless: Invoke aws:info
Serverless: [AWS cloudformation 200 0.484s 0 retries] describeStacks({ StackName: '***********-********-***' })
Serverless: [AWS cloudformation 200 0.508s 0 retries] listStackResources({ StackName: '***********-********-***' })
Service Information
service: ***********-********
stage: dev
region: **-****-*
stack: ***********-********-***
resources: 26
api keys:
  None
endpoints:
  POST - https://**********.execute-api.**-****-*.amazonaws.com/dev/library/updated
  POST - https://**********.execute-api.**-****-*.amazonaws.com/dev/merge/approved
functions:
  updateLibrary: ***********-********-***-updateLibrary
  mergePR: ***********-********-***-mergePR
layers:
  None
Serverless: [AWS sts 200 0.452s 0 retries] getCallerIdentity({})
Serverless: [AWS apigateway 200 0.633s 0 retries] getRestApis({ position: undefined, limit: 500 })
Serverless: [AWS apigateway 200 0.404s 0 retries] getStage({ restApiId: '**********', stageName: 'dev' })
Serverless: [AWS apigateway 200 0.577s 0 retries] updateStage({ restApiId: '**********',
  stageName: 'dev',
  patchOperations: 
   [ { op: 'replace',
       path: '/accessLogSettings/destinationArn',
       value: 'arn:aws:logs:**-****-*:**************:log-group:/aws/api-gateway/***********-********-***' },
     { op: 'replace',
       path: '/accessLogSettings/format',
       value: '{"requestTime":"$context.requestTime","requestId":"$context.requestId","apiId":"$context.apiId","resourceId":"$context.resourceId","resourcePath":"$context.resourcePath","path":"$context.path","httpMethod":"$context.httpMethod","status":"$context.status","authLatency":"$context.authorizer.integrationLatency","integrationLatency":"$context.integrationLatency","integrationStatus":"$context.integrationStatus","responseLatency":"$context.responseLatency","responseLength":"$context.responseLength","errorMessage":"$context.error.message","format":"SLS_ACCESS_LOG","version":"1.0.0"}' },
     { op: 'replace', path: '/*/*/logging/dataTrace', value: 'true' },
     { op: 'replace', path: '/*/*/logging/loglevel', value: 'INFO' },
     [length]: 4 ] })
Serverless: Invoke aws:deploy:finalize
Serverless: [AWS s3 200 0.511s 0 retries] listObjectsV2({ Bucket: '***********-********-***-serverlessdeploymentbucket-**************',
  Prefix: 'serverless/***********-********/dev' })
Serverless: Removing old service artifacts from S3...
Serverless: [AWS s3 200 0.503s 0 retries] deleteObjects({ Bucket: '***********-********-***-serverlessdeploymentbucket-**************',
  Delete: 
   { Objects: 
      [ { Key: 'serverless/***********-********/dev/******************-2020-02-09T03:31:30.226Z/***********-********.zip' },
        { Key: 'serverless/***********-********/dev/******************-2020-02-09T03:31:30.226Z/compiled-cloudformation-template.json' },
        { Key: 'serverless/***********-********/dev/******************-2020-02-09T03:31:30.226Z/custom-resources.zip' },
        [length]: 3 ] } })
Serverless: Publishing service to the Serverless Dashboard...
Serverless: [AWS sts 200 0.432s 0 retries] getCallerIdentity({})
Serverless: [AWS cloudformation 200 0.49s 0 retries] describeStacks({ StackName: '***********-********-***' })
platform-sdk fetching: POST https://api.serverless.com/core/tenants/********/applications/******-*****-*******/services/***********-********/stages/dev/regions/**-****-*/deployments
Serverless: Successfully published your service to the Serverless Dashboard: https://dashboard.serverless.com/tenants/********/applications/******-*****-*******/services/***********-********/stage/dev/region/**-****-*

我看到:

  patchOperations: 
   [ { op: 'replace'

在这些日志中,也许它只是尝试执行更新操作而不是尝试再次创建资源,因为它认为已经存在。我试过sls deploy --force但结果相同。

标签: amazon-web-servicesaws-lambdaaws-api-gatewayserverless-frameworkamazon-api-gateway

解决方案


该问题是由无服务器如何处理、跟踪和部署您的功能和资源引起的。当您删除 API Gateway 方法时,您实际上手动更改了应用程序的状态,并且它基本上不同步。

要解决此问题,请在 serverless.yaml 文件中注释掉您手动删除的整个方法,然后运行 ​​sls deploy。当它完成取消注释该方法和 sls 再次部署。这次它应该再次部署您的方法。


推荐阅读