[파이썬] AWS Lambda 환경 변수 관리

AWS Lambda is a popular serverless computing platform offered by Amazon Web Services (AWS). It allows you to run your code without provisioning or managing servers. AWS Lambda functions can be written using various programming languages, including Python.

In this blog post, we will focus on how to manage environment variables in an AWS Lambda function written in Python. Environment variables are a fundamental aspect of Lambda functions as they allow you to separate configuration settings from your code. This practice enables better flexibility and security.

Accessing Environment Variables in AWS Lambda

To access environment variables in an AWS Lambda function, you can use the os module provided by Python’s standard library. The os.environ dictionary contains the environment variables accessible within the Lambda function.

Here’s an example of how you can access environment variables in Python:

import os

def lambda_handler(event, context):
    # Accessing environment variable
    api_key = os.environ.get('API_KEY')

    # Use the environment variable in your code
    response = my_api_call(api_key)

    return {
        'statusCode': 200,
        'body': response
    }

In the example above, we use the os.environ.get() method to access the value of the API_KEY environment variable. You can replace 'API_KEY' with the name of the environment variable you want to access.

Setting Environment Variables in AWS Lambda

To set environment variables in an AWS Lambda function, you can use the AWS Management Console, AWS CLI, or AWS SDKs.

AWS Management Console

  1. Open the AWS Management Console and navigate to the Lambda service.
  2. Select your Lambda function.
  3. Scroll down to the Environment variables section.
  4. Click on Edit.
  5. Add or modify the environment variables you want to set.
  6. Click Save.

AWS CLI

You can use the AWS CLI to update environment variables for your Lambda functions. Here’s an example command:

aws lambda update-function-configuration --function-name my-function --environment Variables={API_KEY=xyz123}

AWS SDKs

If you prefer using AWS SDKs, you can programmatically set environment variables for your Lambda function. The specific method may vary depending on the SDK you are using.

For example, in Boto3 (the AWS SDK for Python), you can use the update_function_configuration() method to update the environment variables:

import boto3

lambda_client = boto3.client('lambda')

response = lambda_client.update_function_configuration(
    FunctionName='my-function',
    Environment={
        'Variables': {
            'API_KEY': 'xyz123'
        }
    }
)

Make sure to replace 'my-function' with the name of your Lambda function and 'xyz123' with the desired value for the API_KEY environment variable.

Conclusion

Managing environment variables is crucial for building flexible and secure AWS Lambda functions in Python. By keeping sensitive configuration settings separate from your code, you can easily update and manage these variables without modifying your application’s logic.

In this blog post, we covered how to access and set environment variables in an AWS Lambda function written in Python. With this knowledge, you can leverage the power of environment variables to configure your Lambda functions more effectively.