How to launch ec2 instance with custom root volume ebs size (more than 8GB) using AWS Cli

4.5k views Asked by At

I'm trying to launch an ec2 instance using AWS CLI, but default root volume is 8GB only. how can I launch ec2 instance using CLI with say 100GB of root volume?

I'm trying this command,

aws ec2 run-instances --image-id ami-xxxxx --count 1 --instance-type t2.micro \
--subnet-id xxxxxxx \
--key-name my-key \
--security-group-ids sg-xxxxxx \
--no-associate-public-ip-address \
--user-data file://test.sh \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=test-server}]'

I tried adding below parameters, but its not working.

  • --block-device-mapping DeviceName=/dev/sda1,Ebs={VolumeSize=100}
  • --block-device-mapping /dev/sda1=:100:false
  • --block-device-mappings <value> (adds secondary EBS volume to the instance).
1

There are 1 answers

0
Ian Massingham On

This is covered in the AWS CLI Documentation here:

https://docs.aws.amazon.com/cli/latest/reference/ec2/run-instances.html

To launch an instance with a modified block device mapping

You can change individual characteristics of existing AMI block device mappings to suit your needs. Perhaps you want to use an existing AMI, but you want a larger root volume than the usual 8 GiB. Or, you would like to use a General Purpose (SSD) volume for an AMI that currently uses a Magnetic volume.

Use the describe-images command with the image ID of the AMI you want to use to find its existing block device mapping. You should see a block device mapping in the output:

{
  "DeviceName": "/dev/sda1",
  "Ebs": {
    "DeleteOnTermination": true,
    "SnapshotId": "snap-1234567890abcdef0",
    "VolumeSize": 8,
    "VolumeType": "standard",
    "Encrypted": false
  }
}

You can modify the above mapping by changing the individual parameters. For example, to launch an instance with a modified block device mapping, add the following parameter to your run-instances command to change the above mapping's volume size and type:

--block-device-mappings file://mapping.json

Where mapping.json contains the following:

[
  {
    "DeviceName": "/dev/sda1",
    "Ebs": {
      "DeleteOnTermination": true,
      "SnapshotId": "snap-1234567890abcdef0",
      "VolumeSize": 100,
      "VolumeType": "gp2"
    }
  }
]

To do this on one command line, the command should be in the format:

aws ec2 run-instances --block-device-mapping DeviceName=/dev/xvda,Ebs={VolumeSize=100} --image-id ami-0a5e707736615003c --region eu-west-1 --instance-type t3.micro

Note that the device name needs to match the root device name, which you can find with a command in the format:

aws ec2 describe-images --image-id ami-0a5e707736615003c --region eu-west-1