【问题标题】:Problems creating simple terraform script to spin up an AWS EC2 instance创建简单 terraform 脚本以启动 AWS EC2 实例的问题
【发布时间】:2020-11-29 11:01:33
【问题描述】:

我正在尝试使用 Terraform 在 Amazon Web Services 上部署 EC2 实例。我安装了 AWS CLI 并在 linux 机器上工作。使用 terraform 我想模仿下面命令行指令的操作(尽管希望有一点改进):

aws ec2 run-instances --image-id ami-0127d62154efde733 --count 1 --instance-type t3a.nano --key-name aws-key --security-group-ids launch-wizard-13 --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=test}]'

这将在 eu-west-1c 中创建一个实例(虽然这没有定义,并且我的帐户被选择为在 eu-west-1 中),我可以毫无问题地进行 ssh。

  • 我想要一个简单的 .tf 文件来模拟 上面的命令行。
  • 我想定义 正在部署服务器,例如能够在 美国会很好。
  • 我要使用的图像是最新的 ubuntu 服务器,所以图像类型的通配符是 比使用 id 更可取(我相信不同地区可能有不同的 id,但我不确定)。
  • 安全组 (launch-wizard-13) 在我的 帐户,在网络和安全设置中。

我已尝试查看官方文档、博客和 github 存储库,但无法获得适用于上述情况的简单 .tf 文件。通常问题出在安全组上,但如果我从上面的命令行中忽略了该部分,则无法 ssh 进入。请帮忙。

编辑

回复@Marcin,我目前正在运行的完整.tfterraform apply)是

provider "aws" {
  region = "eu-west-2"
}

resource "aws_instance" "myEc2" {
  ami           = "ami-0127d62154efde733"
  instance_type = "t3a.nano"
  key_name      = "aws-key"
  security_groups = [
    "launch-wizard-13"
  ]

  tags = {
    Name = "test"
  }
}

导致错误,

aws_instance.myEc2: Creating...

Error: Error launching instance, possible mismatch of Security Group IDs and Names. See AWS Instance docs here: https://terraform.io/docs/providers/aws/r/instance.html.

        AWS Error: InvalidParameterValue: Value () for parameter groupId is invalid. The value cannot be empty
        status code: 400, request id: 22b572d8-d0d3-4e2e-ba1b-3db91d2e05f6

  on terraform-ec2.tf line 5, in resource "aws_instance" "myEc2":
   5: resource "aws_instance" "myEc2" {

【问题讨论】:

    标签: amazon-web-services amazon-ec2 terraform


    【解决方案1】:

    我尝试验证您的 terrform 代码,在将其调整到我的帐户后,但我没有发现任何问题。它奏效了。

    我认为需要group_id 的唯一方法是在非默认 vpc 中运行代码(我在默认 vpc 中进行了测试)。

    因此,如果您在非默认 VPC 中运行代码并希望按名称(而不是 id)使用安全组,您可以尝试以下操作:

    # get the details of existing launch-wizard-13 security group
    
    data "aws_security_group" "selected" {
      name = "launch-wizard-13"
    }
    

    然后在您的资源中使用组 ID:

    # in your aws_instance resources
    
    vpc_security_group_ids = [
      data.aws_security_group.selected.id
    ]
    

    但是您发布的代码不包含有关自定义 VPC 的任何信息。因此,我看不出您会遇到groupId 问题的原因。

    更新

    完整的工作代码。我用我的密钥对测试了us-east-1 区域。您需要将其更改为您所在的地区:

    provider "aws" {
      # your data
    }
    
    
    resource "aws_security_group" "allow_ssh" {
    
      description = "Allow ssh inbound traffic"
    
      ingress {
        description = "ssh"
        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" "myEc2" {
      ami           = "ami-02354e95b39ca8dec" # "ami-0127d62154efde733"
      instance_type =  "t2.micro" # "t3a.nano"
      key_name      = "MyKey" # "<aws-key>"
      
      vpc_security_group_ids = [
       aws_security_group.allow_ssh.id
      ]
    
      tags = {
        Name = "test"
      }
    }
    
    

    【讨论】:

    • 感谢您的快速回复。我收到错误:错误:在 terraform-ec2.tf 第 5 行,数据“aws_security_group”“selected”中找不到匹配的 SecurityGroup:5:数据“aws_security_group”“selected”{。我也尝试将名称更改为 id,但遇到了同样的问题。我会对默认的 vpc 感到非常满意,只要我可以 ssh 进入它,请您编辑您的答案以显示您的工作代码吗?
    • @user728785 我添加了完整的工作代码。如果这对您不起作用,那么除了阻止它工作的代码之外,还有其他一些问题。
    • @user728785 很高兴听到这个消息:-)
    【解决方案2】:

    您可以参考EC2 section from the terraform documentation

    用类似的东西创建一个文件.tf

    provider "aws" {
      region = "eu-west-1c"
    }
    
    resource "aws_instance" "myEc2" {
      ami           = "ami-0127d62154efde733"
      instance_type = "t3.nano"
      key_name      = "<aws-key>"
      vpc_security_group_ids = [
        "launch-wizard-13"
      ]
    
      tags = {
        Name = "test"
      }
    }
    

    【讨论】:

    • 感谢您的回复。我将区域更改为 eu-west-2,因为 1c 不可用,但没关系。当我运行 terraform apply 时,我收到错误:错误:启动实例时出错,安全组 ID 和名称可能不匹配。在此处查看 AWS 实例文档:terraform.io/docs/providers/aws/r/instance.html。 AWS 错误:InvalidParameterValue:参数 groupId 的值 () 无效。该值不能为空状态代码:400,请求 id:84782a87-9af9-4afc-9604-c7d4257bcdf8 在 terraform-ec2.tf 第 5 行,资源“aws_instance”“myEc2”:这是我一直遇到的问题。
    • @user728785 您可以使用security_groups 代替vpc_security_group_ids
    • @Marcin,感谢您的回复,但该更改会导致与上述相同的错误。
    • @user728785 你好。您可以使用您尝试运行的 terraform 代码和准确的错误消息来更新您的问题吗?
    猜你喜欢
    • 2020-03-10
    • 2021-08-19
    • 2018-11-03
    • 2021-10-11
    • 1970-01-01
    • 2018-11-17
    • 2021-06-19
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多