Search code examples
amazon-web-servicesamazon-ec2sshamazon-ami

Does create-ami command support sshkey of EBS backed instance?


AMI is provided by AWS

In the below command, we are creating an ec2 instance using SOME_OLD_AMI:

export EC2_INSTANCE_ID=$(aws ec2 run-instances --image-id ${SOME_OLD_AMI} --key-name ${SOMEKEY}

and then stopping it:

aws ec2 stop-instances --instance-ids ${EC2_INSTANCE_ID} --region ${REGION}


Now, we are customizing an ami with this stopped instance:

       aws ec2 create-image --instance-id ${EC2_INSTANCE_ID} --name ${SOME_NEW_AMI} --description "xyz"

Can we ssh EC2 instance(launched using SOME_NEW_AMI) using private key(SOMEKEY)? because ssh key was assigned to EC2_INSTANCE_ID....


Solution

  • Yes.

    If the AMI is provided by AWS, then there is some software installed on the instance that will take the key nominated during RunInstances() and will add it to the /home/ec2-user/.ssh/authorized_keys file. This then allows you to login using the private half of the keypair.

    If an AMI is taken of this instance, then the keypair will remain in the authorized_keys file.

    Next, if a new instance is launched with that AMI, another keypair can be passed via RunInstances(), but the old keypair will still be there and can be used.

    So:

    • Instance 1 launched with Keypair 1: Can connect using Keypair 1
    • AMI created from Instance 1
    • Instance 2 launched from AMI, specifying Keypair 2: Can connect using either Keypair 1 or Keypair 2