DataDog / datadog-cloudformation-macro

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

DD_ENV doesn't get set from env parameter #92

Closed cscetbon closed 1 year ago

cscetbon commented 1 year ago

Expected Behavior

DD_ENV should be set from env parameter

Actual Behavior

DD_ENV is not set and no tags are added as well to the lambdas

Steps to Reproduce the Problem

sam init 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

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

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
$ 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
$ aws lambda get-function --function-name xxxxx-MyFunction-xxxx
{
            "FunctionName": "xxxxx-MyFunction-xxxx",
            ...
            "Runtime": "python3.8",
            "Handler": "datadog_lambda.handler.handler",
            "CodeSize": 615780,
            "Description": "",
            "Timeout": 900,
            "MemorySize": 1024,
            "LastModified": "2023-05-12T13:59:08.000+0000",
            "CodeSha256": "rBS5z16SwlMo+i6VAn7BV6dM3Z1bBHv54n1zzcQhBSE=",
            "Version": "$LATEST",
...
            },
            "Environment": {
                "Variables": {
                    "DD_LAMBDA_HANDLER": "handler.handler",
                    "DD_CAPTURE_LAMBDA_PAYLOAD": "false",
                    "DD_FLUSH_TO_LOG": "true",
                    "DD_TRACE_ENABLED": "true",
                    "ENV_NAME": "stg",
                    "DD_SERVERLESS_LOGS_ENABLED": "true",
                    "DD_ENHANCED_METRICS": "true",
                    "DD_MERGE_XRAY_TRACES": "false"
                }
...
"Tags": {
...
        "dd_sls_macro_by": "SAM",
        "dd_sls_macro": "v0.5.2",
...
    }
}

You can see that DD_ENV is missing

sfirrin commented 1 year ago

Hi @cscetbon thanks for opening this and including all of these details

I had a Node SAM app handy and tried to reproduce with that and your example code but my function got the DD_ENV set to dev as expected. I suspect there is something unexpected caused by the !Ref but need to dig in more. I'm going to try again with Python and will update you here

sfirrin commented 1 year ago

Trying again with Python I am able to reproduce. Currently looking into the fix and will link here when it's ready

sfirrin commented 1 year ago

Hi @cscetbon this should be fixed in the latest release https://github.com/DataDog/datadog-cloudformation-macro/releases/tag/serverless-macro-0.7.2

Thanks again for opening and please let us know if you run into any other issue