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:
- 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"
- 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.