- Newest
- Most votes
- Most comments
You could get the original username and then in the Userdata create a file somewhere that contains that detail for "later" use - alternately, have the Userdata create the mount script above.
Another method would be to put the username into a tag for the instance - noting that tags are now available via the instance metadata service which is quite handy.
However, I think your question is actually "how can I get the username of the IAM user who is launching the instance". That's a little trickier. If you're launching in the console, there's no automatic way of doing that - the user would have to create the Userdata or tag appropriately.
If you're launching via some sort of automation (say, a script) then it should be somewhat easier to determine the username. For example, using the AWS CLI you can do aws iam get-user
.
If you're launching via CloudFormation then you'd need to have a custom resource.
Relevant content
- Accepted Answerasked 6 months ago
- asked 2 years ago
- asked 3 years ago
- AWS OFFICIALUpdated 4 months ago
- AWS OFFICIALUpdated 6 months ago
- AWS OFFICIALUpdated 6 months ago
- AWS OFFICIALUpdated a year ago
Many tks for the comments. Yes I'm launching via scripts (Terraform to be precise), however I don't have AWS CLI on the (mac) from which I'm executing the Terraform script; and the Terraform 'aws_iam_user' Data Source requires a username to be supplied (which seemingly can't be determined from my aws credentials file). Your answer made me think I could user 'aws iam get-user' within the UserData but of course, aws credentials will not have been setup at instance launch time (there will only be my key in authorised_key file). (I don't really want to pass in credentials via Terraform either).Hmm