Description
Real AWS keys in EC2 user_data can be read by anyone with access to the playbook or instance user data. First check whether the value is an actual credential.
Potential impact
Someone with a valid access key ID and its secret can perform the AWS actions the key allows. Temporary credentials also require the session token.
Remediation
Attach a least-privilege role through an instance profile. Remove the static credential exports from user_data and any copies in the workload's environment so the AWS SDK can use the role's temporary credentials. If a real key was exposed, deactivate it promptly, switch its consumers, and verify they work. Review AWS activity and copies in repository history, user data, and deployment records.
Examples
Supply the AMI, instance type, and subnet for your environment. The deployment identity needs permission to attach the profile and pass the role.
Before
- name: start an instance
community.aws.ec2_instance:
name: credential-example
image_id: "{{ ami_id }}"
instance_type: "{{ instance_type }}"
vpc_subnet_id: "{{ subnet_id }}"
user_data: |
#!/bin/bash
export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
The startup script exports a public example key pair from the AWS documentation.
After
- name: start an instance
community.aws.ec2_instance:
name: credential-example
image_id: "{{ ami_id }}"
instance_type: "{{ instance_type }}"
vpc_subnet_id: "{{ subnet_id }}"
iam_instance_profile: "{{ instance_profile_name }}"
iam_instance_profile selects the instance profile containing the role.