【问题标题】:terraform : Error creating Security Group: UnauthorizedOperation: You are not authorized to perform this operationterraform:创建安全组时出错:UnauthorizedOperation:您无权执行此操作
【发布时间】:2016-11-23 16:16:03
【问题描述】:

我有一个下面的 terraform 脚本,在终端上使用时可以正常工作。

provider "aws" {
  region = "${var.aws_region}"
}

resource "aws_instance" "jenkins-poc" {
  count = "2"
  ami           = "${var.aws_ami}"
  instance_type = "${var.instance_type}"
  key_name      = "${var.key_name}"
  availability_zone = "${var.aws_region}${element(split(",",var.zones),count.index)}"
  vpc_security_group_ids = ["${aws_security_group.jenkins-poc.id}"]
  subnet_id = "${element(split(",",var.subnet_id),count.index)}"
  user_data = "${file("userdata.sh")}"
  tags {
    Name = "jenkins-poc${count.index + 1}"
    Owner = "Shailesh"
  }
}

resource "aws_security_group" "jenkins-poc" {
  vpc_id = "${var.vpc_id}"
  name = "${var.security_group_name}"
  description = "Allow http,httpd and SSH"

  ingress {
      from_port = 443
      to_port = 443
      protocol = "tcp"
      cidr_blocks = ["10.0.0.0/8"]
  }
  ingress {
      from_port = 22
      to_port = 22
      protocol = "tcp"
      cidr_blocks = ["0.0.0.0/0"]
  }
 ingress {
      from_port = 80
      to_port = 80
      protocol = "tcp"
      cidr_blocks = ["10.0.0.0/8"]
 }
  egress {
      from_port = "0"
      to_port = "0"
      protocol = "-1"
      cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_elb" "jenkins-poc-elb" {
    name = "jenkins-poc-elb"
    subnets = ["subnet-","subnet-"]
listener {
    instance_port = 80
    instance_protocol = "http"
    lb_port = "80"
    lb_protocol = "http"
}

  health_check {
    healthy_threshold = "2"
    unhealthy_threshold = "3"
    timeout = "3"
    target = "tcp:80"
    interval = 30
  }
    instances = ["${aws_instance.jenkins-poc.*.id}"]
}

变量文件如下所示。

variable "aws_ami" {
  default = "ami-"
}

variable "zones"{
  default = "a,b"
}

variable "aws_region" {
    default = "us-east-1"
}

variable "key_name" {
    default = "test-key"
}

variable "instance_type" {
    default = "t2.micro"
}

variable "count" {
    default = "2"
}
variable "security_group_name" {
    default = "jenkins-poc"
}
variable "vpc_id" {
    default = "vpc-"
}
variable "subnet_id" {
    default = "subnet-,subnet"
}

当我使用 terraform apply 运行终端时,一切正常。但是当我通过詹金斯运行它时,同样的代码给了我下面的错误。

aws_security_group.jenkins-poc: Error creating Security Group: UnauthorizedOperation: You are not authorized to perform this operation

注意 :: 这是我在其中执行此操作的非默认 vpc。

我非常感谢任何 cmets。我没有提到敏感值。

【问题讨论】:

  • 使用的iam用户可能没有权限,可以参考类似讨论@github.com/hashicorp/terraform/issues/2834
  • 这是附加到您的 jenkins 用户的策略的问题。
  • 最初我认为这可能是 IAM 政策的问题。但后来我发现这篇文章说 terraform 定义的一部分可能会导致这种情况。 github.com/hashicorp/terraform/issues/2875
  • 我使用 jenkins 只是为了 git clone 而不是任何实际从 repo 中获取所有 terraform 配置文件的 aws 操作。我相信这与此无关。如果我错了,请纠正我。
  • 你使用远程状态文件吗?否则,当在另一个系统上通过另一个用户运行时,terraform 不会知道您已经创建了资源。

标签: amazon-web-services jenkins amazon-ec2 terraform aws-security-group


【解决方案1】:

只要确保您在正确的 aws 配置文件中,default aws 配置文件可能会限制您创建实例

provider "aws" {
  region = "${var.aws_region}"
  shared_credentials_file = "~/.aws/credentials"
  profile = "xxxxxxx"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 2021-10-17
    • 2021-09-30
    • 2021-12-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多