Serverless Deployment on Localstack for Python HTTP API

243 Views Asked by At

I have created from serverless project from aws-python-http-api template

My serverless.yml file looks like below

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

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

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. 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

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. Any idea why it can happen ?

1

There are 1 best solutions below

1
On

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.

You would need to update the docker-compose.yml to the following configuration:

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:

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. On a successful setup, you can run serverless deploy --stage local, and it would yield the following 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)