LoginSignup
0
1

Terraformでテンプレート分割を実行する時に出したエラー集

Last updated at Posted at 2023-05-28

以下の記事を作成する際に出したエラー集で、ただ単にエラーと原因を箇条書き的にして記載しているだけの記事ですが、ナレッジになると思うので是非読んでみていただけると。

① Error: only alphanumeric characters and hyphens allowed in "name"

 Error: only alphanumeric characters and hyphens allowed in "name"
│ 
│   with aws_lb_target_group.target_group,
│   on main.tf line 136, in resource "aws_lb_target_group" "target_group":
│  136:   name             = "target_group"
│ 

原因: ターゲットグループのリソース名にアンダースコア(_)が使えないのに使ってしまった。ハイフン(-)とかの使える文字にすれば解決します。

② Error: creating application Load Balancer: InvalidConfigurationRequest: Security group 'sg-XXXXX' does not belong to VPC 'vpc-XXXXX'

│ Error: creating application Load Balancer: InvalidConfigurationRequest: Security group 'sg-XXXXX' does not belong to VPC 'vpc-XXXXX'
│       status code: 400, request id: 61a79e71-2d47-4f83-89e9-92e987bd5c36
│ 
│   with aws_lb.ALB,
│   on main.tf line 121, in resource "aws_lb" "ALB":
│  121: resource "aws_lb" "ALB" {
│ 

原因: ALBを作成しようとしたのですが、指定するVPCを間違えて、「そのVPCには、セキュリティグループありませんけど」のエラー

③ Error: Error registering targets with target group: Error attaching instance to LB, retrying: InvalidTarget: The following targets are not in the target group VPC 'vpc-XXXXX': 'i-XXXXX'

│ Error: Error registering targets with target group: Error attaching instance to LB, retrying: InvalidTarget: The following targets are not in the target group VPC 'vpc-XXXXX': 'i-XXXXX'
│       status code: 400, request id: 07dffda1-f854-4ea9-a187-944d62943fc2
│ 
│   with aws_lb_target_group_attachment.target-1,
│   on main.tf line 170, in resource "aws_lb_target_group_attachment" "target-1":
│  170: resource "aws_lb_target_group_attachment" "target-1" {

原因: ALBとEC2の作成するVPCを同じにしていなかったので、「ALBの存在しているVPCにそのEC2ありませんけど」のエラー

④ Error: Reference to undeclared input variable

│ Error: Reference to undeclared input variable
│ 
│   on ../../../modules/Network/main.tf line 23, in module "route":
│   23:   vpc_id       = var.vpc_id
│ 
│ An input variable with the name "vpc_id" has not been declared. This variable can be declared with a variable "vpc_id" {} block.

原因: module.vpc.vpc_idにしないといけないのに単に、var.vpc_idにしてしまった。
モジュール間で参照したい場合は、以下のように記載。
vpc_id = module.vpc.vpc_id のようにvar.ではなく、module. にする。

modulevpc.PNG

⑤ Error: Reference to undeclared resource

│ Error: Reference to undeclared resource
│ 
│   on ../../../modules/Network/Route/main.tf line 5, in resource "aws_internet_gateway" "igw":
│    5:   vpc_id = "${aws_vpc.vpc.id}"
│ 
│ A managed resource "aws_vpc" "vpc" has not been declared in module.Network.module.route.

原因: ④と同じで、モジュール間の参照とか何もていないのに、aws_vpc.vpcのように、何も定義作成されていない存在しないvpcの値を拾おうとして出たエラー

⑥ Error: Reference to undeclared module

│ Error: Reference to undeclared module
│ 
│   on ../../../modules/Network/main.tf line 23, in module "route":
│   23:   vpc_id                    = module.VPC.vpc_id
│ 
│ No module call named "VPC" is declared in module.Network.

原因: 以下のキャプチャのように、module.vpc.vpc_idとするところを、source = "./VPC"に目を取られ、大文字のVPCとしてしまったときに出たエラー

module_net.PNG

⑦ Error: Unsupported attribute

│ Error: Unsupported attribute
│ 
│   on ../../../modules/Network/outputs.tf line 2, in output "vpc_id":
│    2:   value = module.vpc.aws_vpc
│     ├────────────────
│     │ module.vpc is a object
│ 
│ This object does not have an attribute named "aws_vpc".
╵

原因: value = module.vpc.vpc_idをvalue = module.vpc.aws_vpcにしてしまって出たエラー。
以下のように、output "vpc_id" { ~のように記載てoutputした場合、その後は、aws_vpcではなく、vpc_idぶする。

/modules/Network/VPC/outputs.tf
output "vpc_id" {
  value = aws_vpc.vpc.id
}

output "aws_subnet_public_1a" {
  value = aws_subnet.public_1a.id
}

output "aws_subnet_public_1c" {
  value = aws_subnet.public_1c.id
}

output "aws_subnet_private_1a" {
  value = aws_subnet.private_1a.id
}

output "aws_subnet_private_1c" {
  value = aws_subnet.private_1c.id
}

⑧ Error: Reference to undeclared resource

│ Error: Reference to undeclared resource
│ 
│   on ../../../modules/Security/SecurityGroup/outputs.tf line 2, in output "security_group_id":
│    2:   value = aws_security_group_security_group.id
│ 
│ A managed resource "aws_security_group_security_group" "id" has not been declared in module.Security.module.securitygroup.

原因: aws_security_group.security_group.id のように.(ドット)でつなぐ箇所を、_(アンダースコア)で繋いでしまっていた。

⑨ Error: Unsupported argument

│ Error: Unsupported argument
│ 
│   on ../../../modules/Security/SecurityGroup/main.tf line 6, in resource "aws_security_group" "security_group":
│    6:   security_group_description = var.security_group_description
│ 
│ An argument named "security_group_description" is not expected here.

原因: description と指定しないといけないところを、security_group_descriptionという変数を作っていたので、security_group_description = var.security_group_description と言う、オリジナルのリソースパラメータを作ってしまったときのエラー。以下のようにセキュリティグループの作成は、descriptionと言うパラメータが正しい。

ag.PNG

⑩ Error: Incorrect attribute value type

│ Error: Incorrect attribute value type
│ 
│   on ../../../modules/Application/EC2/main.tf line 7, in resource "aws_instance" "ec2":
│    7:   vpc_security_group_ids      = data.terraform_remote_state.security.outputs.security_group_id
│     ├────────────────
│     │ data.terraform_remote_state.security.outputs.security_group_id is "sg-03f27d6fc22f0e9ec"
│ 
│ Inappropriate value for attribute "vpc_security_group_ids": set of string required.

原因: idsのように複数形の場合は、指定リソースが1個の単数でもリスト型の[]で囲まないといけない。
正しい書き方:vpc_security_group_ids = [data.terraform_remote_state.security.outputs.security_group_id]
誤った書き方:vpc_security_group_ids = data.terraform_remote_state.security.outputs.security_group_id

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1