localstack / localstack

💻 A fully functional local AWS cloud stack. Develop and test your cloud & Serverless apps offline
https://localstack.cloud
Other
55.95k stars 3.98k forks source link

bug: Create lambda from AWS CLI donest work if using layers #6475

Closed parisnakitakejser closed 2 years ago

parisnakitakejser commented 2 years ago

Is there an existing issue for this?

Current Behavior

When I create a lambda function there should use layers eg. Redis which returns back with an error.

An error occurred (InternalFailure) when calling the CreateFunction operation: Unable to get handler function from lambda code: No module named 'redis'

Have just downloaded a working layer from my AWS account as zip and trying to use it for LocalStack testing

Expected Behavior

I expect when i have created my Lambda Layers from a zip file, my role and then my Lambda function its should be created so i can start invoke my Lambda function.

How are you starting LocalStack?

With a docker run command

Steps To Reproduce

AWS CLI commands

awslocal iam create-role \
    --role-name lambda-execute-role \
    --assume-role-policy-document file://Test-Role-Trust-Policy.json

awslocal lambda publish-layer-version \
    --layer-name redis \
    --compatible-runtimes python3.9 \
    --zip-file fileb://./redis.zip

awslocal lambda create-function \
    --function-name test-lambda \
    --role lambda-execute-role \
    --runtime python3.9 \
    --package-type Zip \
    --layers "arn:aws:lambda:eu-central-1:000000000000:layer:redis:1" \
    --handler lambda_handler.handler \
    --zip-file fileb://./lambda-test.zip

lambda-test.zip -> lambda_handler.py:

import redis

def handler(event, context):
    print(event)

redis.zip

pip install redis -t python
{zip it}

Test-Role-Trust-Policy.json

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudformation:DescribeChangeSet",
                "cloudformation:DescribeStackResources",
                "cloudformation:DescribeStacks",
                "cloudformation:GetTemplate",
                "cloudformation:ListStackResources",
                "cloudwatch:*",
                "cognito-identity:ListIdentityPools",
                "cognito-sync:GetCognitoEvents",
                "cognito-sync:SetCognitoEvents",
                "dynamodb:*",
                "ec2:DescribeSecurityGroups",
                "ec2:DescribeSubnets",
                "ec2:DescribeVpcs",
                "events:*",
                "iam:GetPolicy",
                "iam:GetPolicyVersion",
                "iam:GetRole",
                "iam:GetRolePolicy",
                "iam:ListAttachedRolePolicies",
                "iam:ListRolePolicies",
                "iam:ListRoles",
                "iam:PassRole",
                "iot:AttachPrincipalPolicy",
                "iot:AttachThingPrincipal",
                "iot:CreateKeysAndCertificate",
                "iot:CreatePolicy",
                "iot:CreateThing",
                "iot:CreateTopicRule",
                "iot:DescribeEndpoint",
                "iot:GetTopicRule",
                "iot:ListPolicies",
                "iot:ListThings",
                "iot:ListTopicRules",
                "iot:ReplaceTopicRule",
                "kinesis:DescribeStream",
                "kinesis:ListStreams",
                "kinesis:PutRecord",
                "kms:ListAliases",
                "lambda:*",
                "logs:*",
                "s3:*",
                "sns:ListSubscriptions",
                "sns:ListSubscriptionsByTopic",
                "sns:ListTopics",
                "sns:Publish",
                "sns:Subscribe",
                "sns:Unsubscribe",
                "sqs:ListQueues",
                "sqs:SendMessage",
                "tag:GetResources",
                "xray:PutTelemetryRecords",
                "xray:PutTraceSegments"
            ],
            "Resource": "*"
        }
    ]
}

Environment

- OS: macOS 12.4
- LocalStack: 1.0.1.dev

Anything else?

No response

dfangl commented 2 years ago

Hi @parisnakitakejser ! It seems, LocalStack is using the local executor, which does not support lambda layers. You can find information about different executor modes here: https://docs.localstack.cloud/localstack/lambda-executors/

Please try setting LAMBDA_EXECUTOR=docker and, if this is currently not the case, mount the docker socket into the LocalStack container, so it may spawn the containers for lambda execution.

parisnakitakejser commented 2 years ago

Perfect, it's working mount the socket + change the lambda_executor are working, feedback in the awslocal wrapper to sending better error handling return, don't know about this ticket should be closed or open for that?

HarshCasper commented 2 years ago

Hi @parisnakitakejser — Closing this for now. Can you please send this feedback on our Discussion Pages to help us better prioritise it: https://discuss.localstack.cloud/