Creating and terminating test instances in AWS quickly

This is mostly a note to self. When I need an EC2 instance to run a quick test, it may be overly annoying to provision one through the web console, or it may feel a bit overkill to do that using large frameworks like terraform. Using the AWS command line is just fine, if you know what command to run with which parameters, and it pays off quickly if, to run your tests, you use the settings often (AMI, subnet, security groups…) or if during the same test session  you need to scrap and rebuild test instances a few times. Here is an example on how to do so with the AWS command line client.

Starting an instance

You’ll want to run something like this:

aws ec2 run-instances \
    --image-id ami-019745dc682c2e518 \
    --instance-type t3.nano \
    --subnet-id subnet-9ef234f9 \
    --security-group-ids sg-9b604be2 \
    --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=test-instance},{Key=owner,Value=marco.marongiu}]' \
    --key-name marco.marongiu

The command will return a JSON document where you’ll find all the information you need to know about the instance, and much more. You will be particularly interested in the IP or DNS name, and the instance ID. Save the instance ID aside, it will be useful when you want to terminate the instance.

What you need to find out

  • the ID of the AMI; in the case of Debian 10 “Buster”, you’ll find all of them here;
  • what instance type you want to launch (this is easy);
  • the ID of the subnet in which you want to run the instance (you will probably have a subnet suitable for running test stuff, figure out which one it is and note it down for future use);
  • the ID of the security group(s) you want to apply to the instance; this depends a bit on which kind of tests you are going to run on the instance; again, you need to figure it out;
  • the name of the SSH key, if you need to log in the instance (which may well be the case for this kind of “scrapwork”;
  • add tags to indicate that this is scrap and you are the owner; this is useful if someone wants to clean up in the future and if you left one or more of these instance running for some time: they will know it’s scrap and they may ask you if it’s OK to kill them or not

You will want to add something like --user-data file://my_script.txt if you need something more than a bare instance from the AMI.

Scaling up, slightly

If you work in more than one AWS account, things like the subnet ID, security group IDs, name of the SSH key and, possibly, tags will vary; in that case you may “engineer” the set-up a bit more by preparing “skeletons” for each account, and then replacing most of the option with --cli-input-json and the input file, e.g.:

aws ec2 run-instances --cli-input-json file://myaccount.json

where the file myaccount.json will include the options that you would have passed on the command line for that particular AWS account, e.g.:

    "ImageId": "ami-019745dc682c2e518",
    "InstanceType": "t3.nano",
    "KeyName": "marco.marongiu",
    "SecurityGroupIds": [
    "SubnetId": "subnet-9ef234f9",
    "UserData": "",
    "TagSpecifications": [
            "ResourceType": "instance",
            "Tags": [
                    "Key": "Name",
                    "Value": "test-instance"
                    "Key": "owner",
                    "Value": "marco.marongiu"

You can read more about this in the AWS CLI documentation.

Terminating the instance

Have you saved the instance ID when the instance started? Good! When the time to kill it comes, just run:

aws ec2 terminate-instances --instance-ids i-083f62ba133440fa7

That’s it!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.