无法将无服务器框架应用程序部署到 localstack

问题描述

我正在学习如何使用 localstack 来测试我的无服务器应用。

我从一个从模板创建的简单无服务器应用开始,

serverless create --template aws-nodejs

这是处理程序,

'use strict';

module.exports.hello = async (event) => {
  return {
    statusCode: 200,body: JSON.stringify(
      {
        message: 'Go Serverless v1.0! Your function executed successfully!',input: event,},null,2
    ),};

  // Use this code if you don't use the http event with the LAMBDA-PROXY integration
  // return { message: 'Go Serverless v1.0! Your function executed successfully!',event };
};

然后我安装了serverless-localstack

npm install --save-dev serverless-localstack

我已经使用 pip 安装了 localstack

这是我的 serverless.yml 文件

service: localstack-lambda

plugins:
  - serverless-localstack

custom:
  localstack:
    debug: true
    host: http://localhost
    edgePort: 4566
    autostart: true  # optional - Start LocalStack in Docker on Serverless deploy
    docker:
      # Enable this flag to run "docker ..." commands as sudo
      sudo: False
    
frameworkVersion: '2'

provider:
  name: aws
  runtime: nodejs12.x


functions:
  hello:
    handler: handler.hello

我没有指定任何阶段,例如 local,如果没有指定阶段,那么认情况下 serverless-localstack 将部署到本地堆栈以用于 dev 阶段(这是认值)

现在,我的期望是,当我执行 sls deploy 时,它应该在 localstack 中创建 lambda 函数。当我执行 aws --endpoint-url=http://localhost:4566 lambda list-functions 时,我应该看到创建的函数

然而,实际发生的是,它只是以看似没有错误的方式结束,但我没有看到任何函数被创建。这是 sls deploy 日志,

─ ~/Documents/Playground/serverless-localstack                                                                                                       1 ✘  took 4s  at 05:12:15 pm ─╮
╰─ sls deploy                                                                                                                                                                          ─╯
Serverless: config.options_stage: undefined
Serverless: serverless.service.custom.stage: undefined
Serverless: serverless.service.provider.stage: dev
Serverless: config.stage: dev
Serverless: Using serverless-localstack
Serverless: Reconfiguring service acm to use http://localhost:4566
Serverless: Reconfiguring service amplify to use http://localhost:4566
Serverless: Reconfiguring service apigateway to use http://localhost:4566
Serverless: Reconfiguring service apigatewayv2 to use http://localhost:4566
Serverless: Reconfiguring service application-autoscaling to use http://localhost:4566
Serverless: Reconfiguring service appsync to use http://localhost:4566
Serverless: Reconfiguring service athena to use http://localhost:4566
Serverless: Reconfiguring service autoscaling to use http://localhost:4566
Serverless: Reconfiguring service batch to use http://localhost:4566
Serverless: Reconfiguring service cloudformation to use http://localhost:4566
Serverless: Reconfiguring service cloudfront to use http://localhost:4566
Serverless: Reconfiguring service cloudsearch to use http://localhost:4566
Serverless: Reconfiguring service cloudtrail to use http://localhost:4566
Serverless: Reconfiguring service cloudwatch to use http://localhost:4566
Serverless: Reconfiguring service cloudwatchlogs to use http://localhost:4566
Serverless: Reconfiguring service codecommit to use http://localhost:4566
Serverless: Reconfiguring service cognito-idp to use http://localhost:4566
Serverless: Reconfiguring service cognito-identity to use http://localhost:4566
Serverless: Reconfiguring service docdb to use http://localhost:4566
Serverless: Reconfiguring service dynamodb to use http://localhost:4566
Serverless: Reconfiguring service dynamodbstreams to use http://localhost:4566
Serverless: Reconfiguring service ec2 to use http://localhost:4566
Serverless: Reconfiguring service ecr to use http://localhost:4566
Serverless: Reconfiguring service ecs to use http://localhost:4566
Serverless: Reconfiguring service eks to use http://localhost:4566
Serverless: Reconfiguring service elasticache to use http://localhost:4566
Serverless: Reconfiguring service elasticbeanstalk to use http://localhost:4566
Serverless: Reconfiguring service elb to use http://localhost:4566
Serverless: Reconfiguring service elbv2 to use http://localhost:4566
Serverless: Reconfiguring service emr to use http://localhost:4566
Serverless: Reconfiguring service es to use http://localhost:4566
Serverless: Reconfiguring service events to use http://localhost:4566
Serverless: Reconfiguring service firehose to use http://localhost:4566
Serverless: Reconfiguring service glacier to use http://localhost:4566
Serverless: Reconfiguring service glue to use http://localhost:4566
Serverless: Reconfiguring service iam to use http://localhost:4566
Serverless: Reconfiguring service iot to use http://localhost:4566
Serverless: Reconfiguring service iotanalytics to use http://localhost:4566
Serverless: Reconfiguring service iotevents to use http://localhost:4566
Serverless: Reconfiguring service iot-data to use http://localhost:4566
Serverless: Reconfiguring service iot-jobs-data to use http://localhost:4566
Serverless: Reconfiguring service kafka to use http://localhost:4566
Serverless: Reconfiguring service kinesis to use http://localhost:4566
Serverless: Reconfiguring service kinesisanalytics to use http://localhost:4566
Serverless: Reconfiguring service kms to use http://localhost:4566
Serverless: Reconfiguring service lambda to use http://localhost:4566
Serverless: Reconfiguring service logs to use http://localhost:4566
Serverless: Reconfiguring service mediastore to use http://localhost:4566
Serverless: Reconfiguring service neptune to use http://localhost:4566
Serverless: Reconfiguring service organizations to use http://localhost:4566
Serverless: Reconfiguring service qldb to use http://localhost:4566
Serverless: Reconfiguring service rds to use http://localhost:4566
Serverless: Reconfiguring service redshift to use http://localhost:4566
Serverless: Reconfiguring service route53 to use http://localhost:4566
Serverless: Reconfiguring service s3 to use http://localhost:4566
Serverless: Reconfiguring service s3control to use http://localhost:4566
Serverless: Reconfiguring service sagemaker to use http://localhost:4566
Serverless: Reconfiguring service sagemaker-runtime to use http://localhost:4566
Serverless: Reconfiguring service secretsmanager to use http://localhost:4566
Serverless: Reconfiguring service ses to use http://localhost:4566
Serverless: Reconfiguring service sns to use http://localhost:4566
Serverless: Reconfiguring service sqs to use http://localhost:4566
Serverless: Reconfiguring service ssm to use http://localhost:4566
Serverless: Reconfiguring service stepfunctions to use http://localhost:4566
Serverless: Reconfiguring service sts to use http://localhost:4566
Serverless: Reconfiguring service timestream to use http://localhost:4566
Serverless: Reconfiguring service transfer to use http://localhost:4566
Serverless: Reconfiguring service xray to use http://localhost:4566
Serverless: Deprecation warning: Resolution of lambda version hashes was improved with better algorithm,which will be used in next major release.
            Switch to it Now by setting "provider.lambdaHashingVersion" to "20201221"
            More Info: https://www.serverless.com/framework/docs/deprecations/#LAMBDA_HASHING_VERSION_V2
Serverless: config.options_stage: undefined
Serverless: serverless.service.custom.stage: undefined
Serverless: serverless.service.provider.stage: dev
Serverless: config.stage: dev
Serverless: config.options_stage: undefined
Serverless: serverless.service.custom.stage: undefined
Serverless: serverless.service.provider.stage: dev
Serverless: config.stage: dev
Serverless: Packaging service...
Serverless: Excluding development dependencies...
 
 Serverless Error ----------------------------------------
 
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com
 
  Your Environment information ---------------------------
     Operating System:          darwin
     Node Version:              14.16.1
     Framework Version:         2.44.0
     Plugin Version:            5.2.0
     SDK Version:               4.2.3
     Components Version:        3.11.0

要么这里没有错误,要么由于某种原因它不可见。

这里是来自 localstack 容器的日志,

 ~/Documents/Playground/serverless-localstack                                                                                                                   ✔  at 05:24:45 pm ─╮
╰─ docker logs -f ac9db5ab5627                                                                                                                                                         ─╯
Waiting for all LocalStack services to be ready
2021-06-05 11:36:13,361 CRIT Supervisor is running as root.  Privileges were not dropped because no user is specified in the config file.  If you intend to run as root,you can set user=root in the config file to avoid this message.
2021-06-05 11:36:13,367 INFO supervisord started with pid 15
2021-06-05 11:36:14,372 INFO spawned: 'dashboard' with pid 21
2021-06-05 11:36:14,375 INFO spawned: 'infra' with pid 22
2021-06-05 11:36:14,379 INFO success: dashboard entered RUNNING state,process has stayed up for > than 0 seconds (startsecs)
2021-06-05 11:36:14,379 INFO exited: dashboard (exit status 0; expected)
(. .venv/bin/activate; exec bin/localstack start --host)
2021-06-05 11:36:15,386 INFO success: infra entered RUNNING state,process has stayed up for > than 1 seconds (startsecs)
Starting local dev environment. CTRL-C to quit.
Waiting for all LocalStack services to be ready

LocalStack version: 0.12.11
LocalStack Docker container id: ac9db5ab5627
LocalStack build date: 2021-06-04
LocalStack build git hash: 4005365d

2021-06-05T11:36:21:DEBUG:bootstrap.py: Loading plugins - scope "services",module "localstack": <function register_localstack_plugins at 0x7f9d5a5af700>
Starting edge router (https port 4566)...
Starting mock ACM service on http port 4566 ...
Starting mock API Gateway service on http port 4566 ...
Starting mock CloudFormation service on http port 4566 ...
Starting mock CloudWatch service on http port 4566 ...
Starting mock DynamoDB service on http port 4566 ...
Starting mock DynamoDB Streams service on http port 4566 ...
Starting mock EC2 service on http port 4566 ...
Starting mock ES service on http port 4566 ...
Starting mock Firehose service on http port 4566 ...
Starting mock IAM service on http port 4566 ...
Starting mock STS service on http port 4566 ...
Starting mock Kinesis service on http port 4566 ...
Starting mock KMS service on http port 4566 ...
2021-06-05T11:36:21:INFO:localstack.multiserver: Starting multi API server process on port 37957
[2021-06-05 11:36:21 +0000] [23] [INFO] Running on http://0.0.0.0:37957 (CTRL + C to quit)
2021-06-05T11:36:21:INFO:hypercorn.error: Running on http://0.0.0.0:37957 (CTRL + C to quit)
[2021-06-05 11:36:21 +0000] [23] [INFO] Running on https://0.0.0.0:4566 (CTRL + C to quit)
2021-06-05T11:36:21:INFO:hypercorn.error: Running on https://0.0.0.0:4566 (CTRL + C to quit)
2021-06-05 11:36:21,958:API:  * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
2021-06-05 11:36:21,959:API:  * Running on http://172.17.0.2:39623/ (Press CTRL+C to quit)
INFO[2021-06-05 11:36:22.641] Local KMS 3.9.1 (4e5eb289efb23f9059ae4b1f9896bad6c9a499f2)
INFO[2021-06-05 11:36:22.679] No file found at path /init/seed.yaml; skipping seeding.
INFO[2021-06-05 11:36:22.679] Data will be stored in /tmp/local-kms
INFO[2021-06-05 11:36:22.679] Local KMS started on 0.0.0.0:42981
Initializing DynamoDB Local with the following configuration:
Port:   48595
InMemory:       true
DbPath: null
SharedDb:       false
shouldDelayTransientStatuses:   false
CorsParams:     *

Starting mock Lambda service on http port 4566 ...
Starting mock CloudWatch Logs service on http port 4566 ...
Starting mock Redshift service on http port 4566 ...
Starting mock Route53 service on http port 4566 ...
Starting mock S3 service on http port 4566 ...
Starting mock Secrets Manager service on http port 4566 ...
Starting mock SES service on http port 4566 ...
Starting mock SNS service on http port 4566 ...
Starting mock SQS service on http port 4566 ...
Starting mock SSM service on http port 4566 ...
Starting mock Cloudwatch Events service on http port 4566 ...
Listening at http://:::37625
Starting mock StepFunctions service on http port 4566 ...
Starting mock SWF service on http port 4566 ...
Starting mock Resource Groups Tagging API service on http port 4566 ...
Starting mock Resource Groups API service on http port 4566 ...
Starting mock Support service on http port 4566 ...
Step Functions Local
Version: 1.7.6
Build: 2021-03-26
2021-06-05 11:36:24.127: Configure [PORT] to [8083]
2021-06-05 11:36:24.143: Configure [Account] to [000000000000]
2021-06-05 11:36:24.144: Configure [Region] to [us-east-1]
2021-06-05 11:36:24.144: Configure [Lambda Endpoint] to [http://localhost:4566]
2021-06-05 11:36:24.145: Configure [Batch Endpoint] to [http://localhost:4566]
2021-06-05 11:36:24.145: Configure [DynamoDB Endpoint] to [http://localhost:4566]
2021-06-05 11:36:24.145: Configure [ECS Endpoint] to [http://localhost:4566]
2021-06-05 11:36:24.146: Configure [glue Endpoint] to [http://localhost:4566]
2021-06-05 11:36:24.146: Configure [SageMaker Endpoint] to [http://localhost:4566]
2021-06-05 11:36:24.147: Configure [SQS Endpoint] to [http://localhost:4566]
2021-06-05 11:36:24.147: Configure [SNS Endpoint] to [http://localhost:4566]
2021-06-05 11:36:24.148: Configure [Step Functions Endpoint] to [http://localhost:4566]
2021-06-05 11:36:24.148: Configure [Step Functions Athena] to [http://localhost:4566]
2021-06-05 11:36:24.148: Configure [Step Functions EKS] to [http://localhost:4566]
2021-06-05 11:36:24.167: Loaded credentials from environment
2021-06-05 11:36:24.167: Starting server on port 8083 with account 000000000000,region us-east-1
Jun 05,2021 11:36:25 AM com.amazonaws.http.apache.client.impl.ApacheConnectionManagerFactory createSocketFactoryRegistry
WARNING: SSL Certificate checking for endpoints has been explicitly disabled.
Waiting for all LocalStack services to be ready
Jun 05,2021 11:36:25 AM com.amazonaws.http.apache.client.impl.ApacheConnectionManagerFactory createSocketFactoryRegistry
WARNING: SSL Certificate checking for endpoints has been explicitly disabled.
Jun 05,2021 11:36:26 AM com.amazonaws.http.apache.client.impl.ApacheConnectionManagerFactory createSocketFactoryRegistry
WARNING: SSL Certificate checking for endpoints has been explicitly disabled.
Jun 05,2021 11:36:26 AM com.amazonaws.internal.DefaultServiceEndpointBuilder getServiceEndpoint
INFO: {databrew,us-east-1} was not found in region Metadata,trying to construct an endpoint using the standard pattern for this region: 'databrew.us-east-1.amazonaws.com'.
Waiting for all LocalStack services to be ready
Ready.
2021-06-05T11:36:34:INFO:localstack.utils.analytics.profiler: Execution of "start_api_services" took 12755.086183547974ms
2021-06-05T11:36:38:INFO:localstack.services.edge: Unable to find forwarding rule for host "localhost:4566",path "POST /",target header "",auth header "AWS4-HMAC-SHA256 Credential= /20210605/us-east-1/cloudformation/aws4_request,SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-security-token,Signature=e87dd28034bc2d712b371bb962b65b364e7549bb05c082476a0f16416d42ba1c",data "b'Action=DescribeStacks&StackName=localstack-lambda-dev&Version=2010-05-15'"
2021-06-05T11:37:02:INFO:localstack.services.edge: Unable to find forwarding rule for host "localhost:4566",Signature=bb561b6d3ad75430d504c1d69501427b0e47549e68233f2c12daa0024a75003e",data "b'Action=DescribeStacks&StackName=localstack-lambda-dev&Version=2010-05-15'"

如您所见,localstack 容器日志有一个有趣的错误

2021-06-05T11:36:38:INFO:localstack.services.edge: Unable to find forwarding rule for host "localhost:4566",data "b'Action=DescribeStacks&StackName=localstack-lambda-dev&Version=2010-05-15'"

我看到提到了 cloudformation,但我可以验证它正在运行。 我检查了 http://localhost:4566/health 并得到了,

{"services": {"apigateway": "running","cloudformation": "running","cloudwatch": "running","dynamodb": "running","dynamodbstreams": "running","ec2": "running","es": "running","firehose": "running","iam": "running","sts": "running","kinesis": "running","kms": "running","lambda": "running","logs": "running","redshift": "running","route53": "running","s3": "running","secretsmanager": "running","ses": "running","sns": "running","sqs": "running","ssm": "running","events": "running","stepfunctions": "running","swf": "running","resourcegroupstaggingapi": "running","resource-groups": "running","support": "running","acm": "running"},"features": {"persistence": "disabled","initScripts": "initialized"}}

我需要帮助弄清楚为什么 lambda 函数没有按预期创建

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)