Terraform code in VS studio not functioning

Im trying to find the error in my code. When I do the terraform apply it gives me errors....

Error: Unsupported argument

on jenkins yourself.tf line 9, in resource "aws security group" "web-node":

9:vpc security group ids = ["##################"]

An argument named "vpc security group_ids" is not expected here.

Error: Incorrect attribute value type

on jenkins yourself line 10, in resource "aws security group" "web-node":

Inappropriate value for attribute "tags": element "security_groups": string required. Error: Unsupported block type on jenkins yourself line 39, in resource "aws security group" "web-node" 39: resource "ec2 instance" "EC2Terraform" {

Blocks of type "resource" are not expected here.

    provider "aws"  {
      access_key = "access key"
      secret_key = "secret key"
      region = "us-east-1"
    }
    #resource Configuration for AWS
    resource "aws_security_group" "web-node" {
        vpc_security_group_ids = ["sg-############"]
        tags = {
        name = "Week4 Node"
        description = "My Security Group"
        security_groups = ["${aws_security_group.web-node.name}"]
        }
      ingress {
        from_port = 80
        to_port = 80
        protocol = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
      }
      ingress {
        from_port = 22
        to_port = 22
        protocol = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
      }    
      ingress {
        from_port = 443
        to_port = 443
        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 "ec2 instance" "EC2Terraform" {

        ami = "ami-01d025118d8e760db"
        instance_type = "t2.micro"
        key_name ="XXXXXXXXXX"
        vpc security group ids = ["##################"]
        tags = {
        Name = "My Jenkins " 
       }
       }
    }

Upvotes: 0

Views: 1202

Answers (1)

pabloxio
pabloxio

Reputation: 1493

There are some errors in the snippet code. The resource aws_security_group doesn't accept an argument called vpc_security_group_ids as you can see in the Terraform documentation. You're defining an AWS security group, you don't have to provide any security_group id at all, what you can do is reference the id of that security group: aws_security_group.web-node.id. Try something like this:

provider "aws" {
  access_key = "access key"
  secret_key = "secret key"
  region     = "us-east-1"
}

#resource Configuration for AWS
resource "aws_security_group" "web-node" {
  name        = "Week4 Node"
  description = "My Security Group"

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port   = 443
    to_port     = 443
    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" "ec2terraform" {
  ami                     = "ami-01d025118d8e760db"
  instance_type           = "t2.micro"
  key_name                = "XXXXXXXXXX"
  vpc_security_groups_ids = [aws_security_group.web-node.id]

  tags = {
    Name = "My Jenkins "
  }
}

Upvotes: 1

Related Questions