Search code examples
amazon-web-serviceshostname

How to set EC2 hostname programmatically (from outside the instance)?


This page in the docs describes how to set the hostname of an EC2 instance by logging in and running:

sudo hostnamectl set-hostname webserver.mydomain.com
sudo reboot

Is there a way to do this programmatically from outside the instance? I have multiple instances that I need to do this for and logging into each one is not possible.

I'm using Boto3 but can't find the right command.


Solution

  • Usually you would use AWS Systems Manager Run Command for such tasks:

    AWS Systems Manager Run Command lets you remotely and securely manage the configuration of your managed instances. Run Command enables you to automate common administrative tasks and perform ad hoc configuration changes at scale.

    This would require making your instances to be recognized by AWS Systems Manager (SSM) which requires three things:

    • network connectivity to SSM service.
    • SSM Agent installed and running - common official AMI (ubuntu, amazon linux, ...) already have it running and setup.
    • instance role with AmazonSSMManagedInstanceCore managed policy