3.3.3. Managing a RHEL VM on AWS by using the command line
You can use the Amazon Web Services (AWS) command line interface awscli2 utility to manage a Red Hat Enterprise Linux (RHEL) Elastic Cloud Compute (EC2) VM on AWS through the command line. You can import a RHEL EC2 image snapshot, create an Amazon Machine Image (AMI), launch, and connect to a RHEL EC2 instance.
Prerequisites
- You have created a Red Hat account.
- You have signed up and set up an AWS account.
-
You have installed the
awscli2package and configured AWS credentials for your account.
Procedure
Use the
vmimportrole:$ aws iam create-role --role-name vmimport \ --assume-role-policy-document file://<example_vmimport_trust_policy_file>Embed an inline policy document by using the
vmimportrole policy file:$ aws iam put-role-policy --role-name vmimport \ --policy-name vmimport-access \ --policy-document file://<example_vmimport_role_policy_file>Also, you can grant VM Import/Export permissions by creating an IAM role (often named
vmimport) with a trust policy forvmie.amazonaws.comand attaching the JSON policy from the AWS guide.Import RHEL image as a snapshot:
$ aws ec2 import-snapshot --disk-container file://<example_containers_json_file>Confirm that the snapshot import finished successfully with the
completedstatus:$ aws ec2 describe-import-snapshot-tasks --import-task-ids <example_import_task_id>You can import a RHEL VM image from Amazon S3 as a snapshot to Amazon EC2. Prepare a
<example_containers_json_file>file that points to your S3 bucket and object key, then start the import and monitor the task ID that the command returns.Register a RHEL AMI from the completed snapshot:
$ aws ec2 register-image \ --name <example_rhel_custom_ami_name> \ --architecture x86_64 \ --virtualization-type hvm \ --root-device-name /dev/sda1 \ --block-device-mappings "[{\"DeviceName\": \"/dev/sda1\",\"Ebs\": {\"SnapshotId\": \"<example_snapshot_id>\"}}]" \ --ena-supportCreate a key pair for the RHEL EC2 instance:
$ aws ec2 create-key-pair --key-name <example_key_pair_name> \ --query 'KeyMaterial' --output text > <example_key_file>.pemThis key pair is per region and save a
.pemfile.Restrict permissions on the key file:
$ chmod 400 <example_key>.pemLaunch a RHEL EC2 instance from the registered AMI:
$ aws ec2 run-instances \ --image-id <example_registered_ami_id> \ --instance-type <example_instance_type> \ --key-name <example_key_pair_name>Connect to the RHEL EC2 instance where the default user is
ec2-user:$ ssh -i <example_key>.pem ec2-user@<instance_public_dns_or_ip>
Verification
Verify that the snapshot import finished successfully with the
completedstatus:$ aws ec2 describe-import-snapshot-tasks --import-task-ids <example_import_task_id>Verify that the AMI is available with the
availablestate:$ aws ec2 describe-images --image-ids <example_registered_ami_id>Verify that the instance is running with the
runningstate:$ aws ec2 describe-instances --instance-ids <example_instance_id>The
State.Namevalue isrunningwhen the instance has finished starting.Verify that the SSH connection works:
$ ssh -i <example_key>.pem ec2-user@<instance_public_dns_or_ip> 'echo connected'