Learn how to securely set environment variables on your AWS instances using Terraform, ensuring consistency and simplifying configuration management.
Managing environment variables is crucial for configuring applications and services running on AWS EC2 instances. This guide explores different methods to effectively set environment variables, providing flexibility and security for your deployments. We'll cover direct instance configuration, leveraging infrastructure-as-code tools like Terraform, and utilizing AWS services like Systems Manager Parameter Store and Secrets Manager.
Directly on the instance:
~/.bashrc
, ~/.zshrc
).export MY_VARIABLE="my_value"
.source ~/.bashrc
.Using Terraform:
AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
before running Terraform.variable "instance_name" {
type = string
default = "my-instance"
}
resource "aws_instance" "example" {
tags = {
Name = var.instance_name
}
}
Other methods:
Remember to choose the method that best suits your needs and security requirements.
This code provides examples of setting environment variables for AWS instances using various methods. It demonstrates setting variables directly on an instance via SSH and modifying shell startup files. It also shows how to use Terraform to set variables during instance launch and manage them within Terraform code. Additionally, it includes examples of using AWS Systems Manager Parameter Store and AWS Secrets Manager to store and retrieve environment variables and secrets securely. The code snippets cover Bash, Terraform, and Python, emphasizing the need to adapt the examples to specific use cases and programming languages while prioritizing security best practices.
This guide provides code examples for the methods mentioned in the article to set environment variables for your AWS instances.
1. Directly on the Instance:
# Connect to your instance via SSH
ssh user@your-instance-ip
# Open your shell startup file (e.g., ~/.bashrc)
nano ~/.bashrc
# Add your environment variable
export MY_VARIABLE="my_value"
# Save the file and reload the shell environment
source ~/.bashrc
# Verify the variable is set
echo $MY_VARIABLE
2. Using Terraform:
a) Provider Configuration:
Set environment variables like AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
in your terminal before running Terraform commands.
export AWS_ACCESS_KEY_ID="YOUR_ACCESS_KEY"
export AWS_SECRET_ACCESS_KEY="YOUR_SECRET_KEY"
b) Within Terraform Code:
# Define variables
variable "instance_name" {
type = string
default = "my-instance"
}
variable "my_variable" {
type = string
default = "my_value"
}
# Launch an EC2 instance
resource "aws_instance" "example" {
ami = "ami-0c55b31ad2299a761" # Replace with your desired AMI
instance_type = "t2.micro"
# Access variables within resources
tags = {
Name = var.instance_name
}
# Use user_data to set environment variable on instance launch
user_data = <<-EOF
#!/bin/bash
echo "export MY_VARIABLE='${var.my_variable}'" >> /etc/environment
EOF
}
3. Other Methods:
a) AWS Systems Manager Parameter Store:
import boto3
# Create a SSM client
ssm = boto3.client('ssm')
# Set a parameter
response = ssm.put_parameter(
Name='my-parameter',
Value='my-value',
Type='String',
)
# Retrieve a parameter
response = ssm.get_parameter(
Name='my-parameter',
WithDecryption=False
)
# Access the parameter value
parameter_value = response['Parameter']['Value']
# Use the parameter value in your application
print(f"My parameter value: {parameter_value}")
b) AWS Secrets Manager:
import boto3
# Create a Secrets Manager client
secrets_client = boto3.client('secretsmanager')
# Create a secret
response = secrets_client.create_secret(
Name='my-secret',
SecretString='{"username": "myuser", "password": "mypassword"}'
)
# Retrieve a secret
response = secrets_client.get_secret_value(
SecretId='my-secret'
)
# Access the secret value
secret_value = json.loads(response['SecretString'])
# Use the secret values in your application
print(f"Username: {secret_value['username']}")
print(f"Password: {secret_value['password']}")
Remember to adapt these examples to your specific needs and programming language. Always prioritize security best practices when handling sensitive information like environment variables and secrets.
General Considerations:
Directly on the Instance:
~/.bashrc
, ~/.zshrc
, etc.) depends on the shell you are using. Ensure you are editing the correct file for your environment./etc/environment
or /etc/profile
. These files affect all users and sessions.Using Terraform:
user_data
script allows you to execute commands on instance launch, including setting environment variables.Other Methods:
Best Practices:
This article outlines different methods for setting environment variables on AWS, catering to various use cases and security needs:
1. Directly on the Instance (Manual):
~/.bashrc
) and add export VARIABLE="value"
.2. Using Terraform (Infrastructure as Code):
3. Other AWS Services:
The article emphasizes choosing the method that aligns with your specific requirements and prioritizes security best practices.
By understanding and implementing these methods, you can effectively manage environment variables for your AWS EC2 instances, ensuring your applications have the necessary configurations and secrets to operate securely and reliably. Remember to prioritize security, persistence, and scope when working with environment variables, and always follow best practices for documentation and consistency.