LoginSignup
2
0

AWS IAMのグループ、ユーザーをTerraform で管理する

Last updated at Posted at 2021-06-09

AWS IAM 周りを勉強して、コンソール上からの作成の仕方を覚えたので、実際に Terraform を利用した作成、管理の方法を実践してまとめました。
Terraform のプロジェクトのセットアップに関しては別で記事を出していますのでそちらをぜひ。

IAM の設計

AWS では、個別のIAM ユーザーごとに権限を付与するのではなく、グループを作成し、そこに対して権限を付与、ユーザーを紐付けることが推奨されています。
後々組織が大きくなったりすることによってAWSリソースを利用するメンバーが増えた時に、個別で設定するよりも楽に管理ができるようにするためです。
今回は下記の想定で、IAM グループ、ポリシーを作成し、ユーザーを作成して紐付けていきます。

  • 開発者用のIAMグループを作成する
    • EC2(VPC)、ALB、Auto Scaling、RDS、S3のアクセスが可能
  • 開発者であるIAM ユーザーは上記のグループに紐づけられる

Terraform プロジェクトの構成

.
├── docker-compose.yml // Terraform実行のためのDocker環境
└── src
    ├── module_aws.tf
    ├── modules
    │   └── aws
    │       ├── iam_group.tf
    │       └── iam_user.tf
    └── providers.tf

後々、gcpなど、他プロバイダーの利用なども考えられるため、moduleとして分けました。

IAM グループ、ポリシーの作成

module_aws.tf
module "aws" {
  source = "./modules/aws"
}
providers.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.44.0"
    }
  }
}

provider "aws" {
  region = "ap-northeast-1"
}
modules/aws/iam_group.tf
resource "aws_iam_group" "developers" {
  name = "developers"
  path = "/users/"
}

terraform plan を実行して確認ができるかと思います。

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # module.aws.aws_iam_group.developers will be created
  + resource "aws_iam_group" "developers" {
      + arn       = (known after apply)
      + id        = (known after apply)
      + name      = "developers"
      + path      = "/users/"
      + unique_id = (known after apply)
    }

  # module.aws.aws_iam_group_policy.developer_policy will be created
  + resource "aws_iam_group_policy" "developer_policy" {
      + group  = "developers"
      + id     = (known after apply)
      + name   = "developer_policy"
      + policy = jsonencode(
            {
              + Statement = [
                  + {
                      + Action   = [
                          + "rds:*",
                          + "s3:*",
                          + "ec2:*",
                          + "elasticloadbalancing:*",
                          + "autoscaling-plans:*",
                        ]
                      + Effect   = "Allow"
                      + Resource = "*"
                    },
                ]
              + Version   = "2012-10-17"
            }
        )
    }

Plan: 2 to add, 0 to change, 0 to destroy.

IAM ユーザーの作成、グループへの追加

modules/aws/iam_user.tf
resource "aws_iam_user" "example" {
  name          = "example"
  path          = "/"
  force_destroy = true
}

// 作成したIAMユーザーを、グループに追加する
resource "aws_iam_user_group_membership" "example" {
  user = aws_iam_user.example.name

  groups = [
    aws_iam_group.developers.name
  ]
}

こちらも、terraform plan で確認できます。

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # module.aws.aws_iam_user.example will be created
  + resource "aws_iam_user" "example" {
      + arn           = (known after apply)
      + force_destroy = true
      + id            = (known after apply)
      + name          = "example"
      + path          = "/"
      + tags_all      = (known after apply)
      + unique_id     = (known after apply)
    }

  # module.aws.aws_iam_user_group_membership.example will be created
  + resource "aws_iam_user_group_membership" "example" {
      + groups = [
          + "developers",
        ]
      + id     = (known after apply)
      + user   = "example"
    }

Plan: 2 to add, 0 to change, 0 to destroy.

ここまで作成したところで、terraform applyを実行することで、適用されます。
AWS コンソール上のIAM の画面より、確認することができます。

また他のリソースに関しても、まとめていきます。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0