简体   繁体   English

如果设置了安全组,则无法使用 terraform 创建 ec2 实例

[英]failed to create ec2 instance using terraform if set security group

I tried to create an EC2 instance.我试图创建一个 EC2 实例。 When I don't set security group, it's good, but when set security group it failed with the following message:当我不设置安全组时,这很好,但是当设置安全组时它失败并显示以下消息:

│ Error: creating EC2 Instance: InvalidParameterValue: Value () for parameter groupId is invalid. The value cannot be empty
│   status code: 400, request id: 2935799e-2364-4676-ba02-457740336cd1
│
│   with aws_instance.my_first_instance,
│   on main.tf line 44, in resource "aws_instance" "my_first_instance":
│   44: resource "aws_instance" "my_first_instance" {

The code is代码是

 variable "ecs_cluster_name" {
  type    = string
  default = "production"
}

data "aws_ami" "ecs_ami" {
  most_recent = true
  owners      = ["amazon"]

  filter {
    name   = "name"
    values = ["amzn2-ami-ecs-hvm-2.0.202*-x86_64-ebs"]
  }
}

output "ami_name" {
  value       = data.aws_ami.ecs_ami.name
  description = "the name of ecs ami"
}

output "security_group_id" {
  value       = aws_security_group.default.id
  description = "id of security group"
}


resource "aws_security_group" "default" {
  name = "terraform_Security_group"
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "my_first_instance" {
  ami           = data.aws_ami.ecs_ami.id
  instance_type = "t2.micro"

  # security_groups = ["sg-06e91dae98b2c44c6"]
  security_groups = [aws_security_group.default.id]

  user_data = <<-EOF
                #!/bin/bash
                echo ECS_CLUSTER={cluster_name} >> /etc/ecs/ecs.config
                EOF
}

You should be using vpc_security_group_ids :您应该使用vpc_security_group_ids

  vpc_security_group_ids = [aws_security_group.default.id]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 terraform 中使用模块向 ec2 分配多个安全组时出错 - Getting error while assigning multiple security group using modules to ec2 in terraform 使用 CloudFormation 启动 EC2 实例的安全组问题 - Security Group Issue with launching an EC2 instance with CloudFormation 即使安全组正确也无法连接到 EC2 - Failed to connect to EC2 even security group is correct 无法访问使用 terraform 创建的 EC2 实例 - unable to access EC2 instance created using terraform 使用 Terraform 脚本使用密钥旋转 ec2 实例时出错 - error while spinning ec2 instance with key using Terraform script 如何使用 Jenkinsfile 创建 EC2 实例 - How to create an EC2 Instance using Jenkinsfile 使用 Powershell 和 Terraform 挑战为 AWS EC2 创建 EC2 用户数据 - Create EC2 Userdata for AWS EC2 using Powershell with Terraform challenge 如何在不重新创建 EC2 实例的情况下通过 Cloudformation 更新安全组 - How I can update security group through Cloudformation without recreating EC2 Instance ssh ec2 连接失败,因为 ec2 实例上没有剩余空间 - ssh connection failed for ec2 as there is no space left on ec2 instance 如何使用 Terraform 将 EC2 实例启动到现有 VPC 中? - How do I launch an EC2 instance into an existing VPC using Terraform?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM