Bird
Raised Fist0
AWScloud~5 mins

Environment variables in Lambda in AWS - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes your Lambda function needs extra information to work, like passwords or settings. Environment variables let you store this information safely outside your code so you can change it without updating the function itself.
When you want to keep database passwords out of your Lambda code for security.
When you need to change API keys without redeploying your Lambda function.
When you want to configure different settings for development and production environments.
When you want to pass configuration values like region or log level to your Lambda function.
When you want to avoid hardcoding values that might change over time.
Config File - lambda-environment.json
lambda-environment.json
{
  "Variables": {
    "DB_HOST": "database.example.com",
    "DB_USER": "admin",
    "DB_PASS": "securepassword123",
    "LOG_LEVEL": "info"
  }
}

This JSON file sets environment variables for the Lambda function named example-lambda. The Variables section contains key-value pairs your function can use. For example, DB_HOST tells the function where the database is, and LOG_LEVEL controls how much detail the function logs.

Commands
This command updates the Lambda function's environment variables using the JSON file. It applies the new settings without changing the function code.
Terminal
aws lambda update-function-configuration --function-name example-lambda --environment file://lambda-environment.json
Expected OutputExpected
{ "FunctionName": "example-lambda", "Environment": { "Variables": { "DB_HOST": "database.example.com", "DB_USER": "admin", "DB_PASS": "securepassword123", "LOG_LEVEL": "info" } }, "ResponseMetadata": { "RequestId": "1234abcd-56ef-78gh-90ij-klmnopqrstuv", "HTTPStatusCode": 200 } }
→
--function-name - Specifies which Lambda function to update
→
--environment - Provides the environment variables configuration
This command retrieves the current configuration of the Lambda function, including the environment variables, to verify the update.
Terminal
aws lambda get-function-configuration --function-name example-lambda
Expected OutputExpected
{ "FunctionName": "example-lambda", "Environment": { "Variables": { "DB_HOST": "database.example.com", "DB_USER": "admin", "DB_PASS": "securepassword123", "LOG_LEVEL": "info" } }, "Runtime": "python3.9", "Handler": "lambda_function.lambda_handler", "Role": "arn:aws:iam::123456789012:role/lambda-role" }
→
--function-name - Specifies which Lambda function to show
Key Concept

If you remember nothing else from this pattern, remember: environment variables let you safely pass configuration and secrets to your Lambda function without changing its code.

Common Mistakes
Hardcoding sensitive information like passwords directly in the Lambda code.
This makes it hard to update secrets and risks exposing them if the code is shared.
Use environment variables to store sensitive data and update them separately from the code.
Forgetting to update the Lambda function configuration after changing the environment variables file.
The function will keep using old values until the configuration is updated.
Always run the update-function-configuration command after changing environment variables.
Exposing sensitive environment variables in logs or error messages.
This can leak secrets and cause security issues.
Avoid printing sensitive environment variables in logs or handle them carefully.
Summary
Use a JSON file to define environment variables for your Lambda function.
Update the Lambda function configuration with the AWS CLI to apply environment variables.
Verify the environment variables are set correctly by retrieving the function configuration.

Practice

(1/5)
1. What is the main purpose of environment variables in an AWS Lambda function?
easy
A. To store configuration settings outside the code for flexibility
B. To write logs directly to CloudWatch
C. To increase the memory allocated to the Lambda function
D. To define the runtime language of the Lambda function

Solution

  1. Step 1: Understand environment variables role

    Environment variables hold configuration data separate from code, making functions flexible.
  2. Step 2: Compare with other options

    Logging, memory, and runtime are unrelated to environment variables.
  3. Final Answer:

    To store configuration settings outside the code for flexibility -> Option A
  4. Quick Check:

    Environment variables = configuration outside code [OK]
Hint: Environment variables hold config, not code or logs [OK]
Common Mistakes:
  • Confusing environment variables with logging settings
  • Thinking environment variables control memory or runtime
  • Assuming environment variables store code
2. Which of the following is the correct way to define an environment variable named API_KEY with value 12345 in AWS Lambda console?
easy
A. Create a CloudWatch alarm named API_KEY with threshold 12345
B. Add a tag with key API_KEY and value 12345
C. Write export API_KEY=12345 in the Lambda function code
D. Set key as API_KEY and value as 12345 in the Environment Variables section

Solution

  1. Step 1: Identify environment variable setup

    Environment variables are set as key-value pairs in the Lambda console's Environment Variables section.
  2. Step 2: Eliminate incorrect options

    Tags, code exports, and CloudWatch alarms do not define environment variables.
  3. Final Answer:

    Set key as API_KEY and value as 12345 in the Environment Variables section -> Option D
  4. Quick Check:

    Environment variables = key-value pairs in Lambda console [OK]
Hint: Set env vars in Lambda console's Environment Variables section [OK]
Common Mistakes:
  • Confusing tags with environment variables
  • Trying to set env vars inside code with export
  • Using unrelated AWS features like CloudWatch alarms
3. Given this Python Lambda function code:
import os
def lambda_handler(event, context):
    return os.environ.get('MODE', 'default')

If the environment variable MODE is set to production, what will the function return?
medium
A. "default"
B. None
C. "production"
D. An error occurs

Solution

  1. Step 1: Understand os.environ.get usage

    os.environ.get('MODE', 'default') returns the value of MODE if set, else 'default'.
  2. Step 2: Apply given environment variable

    Since MODE is set to 'production', the function returns 'production'.
  3. Final Answer:

    "production" -> Option C
  4. Quick Check:

    Env var MODE='production' returns 'production' [OK]
Hint: os.environ.get returns env var value or default if missing [OK]
Common Mistakes:
  • Assuming default is always returned
  • Expecting None if variable exists
  • Thinking code throws error if env var exists
4. A developer sets an environment variable DB_PASSWORD in Lambda console but the function code returns None when accessing it. What is the most likely cause?
medium
A. Lambda function memory is too low
B. The environment variable key is misspelled in the code
C. The environment variable value is encrypted and inaccessible
D. The Lambda function timeout is too short

Solution

  1. Step 1: Check environment variable access

    If code returns None, likely the key used in code does not match the set key.
  2. Step 2: Exclude unrelated causes

    Memory size, encryption, or timeout do not cause None return for env var access.
  3. Final Answer:

    The environment variable key is misspelled in the code -> Option B
  4. Quick Check:

    Misspelled env var key returns None [OK]
Hint: Check spelling of env var keys in code vs console [OK]
Common Mistakes:
  • Blaming memory or timeout for env var issues
  • Assuming encryption blocks access without decryption
  • Not verifying exact key names
5. You want to securely store a database password in a Lambda environment variable but avoid exposing it in plain text. Which approach is best practice?
hard
A. Use AWS Lambda environment variables with encryption enabled and grant decryption permissions
B. Hardcode the password directly in the Lambda function code
C. Store the password in a public S3 bucket and read it at runtime
D. Send the password as a query parameter in API requests

Solution

  1. Step 1: Understand secure storage options

    Lambda environment variables support encryption with AWS KMS and access control.
  2. Step 2: Evaluate other options

    Hardcoding, public S3, or sending passwords in query parameters expose secrets and are insecure.
  3. Final Answer:

    Use AWS Lambda environment variables with encryption enabled and grant decryption permissions -> Option A
  4. Quick Check:

    Encrypted env vars + permissions = secure secret storage [OK]
Hint: Encrypt env vars and control access for secrets [OK]
Common Mistakes:
  • Hardcoding secrets in code
  • Using public storage for passwords
  • Exposing secrets in API requests
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard