localstack serverless deploy is not getting deployed

Viewed 1131

I am trying to deploy my serverless project locally with LocalStack and serverless-local plugin. When I try to deploy it with serverless deploy it throws an error and its failing to create the cloudformation stack.But, I manage to create the same stack when I deploy the project in to real aws environment. What is the possible issue here. I checked answers in all the previous questions asked on similar issue, nothing seems to work.

docker-compose.yml

version: "3.8"

services:
  localstack:
    container_name: "serverless-localstack_main"
    image: localstack/localstack
    ports:
      - "4566-4597:4566-4597"
    environment:
      - AWS_DEFAULT_REGION=eu-west-1
      - EDGE_PORT=4566
      - SERVICES=lambda,cloudformation,s3,sts,iam,apigateway,cloudwatch
    volumes:
      - "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

serverless.yml

service: serverless-localstack-test

frameworkVersion: '2'

plugins:
  - serverless-localstack

custom:
  localstack:
    debug: true
    host: http://localhost
    edgePort: 4566
    autostart: true
    lambda:
      mountCode: True
    stages:
      - local
    endpointFile: config.json


provider:
  name: aws
  runtime: nodejs12.x
  lambdaHashingVersion: 20201221
  stage: local
  region: eu-west-1
  deploymentBucket:
    name: deployment

functions:
  hello:
    handler: handler.hello

Config.json (which has the endpoints)

{
    "CloudFormation": "http://localhost:4566",
    "CloudWatch": "http://localhost:4566",
    "Lambda": "http://localhost:4566",
    "S3": "http://localhost:4566"
}

Error in Localstack container

serverless-localstack_main | 2021-06-04T17:41:49:WARNING:localstack.utils.cloudformation.template_deployer: Error calling
 <bound method ClientCreator._create_api_method.<locals>._api_call of 
<botocore.client.Lambda object at 0x7f31f359a4c0>> with params: {'FunctionName': 
'serverless-localstack-test-local-hello', 'Runtime': 'nodejs12.x', 'Role': 
'arn:aws:iam::000000000000:role/serverless-localstack-test-local-eu-west-1-lambdaRole',
 'Handler': 'handler.hello', 'Code': {'S3Bucket': '__local__', 'S3Key': 
'/Users/charles/Documents/Practice/serverless-localstack-test'}, 'Timeout': 6,
 'MemorySize': 1024} for resource: {'Type': 'AWS::Lambda::Function', 'Properties': 
{'Code': {'S3Bucket': '__local__', 'S3Key': 
'/Users/charles/Documents/Practice/serverless-localstack-test'}, 'Handler': 
'handler.hello', 'Runtime': 'nodejs12.x', 'FunctionName': 'serverless-localstack-test-
local-hello', 'MemorySize': 1024, 'Timeout': 6, 'Role': 
'arn:aws:iam::000000000000:role/serverless-localstack-test-local-eu-west-1-lambdaRole'}, 
'DependsOn': ['HelloLogGroup'], 'LogicalResourceId': 'HelloLambdaFunction', 
'PhysicalResourceId': None, '_state_': {}}
2 Answers

You need to make some adjustments in your files.

  • Update your docker-compose.yml, use the reference docker compose from localstack, you can check it here.

  • Use a template that works correctly, AWS docs page have several examples, you can check it here.

  • Run it with next command aws cloudformation create-stack --endpoint-url http://localhost:4566 --stack-name samplestack --template-body file://lambda.yml --profile dev

You can also run localstack using Python with next commands

pip install localstack 
localstack start
Related