5

I have a Java application running on AWS Fargate. Regions.getCurrentRegion() returns null from within an AWS Fargate for security reasons

Is there an alternative way to determine which region the fargate container is running in?

dogfish
  • 2,646
  • 4
  • 21
  • 37

2 Answers2

14

In Fargate, the current region is available via the AWS_REGION environment variable.

Samuel Karp
  • 4,373
  • 22
  • 34
  • 2
    Do you also know how to get the AZ for this case ? – dogfish Aug 27 '18 at 21:52
  • 12
    Where is this documented? – spg Jan 10 '19 at 23:05
  • If it's not documented it's subject to change – ekcrisp Feb 12 '20 at 21:52
  • Here is where it is documented: https://docs.aws.amazon.com/AmazonECS/latest/userguide/ECS_CLI_Configuration.html – GunWanderer Jul 05 '22 at 19:30
  • @GunWanderer That's documentation for the ECS CLI; it doesn't have anything to do with the ECS/Fargate runtime environment... – Jesse Friedman Jul 29 '22 at 09:53
  • @JesseFriedman, you didn’t read the entire page. Look at the bottom of the page, #3: Environment variables – GunWanderer Jul 30 '22 at 13:16
  • @GunWanderer I did read the page, and I stand by my statement... The page says that the ECS CLI, a local command-line tool, configures its region using several methods, including looking at the `AWS_REGION` environment variable. The AWS CLI and SDKs also look at this variable, making it something of a convention - but the description of a command-line tool's behavior is in _no way whatsoever_ documentation of what environment variables may or may not be set in the runtime environment of an ECS task. The page makes no mention at all of ECS runtime behavior. – Jesse Friedman Jul 31 '22 at 14:14
  • @JesseFriedman, interesting interpretation. I’m not going to try to convince you to do what you don’t want. To each their own. #3. Environment variables - Attempts to fetch the region from the following environment variables: AWS_REGION AWS_DEFAULT_REGION – GunWanderer Aug 01 '22 at 16:32
  • 1
    The answerer Samuel Karp used to work at Amazon on containers, so I think he's a reliable source. There's two separate things here: Fargate environment variables and configuring ECS CLI. They both happen to use AWS_REGION, though Fargate doesn't seem to be documented. – Daniel Compton Aug 29 '22 at 18:38
1

If you are using AWS java sdk you can chain AwsRegionProvider and abstract out logic around region retrieval. If you use a credentials profile, or the environment variable changes, or it eventually becomes available in TaskMetadata you can chain region providers so that you can get region wherever it's available. This also allows you to reuse code that may run on different types of AWS infrastructure.

You would construct an AwsRegionProviderChain and pass in an instance of each relevant AwsRegionProvider such as AwsEnvVarOverrideRegionProvider, AwsProfileRegionProvider etc.

ekcrisp
  • 1,967
  • 1
  • 18
  • 26
  • I like this answer, but is there a way to configure the timeout for all providers? Doing this seems to increase my application startup time by quite a lot – Toofy Aug 17 '20 at 11:45
  • you could try using a CompletableFuture.allOf to get the first one that returns. If speed is imperative you may be better off doing something platform specific – ekcrisp Sep 11 '20 at 03:41