cloudyr/aws.ec2

Working on aws

Opened this issue · 1 comments

I've created two short functions that would be useful additions:

  1. Get the aws instance id. Only run on an actual aws instance:

    # Only run on aws instance
    # Get the instance id
    get_instance_id = function(verbose = FALSE) {
     system(
       "curl http://instance-data/latest/meta-data/instance-id",
       intern = TRUE,
       ignore.stderr = !verbose
     )
    }
    
  2. Check if we are running on an instance.

    is_instance = function() {
     hostname <- system("hostname -d", intern = TRUE)
     # Grep could be made better by checking for eu-west-1, etc.
     length(grep("*.compute.internal$", hostname)) > 1
    }
    

Example use case:

run = function() {
   if(is_instance()) {
       instance_id = get_instance_id()
       on.exit(terminate_instances(instance_id))
   }
   simulate_stuff()
}

Thanks - I'm just getting to this for some reason. The aws.ec2metadata package now provides an is_ec2() function that checks whether R is running on EC2. The other ideas are great and I'll try to implement them.