EC2 资源的 Terraform 动态标记失败,并显示“此处不应出现“标签”类型的块”

Dmi*_*nov 3 terraform terraform-provider-aws terraform0.12+

\xe2\x9e\x9c terraform -v  \nTerraform v0.12.24\n+ provider.aws v2.60.0\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的地形example.tf

\n\n
locals {\n  standard_tags = {\n    team        = var.team\n    project     = var.project\n    component   = var.component\n    environment = var.environment\n  }\n}\n\nprovider "aws" {\n  profile = "profile"\n  region  = var.region\n}\n\nresource "aws_key_pair" "security_key" {\n  key_name   = "security_key"\n  public_key = file(".ssh/key.pub")\n}\n\n# New resource for the S3 bucket our application will use.\nresource "aws_s3_bucket" "project_bucket" {\n  # NOTE: S3 bucket names must be unique across _all_ AWS accounts, so\n  # this name must be changed before applying this example to avoid naming\n  # conflicts.\n  bucket = "project-bucket"\n  acl    = "private"\n}\n\n\nresource "aws_security_group" "ssh_allow" {\n  name = "allow-all-ssh"\n  ingress {\n    cidr_blocks = [\n      "0.0.0.0/0"\n    ]\n    from_port = 22\n    to_port   = 22\n    protocol  = "tcp"\n  }\n  egress {\n    from_port   = 0\n    to_port     = 0\n    protocol    = "-1"\n    cidr_blocks = ["0.0.0.0/0"]\n  }\n}\n\nresource "aws_security_group" "http_allow" {\n  name = "allow-all-http"\n  ingress {\n    cidr_blocks = [\n      "0.0.0.0/0"\n    ]\n    from_port = 80\n    to_port   = 80\n    protocol  = "tcp"\n  }\n  egress {\n    from_port   = 0\n    to_port     = 0\n    protocol    = "-1"\n    cidr_blocks = ["0.0.0.0/0"]\n  }\n}\n\n\nresource "aws_instance" "example" {\n  ami             = "ami-08ee2516c7709ea48"\n  instance_type   = "t2.micro"\n  security_groups = [aws_security_group.ssh_allow.name, aws_security_group.http_allow.name]\n  key_name        = aws_key_pair.security_key.key_name\n\n  connection {\n    type        = "ssh"\n    user        = "centos"\n    private_key = file(".ssh/key")\n    host        = self.public_ip\n  }\n\n\n  provisioner "local-exec" {\n    command = "echo ${aws_instance.example.public_ip} > ip_address.txt"\n  }\n\n  provisioner "remote-exec" {\n    inline = [\n      "sudo yum -y install nginx",\n      "sudo systemctl start nginx"\n    ]\n  }\n\n  depends_on = [aws_s3_bucket.project_bucket, aws_key_pair.security_key]\n\n  dynamic "tag" {\n    for_each = local.standard_tags\n\n    content {\n      key                 = tag.key\n      value               = tag.value\n      propagate_at_launch = true\n    }\n  }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

当我跑步时terraform plan

\n\n

我收到以下错误:

\n\n
\xe2\x9e\x9c terraform plan\n\nError: Unsupported block type\n\n  on example.tf line 94, in resource "aws_instance" "example":\n  94:   dynamic "tag" {\n\nBlocks of type "tag" are not expected here.\n\n\n
Run Code Online (Sandbox Code Playgroud)\n

Mar*_*ins 5

tag资源类型的架构中没有名为“已定义”的块类型aws_instance。有一个名为 的参数tags我认为这是获得您正在寻找的结果的方法:

  tags = local.standard_tags
Run Code Online (Sandbox Code Playgroud)

我希望您正在考虑中的tagaws_autoscaling_group,它偏离了tagsAWS 提供商资源中参数的通常设计,因为对于此资源类型,特别是每个标签都有附加属性propagate_at_launch。该属性仅适用于自动缩放组,因为它决定从自动缩放组启动的实例是否将从该组本身继承特定标签。