无服务器示例不适用于 serverless-localstack 插件

Serverless example not working with serverless-localstack plugin

我是 运行 来自官方 serverless GitHub 页面的示例,当我直接在我的 AWS 帐户上部署时,它开箱即用。

现在,当我尝试通过相应地更改 serverless.yml 来激活 serverless-localstack 插件时:

service: serverless-rest-api-with-dynamodb

frameworkVersion: ">=1.1.0 <2.0.0"

provider:
  name: aws
  runtime: python2.7
  environment:
    DYNAMODB_TABLE: ${self:service}-${opt:stage, self:provider.stage}
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}"

custom:
  localstack:
    stages:
    - local
    - dev
    endpoints:
      S3: http://localhost:4572
      DynamoDB: http://localhost:4570
      CloudFormation: http://localhost:4581
      Elasticsearch: http://localhost:4571
      ES: http://localhost:4578
      SNS: http://localhost:4575
      SQS: http://localhost:4576
      Lambda: http://localhost:4574
      Kinesis: http://localhost:4568

plugins:
  - serverless-localstack

functions:
  create:
    handler: todos/create.create
    events:
      - http:
          path: todos
          method: post
          cors: true

  list:
    handler: todos/list.list
    events:
      - http:
          path: todos
          method: get
          cors: true

  get:
    handler: todos/get.get
    events:
      - http:
          path: todos/{id}
          method: get
          cors: true

  update:
    handler: todos/update.update
    events:
      - http:
          path: todos/{id}
          method: put
          cors: true

  delete:
    handler: todos/delete.delete
    events:
      - http:
          path: todos/{id}
          method: delete
          cors: true

resources:
  Resources:
    TodosDynamoDbTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          -
            AttributeName: id
            AttributeType: S
        KeySchema:
          -
            AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: ${self:provider.environment.DYNAMODB_TABLE}

我在 localstack 端(docker 中的 运行)收到以下错误:

localstack_1  | 2019-12-04 09:48:15,260:API: 127.0.0.1 - - [04/Dec/2019 09:48:15] "GET /serverless-rest-api-with-dynamodb-local-ServerlessDeploymentBucket-5YYKLD61WXMT?location HTTP/1.1" 404 -
localstack_1  | 2019-12-04T09:48:15:DEBUG:localstack.services.cloudformation.cloudformation_listener: Error response from CloudFormation (400) POST /: b'<ErrorResponse xmlns="http://cloudformation.amazonaws.com/doc/2010-05-15/">\n  <Error>\n    <Type>Sender</Type>\n    <Code>ValidationError</Code>\n    <Message>Stack with id serverless-rest-api-with-dynamodb-local does not exist</Message>\n  </Error>\n  <RequestId>cf4c737e-5ae2-11e4-a7c9-ad44eEXAMPLE</RequestId>\n</ErrorResponse>'
localstack_1  | 2019-12-04T09:48:15:DEBUG:localstack.services.cloudformation.cloudformation_starter: Currently updating stack resource serverless-rest-api-with-dynamodb-local/ServerlessDeploymentBucket: None
localstack_1  | 2019-12-04T09:48:15:ERROR:localstack.services.cloudformation.cloudformation_starter: Unable to parse and create resource "ServerlessDeploymentBucket": 400 Bad Request: <?xml version="1.0" encoding="UTF-8"?>
localstack_1  | <Error>
localstack_1  |     <Code>InvalidBucketName</Code>
localstack_1  |     <Message>The specified bucket is not valid.</Message>
localstack_1  |     
localstack_1  |     <RequestID>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</RequestID>
localstack_1  | </Error> Traceback (most recent call last):
localstack_1  |   File "/opt/code/localstack/localstack/services/cloudformation/cloudformation_starter.py", line 173, in parse_and_create_resource
localstack_1  |     return _parse_and_create_resource(logical_id, resource_json, resources_map, region_name)
localstack_1  |   File "/opt/code/localstack/localstack/services/cloudformation/cloudformation_starter.py", line 229, in _parse_and_create_resource
localstack_1  |     resource_json_arns_fixed, resources_map, region_name)
localstack_1  |   File "/opt/code/localstack/.venv/lib/python3.7/site-packages/moto/cloudformation/parsing.py", line 306, in parse_and_create_resource
localstack_1  |     resource_name, resource_json, region_name)
localstack_1  |   File "/opt/code/localstack/.venv/lib/python3.7/site-packages/moto/s3/models.py", line 701, in create_from_cloudformation_json
localstack_1  |     bucket = s3_backend.create_bucket(resource_name, region_name)
localstack_1  |   File "/opt/code/localstack/.venv/lib/python3.7/site-packages/moto/s3/models.py", line 714, in create_bucket
localstack_1  |     raise InvalidBucketName()
localstack_1  | moto.s3.exceptions.InvalidBucketName: 400 Bad Request: <?xml version="1.0" encoding="UTF-8"?>
localstack_1  | <Error>
localstack_1  |     <Code>InvalidBucketName</Code>
localstack_1  |     <Message>The specified bucket is not valid.</Message>
localstack_1  |     
localstack_1  |     <RequestID>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</RequestID>
localstack_1  | </Error>
localstack_1  | 
localstack_1  | 2019-12-04T09:48:15:DEBUG:localstack.services.cloudformation.cloudformation_listener: Error response from CloudFormation (400) POST /: b'<?xml version="1.0" encoding="UTF-8"?>\n<Error>\n    <Code>InvalidBucketName</Code>\n    <Message>The specified bucket is not valid.</Message>\n    \n    <RequestID>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</RequestID>\n</Error>'
localstack_1  | 2019-12-04 09:48:34,571:API: 127.0.0.1 - - [04/Dec/2019 09:48:34] "GET /serverless-rest-api-with-dynamodb-local-ServerlessDeploymentBucket-C5ZNB3XCGTSA?location HTTP/1.1" 404 -
localstack_1  | 2019-12-04T09:48:34:DEBUG:localstack.services.cloudformation.cloudformation_listener: Error response from CloudFormation (400) POST /: b'<ErrorResponse xmlns="http://cloudformation.amazonaws.com/doc/2010-05-15/">\n  <Error>\n    <Type>Sender</Type>\n    <Code>ValidationError</Code>\n    <Message>Stack with id serverless-rest-api-with-dynamodb-local does not exist</Message>\n  </Error>\n  <RequestId>cf4c737e-5ae2-11e4-a7c9-ad44eEXAMPLE</RequestId>\n</ErrorResponse>'
localstack_1  | 2019-12-04T09:48:34:DEBUG:localstack.services.cloudformation.cloudformation_starter: Currently updating stack resource serverless-rest-api-with-dynamodb-local/ServerlessDeploymentBucket: None
localstack_1  | 2019-12-04T09:48:34:ERROR:localstack.services.cloudformation.cloudformation_starter: Unable to parse and create resource "ServerlessDeploymentBucket": 400 Bad Request: <?xml version="1.0" encoding="UTF-8"?>
localstack_1  | <Error>
localstack_1  |     <Code>InvalidBucketName</Code>
localstack_1  |     <Message>The specified bucket is not valid.</Message>
localstack_1  |     
localstack_1  |     <RequestID>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</RequestID>
localstack_1  | </Error> Traceback (most recent call last):
localstack_1  |   File "/opt/code/localstack/localstack/services/cloudformation/cloudformation_starter.py", line 173, in parse_and_create_resource
localstack_1  |     return _parse_and_create_resource(logical_id, resource_json, resources_map, region_name)
localstack_1  |   File "/opt/code/localstack/localstack/services/cloudformation/cloudformation_starter.py", line 229, in _parse_and_create_resource
localstack_1  |     resource_json_arns_fixed, resources_map, region_name)
localstack_1  |   File "/opt/code/localstack/.venv/lib/python3.7/site-packages/moto/cloudformation/parsing.py", line 306, in parse_and_create_resource
localstack_1  |     resource_name, resource_json, region_name)
localstack_1  |   File "/opt/code/localstack/.venv/lib/python3.7/site-packages/moto/s3/models.py", line 701, in create_from_cloudformation_json
localstack_1  |     bucket = s3_backend.create_bucket(resource_name, region_name)
localstack_1  |   File "/opt/code/localstack/.venv/lib/python3.7/site-packages/moto/s3/models.py", line 714, in create_bucket
localstack_1  |     raise InvalidBucketName()
localstack_1  | moto.s3.exceptions.InvalidBucketName: 400 Bad Request: <?xml version="1.0" encoding="UTF-8"?>
localstack_1  | <Error>
localstack_1  |     <Code>InvalidBucketName</Code>
localstack_1  |     <Message>The specified bucket is not valid.</Message>
localstack_1  |     
localstack_1  |     <RequestID>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</RequestID>
localstack_1  | </Error>
localstack_1  | 
localstack_1  | 2019-12-04T09:48:34:DEBUG:localstack.services.cloudformation.cloudformation_listener: Error response from CloudFormation (400) POST /: b'<?xml version="1.0" encoding="UTF-8"?>\n<Error>\n    <Code>InvalidBucketName</Code>\n    <Message>The specified bucket is not valid.</Message>\n    \n    <RequestID>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</RequestID>\n</Error>'

serverless.yml 中是否缺少使 localstack 插件正常工作的配置?

我用于部署的命令是:

SLS_DEBUG=3 serverless deploy --stage local --region us-east-1

我只尝试过一次 localstack,但没有成功使用本地堆栈。我用谷歌搜索了你并找到了答案。看看它是否有效。

provider:
  deploymentBucket:
    name: ${self:service}-${opt:stage}-deployment-bucket

参考:https://github.com/localstack/serverless-localstack/issues/30

我曾经遇到过这个问题,按照@arun-k 的建议,我添加了 deploymentBucket。虽然我遇到了其他问题,但它运行良好,所以我提取了 LocalStack 的标记图像(即 image: localstack/localstack:0.10.5)而不是稳定版本,我没有再次指定 deploymentBucket,它也有效.

认为它可能对其他人有用。