0

new to AWS and sam

notice my sam template.yaml below.

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
  backend

Parameters:  
  DEBUG:
    Type: String
    AllowedValues: ["true", "false"]
    Default: "false"
    Description: "debug mode"
  DEV:
    Type: String
    AllowedValues: ["true", "false"]
    Default: "false"
    Description: "dev mode"

Conditions:
  IsDev: !Equals [!Ref DEV, "true"]

Globals:
  Function:
    Timeout: 900

    Tracing: Active
  Api:
    TracingEnabled: true

Resources:
  API:
    Type: AWS::Serverless::Api
    Properties:
      StageName: !If [IsDev, "dev", "prod"]
      Cors:
        AllowMethods: "'OPTIONS,POST'"
        AllowHeaders: "'*'"
        AllowOrigin: "'*'"
      Auth:
        DefaultAuthorizer: AuthFunction
        Authorizers:
          AuthFunction:
            FunctionArn: !GetAtt AuthFunction.Arn
            Identity:
              Header: Authorization

  CoreFunction:
    Type: AWS::Serverless::Function
    Properties:
      Architectures:
        - x86_64
      Events:
        Core:
          Type: Api
          Properties:
            RestApiId: !Ref API
            Path: /test
            Method: post
    Environment:
      Variables:
        DEBUG: !Ref DEBUG

  AuthFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: authorizer/
      Handler: app.lambda_handler
      Runtime: python3.13
      Architectures:
        - x86_64
    Environment:
      Variables:
        DEBUG: !Ref DEBUG

I'm overriding the debug when deploying using the following:

sam deploy --parameter-overrides DEBUG="true" DEV="false"

I've two questions:

  1. I'm seeing that the parameter is set in cloudformation but when I log/print the enviromental variable in my python code it is not loaded. when I check the env variables under configuration in my lambda console it is empty too.
import os
import requests

def lambda_handler(event, context):
    DEBUG = os.environ.get("DEBUG", "not loaded").lower() == "true"

    method_arn = event.get("methodArn")
    token = extract_bearer_token(event.get("authorizationToken"))

    print("env variables:", DEBUG)

DEBUG is "not loaded"

  1. is there a better way to deploy different stages from my console since deploying like this would replace the apigateway I think

Edit:

found the solution for the first question. the Environment property should be under properties.

4
  • Unclear how DEBUG is "not loaded". It seems to be assigned a boolean value in your code. Commented Jul 31 at 14:37
  • 1
    To debug, I'd add another environment variable like FRED=123, modify the code to print out all environment variables, and then re-deploy/re-invoke. Commented Jul 31 at 14:43
  • 1
    found the solution. The environment field was supposed to be under properties. Commented Jul 31 at 17:23
  • One of the great frustrations of YAML (and JSON to a lesser extent). Parsers really need to error out when they see keys at a level that doesn't match the schema. Commented Jul 31 at 18:55

1 Answer 1

0

You have already identified the root cause for the first issue: the Environment property must be under the Properties section of the Lambda resource in your SAM template. In your CoreFunction resource, the Environment is currently outside of Properties, so SAM doesn't apply it.

Here’s the corrected YAML for your CoreFunction:

yaml

CoreFunction:
  Type: AWS::Serverless::Function
  Properties:
    Architectures:
      - x86_64
    Events:
      Core:
        Type: Api
        Properties:
          RestApiId: !Ref API
          Path: /test
          Method: post
    Environment:
      Variables:
        DEBUG: !Ref DEBUG

Why was your environment variable not loaded?

In AWS SAM, the Environment must be under Properties.

Since it was misplaced, SAM ignored it, resulting in no environment variables being set in Lambda.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.