aws-cli icon indicating copy to clipboard operation
aws-cli copied to clipboard

aws-cli should set default region to EC2 instance region

Open mveitas opened this issue 13 years ago • 32 comments

Recently we provisioned an EC2 instance with the aws-cli installed that is using IAM roles. We forgot to set the AWS_DEFAULT_REGION environment variable and got an error stating that the default region was not specified.

I am proposing that aws-cli should be able to assume the given region of the EC2 instance. This would eliminate one step of adding the environment variable to the system. If the aws-cli needs to talk to a different region, it can always use a different profile or override the region.

I am sure there are implications to this that need to be thought about.

mveitas avatar Nov 14 '13 20:11 mveitas

It seems to me that this would require aws-cli to query every region out there to find the specified instance id. This would be slower and more expensive. Plus, instance-ids are not guaranteed to be unique between regions, so there is an (admittedly small) risk that the wrong instance could be acted on.

It is completely standard with most AWS services to have to pick which region you want to interact with and I'm not sure this should be hidden in the cli.

ehammond avatar Nov 14 '13 23:11 ehammond

An alternative would be to potentially look at storing the region information into the metadata server which I believe the cli uses to get some of it's information.

mveitas avatar Nov 15 '13 17:11 mveitas

As @jamesls has found, we can get the region itself from the instance metadata:

$ curl http://169.254.169.254/latest/dynamic/instance-identity/document
{
  "version" : "2010-08-31",
  "architecture" : "x86_64",
  "instanceType" : "t1.micro",
  ...
  "region" : "us-west-2",
  "availabilityZone" : "us-west-2b",
  ...
}

So, we can discover the region in which an EC2 instance is running quickly and efficiently.

If a customer ran aws configure on an EC2 instance, it would seem reasonably to offer the region in which they are currently running as the default value to place in their config file. I think this makes a lot of sense.

However, what if they are using an IAM Role rather than traditional credentials. In this case they probably wouldn't run aws configure. Should we still just automatically assume that they want to use the region of the instance as the default region? I'm not so sure about that. Sometimes it will be exactly what they want and in other situations it may be confusing.

Comments?

garnaat avatar Nov 22 '13 19:11 garnaat

I misread the original question. I was thinking that an "aws ec2 ... --instance-ids X" command was being run outside of EC2 and there was an expectation that the command should find the region for the specified instance before operating on it.

Now I see the question is really about running aws-cli inside an EC2 instance which has an IAM role.

+1 for defaulting the aws-cli region to the region of the current instance running aws-cli if IAM roles are being used.

This is going to correctly reduce work for the vast majority of IAM role users and is easy to override for anybody who wants a different behavior (which they would have had to do anyway even without a default).

The only other possible options are defaulting to "us-east-1" (almost certainly not what they wanted) and an error message.

Anybody who is using multiple regions should be used to specifying --region or configuring aws-cli with a region.

ehammond avatar Nov 22 '13 21:11 ehammond

This is a great idea. If it's an instance and then the region is easily accessible by the metadata. If the host has something to stop access to the metadata (eg host firewall) then it can just fail as it does now. It would remove one step from my bootstrap process which currently sets the default region based on the metadata.

Iamrodos avatar Nov 28 '13 20:11 Iamrodos

Any updates about potentially implementing this?

mveitas avatar Mar 17 '14 01:03 mveitas

+1

albertoconnor avatar Jun 25 '14 17:06 albertoconnor

+1

fadeddata avatar Oct 29 '14 20:10 fadeddata

And aws opsworks should ignore the region specification, since it always uses us-east-1. On Oct 29, 2014 10:20 PM, "Dustin Withers" [email protected] wrote:

+1

— Reply to this email directly or view it on GitHub https://github.com/aws/aws-cli/issues/486#issuecomment-60997358.

shlomoswidler avatar Oct 29 '14 22:10 shlomoswidler

+1

aalbertson avatar Jun 23 '15 17:06 aalbertson

+1

dmulter avatar Sep 01 '15 18:09 dmulter

+1

nonbeing avatar Nov 02 '15 09:11 nonbeing

+1 I think this would be great, even if I had to pass a param like --use-instance-region. Right now I have to parse the region from ec2metadata and a regex and then pass it as --region.

npinchot avatar Aug 16 '16 04:08 npinchot

+1

georgealton avatar Sep 06 '16 11:09 georgealton

+1

vpal avatar Sep 14 '16 15:09 vpal

+1

cosmok avatar Nov 10 '16 23:11 cosmok

This still isn't a thing? Its super annoying....

I currently get around it by:

region=$(curl http://169.254.169.254/latest/dynamic/instance-identity/document|grep region|awk -F\" '{print $4}')
echo "[default]" > /root/.aws/config
echo "region = ${region}" >> /root/.aws/config

It would be so useful to have it default to the region that the instance is spawned in...

BradErz avatar Nov 15 '16 16:11 BradErz

+1

WhileLoop avatar Feb 15 '17 00:02 WhileLoop

+1

maslakov avatar Apr 04 '17 09:04 maslakov

+1 When using an IAM EC2 Role, I don't want to have to specify region in all my CLI commands when there is only 1 region in GovCloud (us-gov-west-1). When a second region is added, just default to the region that the instance is running in.

szotrj avatar Jun 08 '17 21:06 szotrj