Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: aws_instance fails with subnet and launch template having security group #32614

Closed
dhrkumar opened this issue Jul 20, 2023 · 3 comments · Fixed by #32854
Closed

[Bug]: aws_instance fails with subnet and launch template having security group #32614

dhrkumar opened this issue Jul 20, 2023 · 3 comments · Fixed by #32854
Labels
bug Addresses a defect in current functionality. service/ec2 Issues and PRs that pertain to the ec2 service. service/vpc Issues and PRs that pertain to the vpc service.
Milestone

Comments

@dhrkumar
Copy link
Contributor

dhrkumar commented Jul 20, 2023

Terraform Core Version

1.3.6

AWS Provider Version

5.8.0

Affected Resource(s)

aws_instance

Expected Behavior

One should be able to launch an EC2 instance with a launch template(having security group) in a subnet without configuring network interface block.

Actual Behavior

Terraform failed to create an EC2 instance with an AWS message that network interfaces and an instance-level security groups may not be specified together.

Relevant Error/Panic Output Snippet

│ Error: creating EC2 Instance: InvalidParameterCombination: Network interfaces and an instance-level security groups may not be specified on the same request
│ 	status code: 400

Terraform Configuration Files

variable "aws_region" {}
variable "aws_access_key_id" {}
variable "aws_secret_key_id" {}
variable "aws_session_token" {}

provider "aws" {
  access_key = var.aws_access_key_id
  secret_key = var.aws_secret_key_id
  token      = var.aws_session_token
  region     = var.aws_region
}

locals {
  tags = { "created-by" : "dhrkumar" }
}
resource "aws_iam_role" "role" {
  assume_role_policy = jsonencode({
    Version   = "2012-10-17"
    Statement = [
      {
        Action    = "sts:AssumeRole"
        Effect    = "Allow"
        Principal = {
          Service = "ec2.amazonaws.com"
        }
      },
    ]
  })
  name = "launch-template-ec2-role"
  path = "/ec2/instance/"
  tags = local.tags
}
resource "aws_iam_instance_profile" "instance_profile" {
  name = "ec2-instance-profile"
  path = "/ec2/instance/"
  role = aws_iam_role.role.name
}
data "aws_subnet" "subnet" {
  filter {
    name   = "tag:Name"
    values = ["DEMO Private Subnet (AZ2)"]
  }
}
resource "aws_security_group" "sg" {
  description = "SG for EC2"
  name        = "ec2-security-group"
  tags        = local.tags
  vpc_id      = data.aws_subnet.subnet.vpc_id
}
resource "aws_launch_template" "lt" {
  description             = "Launch template for ec2"
  disable_api_termination = false
  image_id                = "ami-06ca3ca175f37dd66"
  instance_type           = "t3.nano"
  name                    = "ec2-linux"
  tags                    = local.tags
  update_default_version  = true
  vpc_security_group_ids  = [aws_security_group.sg.id]
  iam_instance_profile {
    arn = aws_iam_instance_profile.instance_profile.arn
  }
  metadata_options {
    http_endpoint               = "enabled"
    http_put_response_hop_limit = 2
    http_tokens                 = "required"
  }
  monitoring {
    enabled = true
  }
  tag_specifications {
    resource_type = "instance"
    tags          = local.tags
  }
  tag_specifications {
    resource_type = "volume"
    tags          = local.tags
  }
}
resource "aws_instance" "ec2" {
  subnet_id = data.aws_subnet.subnet.id
  tags      = local.tags
  launch_template {
    name    = aws_launch_template.lt.name
    version = "$Latest"
  }
  root_block_device {
    delete_on_termination = true
  }
}

Steps to Reproduce

Replace name in aws_subnet data source and apply above configuration code to get error.

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

No response

Would you like to implement a fix?

Yes

@dhrkumar dhrkumar added bug Addresses a defect in current functionality. needs-triage Waiting for first response or review from a maintainer. labels Jul 20, 2023
@github-actions
Copy link

Community Note

Voting for Prioritization

  • Please vote on this issue by adding a 👍 reaction to the original post to help the community and maintainers prioritize this request.
  • Please see our prioritization guide for information on how we prioritize.
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.

Volunteering to Work on This Issue

  • If you are interested in working on this issue, please leave a comment.
  • If this would be your first contribution, please review the contribution guide.

@github-actions github-actions bot added service/ec2 Issues and PRs that pertain to the ec2 service. service/iam Issues and PRs that pertain to the iam service. service/vpc Issues and PRs that pertain to the vpc service. labels Jul 20, 2023
@justinretzolk justinretzolk removed service/iam Issues and PRs that pertain to the iam service. needs-triage Waiting for first response or review from a maintainer. labels Aug 1, 2023
@github-actions github-actions bot added this to the v5.12.0 milestone Aug 8, 2023
@github-actions
Copy link

This functionality has been released in v5.12.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality. service/ec2 Issues and PRs that pertain to the ec2 service. service/vpc Issues and PRs that pertain to the vpc service.
Projects
None yet
2 participants