DataDog / datadog-cloudformation-macro

CloudFormation Macros by Datadog
Apache License 2.0
14 stars 22 forks source link

sam local invoke fails to find ddtrace module #91

Closed cscetbon closed 1 year ago

cscetbon commented 1 year ago

Expected Behavior

sam local invoke should work and not raise an exception about not finding ddtrace module

Actual Behavior

sam local invoke fails because the lambda cannot find ddtrace module

Steps to Reproduce the Problem

  1. sam init
  2. template.yaml content:
    
    AWSTemplateFormatVersion: '2010-09-09'
    Transform:
    - AWS::Serverless-2016-10-31
    - Name: DatadogServerless
    Parameters:
     pythonLayerVersion: 72
      stackName: datadog-serverless-macro
      service: my-service
      env: !Ref StageName

Parameters: StageName: Type: String Default: dev

Resources: MyFunction: Type: AWS::Serverless::Function Properties: CodeUri: src/ Handler: app.lambda_handler Runtime: python3.8

  3. My `src/app.py` contains a simple dummy handler(not relevant)

from ddtrace import tracer

@tracer.wrap() def lambda_handler(event, context): print('Hello')


## Specifications

  For the Datadog Serverless Macro:
  - Deployment method and version (CloudFormation, SAM, CDK): sam 1.80.0
  - Datadog CloudFormation Macro version: 72
  - Lambda function runtime (Python 3.7, Node 10, etc.): python3.8

## Stacktrace

$ sam build Building codeuri: /Users/cscetbon/Downloads/test/src runtime: python3.8 metadata: {} architecture: x86_64 functions: MyFunction requirements.txt file not found. Continuing the build without dependencies. Running PythonPipBuilder:CopySource

Build Succeeded

Built Artifacts : .aws-sam/build Built Template : .aws-sam/build/template.yaml

Commands you can use next

[] Validate SAM template: sam validate [] Invoke Function: sam local invoke [] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch [] Deploy: sam deploy --guided

$ sam local invoke MyFunction Invoking app.lambda_handler (python3.8) Local image is up-to-date Using local image: public.ecr.aws/lambda/python:3.8-rapid-x86_64.

Mounting /Users/cscetbon/Downloads/test/.aws-sam/build/MyFunction as /var/task:ro,delegated, inside runtime container START RequestId: 02b5c265-97ca-46bf-ae62-7791b34ad305 Version: $LATEST Traceback (most recent call last): Unable to import module 'app': No module named 'ddtrace' END RequestId: 02b5c265-97ca-46bf-ae62-7791b34ad305 REPORT RequestId: 02b5c265-97ca-46bf-ae62-7791b34ad305 Init Duration: 1.09 ms Duration: 601.14 ms Billed Duration: 602 ms Memory Size: 128 MB Max Memory Used: 128 MB {"errorMessage": "Unable to import module 'app': No module named 'ddtrace'", "errorType": "Runtime.ImportModuleError", "stackTrace": []}%

sfirrin commented 1 year ago

Hi @cscetbon, thanks for opening the issue. Reading the newer posts in your thread on the Datadog Slack with AJ, it appears that because CFN macros operate on stacks in AWS there's not a straightforward way to apply a macro transform locally for aws sam invoke. It looks like the workarounds AJ mentioned in the thread are currently the best option for this