Hi there!
I’m writting a Golang tool and I’d like to extract the region
attribute from potentially several provider
s aws
.
Eg:
provider "aws" {
region = "us-east-1"
alias = "us-east-1"
}
provider "aws" {
region = "us-west-2"
alias = "us-west-2"
}
resource "aws_dynamodb_table" "foo" {
provider = aws.us-east-1
name = "foo"
read_capacity = 1
write_capacity = 1
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
}
or:
provider "aws" {
region = var.aws_region
}
variable "aws_region" {
default = "us-east-1"
}
My tool takes for example the DynamoDB resource aws_dynamodb_table.foo
and will make some calls to the AWS API using the AWS SDK for Golang v2, but then, I need the AWS region to make that AWS API call.
I’ve been reading about the packages:
- hclparse package - github.com/hashicorp/hcl/v2/hclparse - Go Packages
- hclsimple package - github.com/hashicorp/hcl/v2/hclsimple - Go Packages
- hclwrite package - github.com/hashicorp/hcl/v2/hclwrite - Go Packages
- gohcl package - github.com/hashicorp/hcl/v2/gohcl - Go Packages
but I’m kinda lost on which one I’d need.
My feeling is that I need to use gohcl package - github.com/hashicorp/hcl/v2/gohcl - Go Packages so I can add an hcl.EvalContext
with some variables for the aws_region
a bit like this:
ctx := &hcl.EvalContext{
Variables: map[string]cty.Value{},
Functions: map[string]function.Function{},
}
taken from go-wardley/hcl/hcl.go at master · DavidGamba/go-wardley · GitHub .
But before I dig too far, is there a simple way from terraform
to get these information by any chance?
All I need is the map of:
"provider[\"registry.terraform.io/hashicorp/aws\"]" = "us-east-1"
"provider[\"registry.terraform.io/hashicorp/aws\"].my_alias" = "us-west-2"
where these keys are coming from the TF state file, eg:
"resources": [
{
"mode": "managed",
"type": "aws_dynamodb_table",
"name": "foo",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"].my_aws",
...
Thanks!