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