如何在EC2 Bootstrap脚本中安全传递应用安装所需密码?
Great question—passing sensitive data like passwords in EC2 user data/bootstrap scripts is a common pitfall, and you’re right to prioritize security here. Here are the most secure methods, ordered by best practice:
1. AWS Secrets Manager (Top Recommendation)
This is the gold standard for managing sensitive credentials in AWS, as it’s purpose-built for secret storage, automated rotation, and fine-grained access control.
- How to implement:
- Store your application’s password as a secret in Secrets Manager.
- Attach an IAM instance profile to your EC2 instance that includes permission to retrieve this specific secret (use the
secretsmanager:GetSecretValueaction). - In your bootstrap script, fetch the password using the AWS CLI:
SECRET=$(aws secretsmanager get-secret-value --secret-id your-app-password-secret --query SecretString --output text) # If your secret is stored as JSON, extract the password field with jq PASSWORD=$(echo $SECRET | jq -r '.password') # Use $PASSWORD in your application installation command
- Why it’s safe: The password never appears in plaintext in your user data, instance logs, or AWS console. You can also set up automatic password rotation without updating your bootstrap script.
2. AWS Systems Manager Parameter Store (Secure String Type)
If you don’t need the full feature set of Secrets Manager (like automatic rotation), Parameter Store’s Secure String type is a cost-effective, secure alternative.
- How to implement:
- Create a
Secure Stringparameter in Parameter Store, encrypting it with an AWS KMS key. - Grant your EC2 instance’s IAM profile permission to retrieve the parameter (
ssm:GetParameteraction, plus permission to decrypt using the KMS key). - Fetch the password in your bootstrap script:
PASSWORD=$(aws ssm get-parameter --name /your/app/password --with-decryption --query Parameter.Value --output text) # Use $PASSWORD for your installation steps
- Create a
- Why it’s safe: The parameter is encrypted at rest and in transit, and access is strictly controlled via IAM policies.
3. KMS-Encrypted Password in User Data (Fallback Option)
If you can’t use Secrets Manager or Parameter Store, you can encrypt the password locally and include the ciphertext in your user data, then decrypt it on the instance using KMS.
- How to implement:
- Encrypt your password locally with a KMS key you control:
ENCRYPTED_PASSWORD=$(aws kms encrypt --key-id your-kms-key-id --plaintext "your-plaintext-password" --query CiphertextBlob --output text) - Include the
ENCRYPTED_PASSWORDvalue in your bootstrap script. - On the instance, decrypt it using the AWS CLI (ensure the instance profile has
kms:Decryptpermission for the key):PASSWORD=$(aws kms decrypt --ciphertext-blob fileb://<(echo $ENCRYPTED_PASSWORD | base64 -d) --query Plaintext --output text)
- Encrypt your password locally with a KMS key you control:
- Note: This is less ideal than the first two methods because the ciphertext is still present in your user data, but it’s far better than using a plaintext password.
Critical: What to Avoid
Never embed a plaintext password directly in your bootstrap script or EC2 user data. User data is accessible via the instance metadata service, visible in AWS CloudTrail logs, and can be retrieved by anyone with access to the instance—this is a major security risk.
内容的提问来源于stack exchange,提问作者Pankaj_Pandav

