Terraform plan err: Unsupported argument - terraform

I have a resource like this repo/dynamo/main.tf:
resource "aws_dynamodb_table" "infra_locks" {
name = "infra-locks"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
}
And I refer above file as a module follow github repo
Tf file where I refer to above file repo/example/main.tf:
provider "aws" {
region = var.region
}
module "dynamodb_table" {
source = "../dynamodb_table"
name = "my-table"
}
I have terraform init success but fail when run terraform plan
Error: Unsupported argument
on main.tf line 14, in module "dynamodb_table":
14: name = "my-table"
An argument named "name" is not expected here.
How can i fix this?
Tks in advance

As #luk2302 has said:
resource "aws_dynamodb_table" "infra_locks" {}
The above resource exactly has name attribute but it 's not a variable so we can not assign anything to it.
There is a confused right here.
I have fixed by change a little bit: repo/dynamo/main.tf
resource "aws_dynamodb_table" "infra_locks" {
name = var.name
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
}
repo/dynamo/variable.tf
variable "name" {
description = "dynamo table name"
default = null
}
And final, we can configure your dynamo_table 's name.

Related

Tag variable with list in aws_s3_bucket terraform resource

I am trying to create a tag variable as part of a module for aws_s3_bucket terraform resource. One of the tag keys should allow a list(string) value. What data type should I use?
I tried the following in variables.tf:
variable "name" {
type = string
}
variable "tags" {
type = object({
Owner = string
DataCategory = list(string)
}))
With this implementation in main.tf:
resource "aws_s3_bucket" "bucket" {
bucket = var.name
tags = var.tags
}
And trying to create a bucket in ./example/main.tf:
module "s3_bucket_test" {
source = "../"
name = "example-bucket"
tags = {
Owner = "My Team"
DataCategory = ["Customer","Finance"]
}
}
But I am getting the error:
Inappropriate value for attribute "tags": element
"DataCategory": string required.

Terraform - Reference a for_each resource from another for_each resource

I have a terraform file with the following contents:
resource "aws_iam_group" "developers" {
name = each.value
for_each = toset(var.groups)
}
resource "aws_iam_group_membership" "developers_team" {
name = "Developers Team"
users = [each.value]
for_each = toset(var.group_users)
group = aws_iam_group.developers.name
}
I would like to reference aws_iam_group from aws_iam_group_membership. How would I do that? The current terraform file is not working.
I tried this:
group = aws_iam_group.developers[each.value] //This will not work since it uses the for_each of
its own code block
The variable file is as below:
variable "groups" {
type = list(string)
default = [
"terraform_group1",
"terraform_group2",
"terraform_group3",
]
}
variable "group_users" {
type = list(string)
default = [
"terraform_test_user1",
"terraform_test_user2"
]
}
Edit:
I tried the below, but it is not working
resource "aws_iam_group_membership" "developers_team" {
name = "Developers Team"
users = [for group_user in var.group_users : group_user]
for_each = toset(var.groups)
group = aws_iam_group.developers[each.key]
}
Apparently, this is working:
resource "aws_iam_group" "developer" {
name = "truedeveloper"
}
resource "aws_iam_group_membership" "developers_team" {
name = "Developers_Team"
users = [for group_user in var.group_users : group_user]
for_each = toset(var.groups)
group = aws_iam_group.developer.name
}

Providing list type variable in tfvars file for for_each parameter in resource

depending on environment I want to set separate tfvars files.
main.tf
module "dynamodb_users" {
source = "./modules/dynamodb/users"
for_each = var.users_tables_list
table_name = each.value
}
module
resource "aws_dynamodb_table" "users" {
name = var.table_name
billing_mode = "PROVISIONED"
read_capacity = 5
write_capacity = 5
hash_key = "login"
attribute {
name = "login"
type = "S"
}
}
variables.tf
variable "users_tables_list" {
type = set(string)
default = ["users"]
}
tfvars json file
{
"users_tables_list": [
"test-users",
"users"
]
}
When I am trying to plan without ftvars it is working as it should, but with it error is occuring
Error: error unmarshaling run variable: json: cannot unmarshal array into Go value of type string

Why am I getting an 'Incorrect attribute value type' error when I create a variable in Terraform?

I'm getting an Incorrect attribute value type when I try to create variables in TF.
I'm on this version of TF:
Terraform v0.14.4
+ provider registry.terraform.io/hashicorp/aws v2.70.0
This is the error that I'm getting:
λ terraform apply
Error: Incorrect attribute value type
on main.tf line 18, in resource "aws_instance" "example":
18: vpc_security_group_ids = var.vpc_security_group_ids["jf-master-pd"]
Inappropriate value for attribute "vpc_security_group_ids": set of string
required.
This is the code block in my main.tf code:
resource "aws_instance" "example" {
ami = var.amis["us-east-1"]
instance_type = "t2.micro"
vpc_security_group_ids = var.vpc_security_group_ids["jf-master-pd"]
subnet_id = var.subnet_id["jf-master-pd"]
key_name = "jf-timd-keypair"
}
These are the variables that I'm trying to set in the variables.tf file:
variable "vpc_security_group_ids" {
type = map(string)
default = {
"jf-master-pd" = "sg-0333d9eaaeb3ab1b0"
"jf-master-pd-gov" = "sg-7f051404"
}
}
variable "subnet_id" {
type = map(string)
default = {
"jf-master-pd" = "subnet-3ab1835d"
"jf-master-pd-gov" = "subnet-4dad6304"
}
}
The main.tf and variables.tf files are living in the same directory.
Why am I getting this error? How do I correct it?
I had to supply a list in the variable. That fixed the problem:
variable "vpc_security_group_ids" {
type = map(list(string))
default = {
"jf-master-pd" = ["sg-0333d9eaaeb3ab1b0"]
"jf-master-pd-gov" = ["sg-7f051404"]
}
}

Terraform modules azure event subscription optional fields

I am trying to work with terraform modules to create event subscription pointing to storage queue as an endpoint to it.
Below is the module
resource "azurerm_eventgrid_event_subscription" "events" {
name = var.name
scope = var.scope
subject_filter = var.subject_filter
storage_queue_endpoint = var.storage_queue_endpoint
}
and terraform is
module "storage_account__event_subscription" {
source = "../modules/event"
name = "testevent"
scope = test
subject_filter = {
subject_begins_with = "/blobServices/default/containers/test/blobs/in"
}
storage_queue_endpoint = {
storage_account_id = test
queue_name = test
}
}
Error message:
: subject_filter {
Blocks of type "subject_filter" are not expected here.
Error: Unsupported block type
on azure.tf line 90, in module "storage_account__event_subscription":
: storage_queue_endpoint {
Blocks of type "storage_queue_endpoint" are not expected here.
How do i parse the optional fields properly in terraform modules ?
In you module:
resource "azurerm_eventgrid_event_subscription" "events" {
name = var.name
scope = var.scope
subject_filter = {
subject_begins_with = var.subject_begins_with
}
storage_queue_endpoint = var.storage_queue_endpoint
}
Formatting is off here so make sure to run terraform fmt to account for my poor formatting. Also add the variable to the variables.tf file.
Your Terraform file:
module "storage_account__event_subscription" {
source = "../modules/event"
name = "testevent"
scope = test
subject_begins_with = "/blobServices/default/containers/test/blobs/in"
storage_queue_endpoint = {
storage_account_id = test
queue_name = test
}
}
You create the full structure in the module and then you assign the variables in the terraform file.
Anything that will have the same, or generally the same, value can have a default value set in the variables.tf as well so that you get smaller chunks in the TF file.

Resources