refactor(iam): convert IAM module to reusable universal role module
This commit is contained in:
parent
0b2050c886
commit
9984f19f97
@ -4,8 +4,23 @@ locals {
|
||||
)
|
||||
}
|
||||
|
||||
# 第一个正式 module:iam
|
||||
module "iam" {
|
||||
source = "../../modules/iam"
|
||||
account = local.account # << 唯一需要传入 module 的变量
|
||||
|
||||
data "aws_iam_policy_document" "dev_assume" {
|
||||
statement {
|
||||
actions = ["sts:AssumeRole"]
|
||||
|
||||
principals {
|
||||
type = "AWS"
|
||||
identifiers = ["arn:aws:iam::${local.account.account_id}:root"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module "dev_role" {
|
||||
source = "../../modules/iam"
|
||||
|
||||
name = "dev-app-role"
|
||||
assume_role_policy = data.aws_iam_policy_document.dev_assume.json
|
||||
|
||||
tags = local.account.tags
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ terraform {
|
||||
}
|
||||
|
||||
provider "aws" {
|
||||
region = "ap-northeast-1"
|
||||
region = local.account.region
|
||||
|
||||
assume_role {
|
||||
role_arn = "arn:aws:iam::730335654753:role/TerraformDeployRole-Dev"
|
||||
|
||||
@ -1,18 +1,13 @@
|
||||
resource "aws_iam_role" "this" {
|
||||
name = var.role_name
|
||||
|
||||
assume_role_policy = data.aws_iam_policy_document.assume.json
|
||||
|
||||
tags = var.tags
|
||||
name = var.name
|
||||
assume_role_policy = var.assume_role_policy
|
||||
tags = var.tags
|
||||
}
|
||||
|
||||
data "aws_iam_policy_document" "assume" {
|
||||
statement {
|
||||
actions = ["sts:AssumeRole"]
|
||||
# 附加多个 AWS managed policies
|
||||
resource "aws_iam_role_policy_attachment" "managed" {
|
||||
for_each = toset(var.managed_policy_arns)
|
||||
|
||||
principals {
|
||||
type = "AWS"
|
||||
identifiers = ["*"] # 你可以未来改为 OIDC provider 等
|
||||
}
|
||||
}
|
||||
role = aws_iam_role.this.name
|
||||
policy_arn = each.value
|
||||
}
|
||||
|
||||
@ -1,3 +1,7 @@
|
||||
output "role_arn" {
|
||||
output "arn" {
|
||||
value = aws_iam_role.this.arn
|
||||
}
|
||||
|
||||
output "name" {
|
||||
value = aws_iam_role.this.name
|
||||
}
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
locals {
|
||||
account = yamldecode(
|
||||
file("${path.root}/../../config/accounts/dev.yaml")
|
||||
)
|
||||
}
|
||||
|
||||
terraform {
|
||||
required_version = ">= 1.2"
|
||||
|
||||
required_providers {
|
||||
aws = {
|
||||
source = "hashicorp/aws"
|
||||
version = "~> 5.92.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
provider "aws" {
|
||||
region = var.region
|
||||
|
||||
assume_role {
|
||||
role_arn = "local.account.role_to_assume"
|
||||
session_name = "TerraformDevSession"
|
||||
}
|
||||
}
|
||||
@ -1,18 +1,22 @@
|
||||
variable "account" {
|
||||
type = object({
|
||||
account_id = string
|
||||
name = string
|
||||
environment = string
|
||||
region = string
|
||||
role_to_assume = string
|
||||
logging_bucket = string
|
||||
shared_vpc_account = string
|
||||
backend = object({
|
||||
bucket = string
|
||||
key_prefix = string
|
||||
dynamodb_table = string
|
||||
})
|
||||
tags = map(string)
|
||||
})
|
||||
variable "name" {
|
||||
description = "IAM role name"
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "assume_role_policy" {
|
||||
description = "Assume role policy JSON"
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "managed_policy_arns" {
|
||||
description = "List of managed policy ARNs to attach"
|
||||
type = list(string)
|
||||
default = []
|
||||
}
|
||||
|
||||
variable "tags" {
|
||||
description = "Tags to apply to the IAM role"
|
||||
type = map(string)
|
||||
default = {}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user