简体   繁体   English

Localstack 上的无服务器部署 Python HTTP API

[英]Serverless Deployment on Localstack for Python HTTP API

I have created from serverless project from aws-python-http-api template我从aws-python-http-api模板的无服务器项目创建

My serverless.yml file looks like below我的 serverless.yml 文件如下所示

service: aws-http-api
frameworkVersion: '3'

plugins:
  - serverless-localstack

provider:
  name: aws
  stage: local
  runtime: python3.9
  profile: localstack

functions:
  hello:
    handler: handler.hello
    events:
      - httpApi:
          path: /
          method: get

custom:
  localstack:
    stages:
      - local
    host: http://localhost 
    edgePort: 4566 
    autostart: true
    lambda:
      # Enable this flag to improve performance
      mountCode: True
    docker:
      # Enable this flag to run "docker ..." commands as sudo
      sudo: False
  stages:
    local:
      ...

I am running localstack with docker-compose and docker-compose.yml looks like我正在使用 docker-compose 和 docker-compose.yml 运行 localstack 看起来像

version: '3.0'

services:
  localstack:
    container_name: "${LOCALSTACK_DOCKER_NAME-localstack_latest}"
    image: localstack/localstack:latest
    environment:
      - AWS_DEFAULT_REGION=us-east-1
      - EDGE_PORT=4566
      - DEBUG=${DEBUG-}
      - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR-}
      - DOCKER_HOST=unix:///var/run/docker.sock
      - HOSTNAME=localhost
            - SERVICES=${SERVICES-serverless
          ,acm,apigateway,cloudformation,cloudwatch
          ,dynamodb,dynamodbstreams,ec2,es,events
          ,firehose,iam,kinesis,kms,lambda,rds
          ,route53,s3,s3api,secretsmanager,ses,sns
          ,sqs,ssm,stepfunctions,sts}
    ports:
      - "127.0.0.1:4566:4566"            # LocalStack Gateway
      - "127.0.0.1:4510-4559:4510-4559"  # external services port range
    volumes:
      - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"


docker-compose up -d worked properly and I can see results properly on http://127.0.0.1:4566/health docker-compose up -d工作正常,我可以在http://127.0.0.1:4566/health上正确看到结果

I have move into project folder after all and runned following command serverless deploy and error is毕竟我已经进入项目文件夹并按照命令无服务器部署运行并且错误是

UPDATE_FAILED: aws-http-api-local (AWS::CloudFormation::Stack)
undefined

I have also runned next one ** serverless deploy --stage local** and same error.我还运行了下一个 ** serverless deploy --stage local** 和同样的错误。 Any idea why it can happen?知道为什么会发生吗?

docker-compose up -d worked properly and I can see results properly on http://127.0.0.1:4566/health docker-compose up -d工作正常,我可以在http://127.0.0.1:4566/health上正确看到结果

I have move into project folder after all and runned following command serverless deploy and error is毕竟我已经进入项目文件夹并按照命令无服务器部署运行并且错误是

UPDATE_FAILED: aws-http-api-local (AWS::CloudFormation::Stack)
undefined

I have also runned next one ** serverless deploy --stage local** and same error.我还运行了下一个 ** serverless deploy --stage local** 和同样的错误。 Any idea why it can happen?知道为什么会发生吗?

Hi — To run the above Serverless example, you will need access to LocalStack Pro, since the Community version doesn't support some of the AWS APIs being called during the operation.您好——要运行上面的无服务器示例,您需要访问 LocalStack Pro,因为社区版本不支持在操作期间调用的某些 AWS API。

You would need to update the docker-compose.yml to the following configuration:您需要将docker-compose.yml更新为以下配置:

version: "3.8"

services:
  localstack:
    container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
    image: localstack/localstack-pro  # required for Pro
    ports:
      - "127.0.0.1:4566:4566"            # LocalStack Gateway
      - "127.0.0.1:4510-4559:4510-4559"  # external services port range
      - "127.0.0.1:53:53"                # DNS config (required for Pro)
      - "127.0.0.1:53:53/udp"            # DNS config (required for Pro)
      - "127.0.0.1:443:443"              # LocalStack HTTPS Gateway (required for Pro)
    environment:
      - DEBUG=${DEBUG-}
      - PERSISTENCE=${PERSISTENCE-}
      - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR-}
      - LOCALSTACK_API_KEY=XXXXXXX  # required for Pro
      - DOCKER_HOST=unix:///var/run/docker.sock
    volumes:
      - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

You will also need to update the serverless.yml to the following configuration:您还需要将serverless.yml更新为以下配置:

service: aws-python-http-api
frameworkVersion: '3'

provider:
  name: aws
  runtime: python3.9

functions:
  hello:
    handler: handler.hello
    events:
      - httpApi:
          path: /
          method: get

plugins:
  - serverless-localstack

custom:
  localstack:
    stages:
      - local

It would involve installing the serverless-localstack plugin as well.它还将涉及安装serverless-localstack插件。 On a successful setup, you can run serverless deploy --stage local , and it would yield the following output:设置成功后,您可以运行serverless deploy --stage local ,它会产生以下 output:

Using serverless-localstack

Deploying aws-python-http-api to stage local (us-east-1)
Skipping template validation: Unsupported in Localstack

✔ Service deployed to stack aws-python-http-api-local (14s)

endpoint: GET - https://e2af2ca4.execute-api.localhost.localstack.cloud:4566//
functions:
  hello: aws-python-http-api-local-hello (119 kB)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM