Bootstrap FreeKB - Amazon Web Services (AWS) - List Region using Terraform
Amazon Web Services (AWS) - List Region using Terraform


Let's say your Region is us-east-1.

 

Let's say you have the following files on your Terraform server.

├── required_providers.tf
├── aws_regions (directory)
│   ├── data.tf
│   ├── outputs.tf
│   ├── resources.tf

 

required_providers.tf will almost always have this.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
    }
  }
}

 

Let's say provider.tf has the following. In this example, the "default" profile in /home/username/.aws/config and /home/username/.aws/credentials is being used. This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform.

provider "aws" {
  alias   = "default"
  profile = "default"
  region  = "default"
}

 

And modules.tf could have something like this.

module "aws_regions" {
  source = "./aws_regions"
}

 

And data.tf in your aws_regions module could have the following.

 

data "aws_region" "my-aws-region" {}

 

And outputs.tf in your aws_regions module could have the following.

output "my_aws_regions" {
  value = data.aws_region.my-aws-region
}

 

And outputs.tf in the same directory as your main root module has the following.

output "zone" {
  value = module.aws_regions.my_aws_region
}

 

The terraform init and then terraform refresh command can be used to produce output, which should return something like this

zone = {
  "description" = "US East (N. Virginia)"
  "endpoint" = "ec2.us-east-1.amazonaws.com"
  "id" = "us-east-1"
  "name" = "us-east-1"
}

 

Here is how you would output the value of a specific, the "id" key in this example.

output "zone-id" {
  value = data.aws_region.my-aws-region.id
}

 

Which should return the following.

zone-id = "us-east-1"

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 44327c in the box below so that we can be sure you are a human.