Using IAM Roles for EC2
The most secure way to provide credentials to an EC2 instance is by using IAM Roles. You should never hardcode AWS Access Keys or Secret Keys within your application code or configuration files. Instead, you attach an IAM Role to the instance, and the AWS SDKs automatically fetch temporary credentials from the Instance Metadata Service (IMDS).
Automatic credential rotation performed by AWS.
No long-term secrets stored on the instance disk.
Easily managed via IAM policies and central identity management.