forked from plus3it/terraform-aws-org-new-account-iam-role
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
92 lines (78 loc) · 2.08 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
terraform {
required_version = ">= 0.12"
}
locals {
name = "new_account_iam_role_${random_string.id.result}"
}
data "aws_partition" "current" {}
data "aws_iam_policy_document" "lambda" {
statement {
actions = [
"organizations:DescribeCreateAccountStatus"
]
resources = [
"*",
]
}
statement {
actions = [
"sts:AssumeRole"
]
resources = [
"arn:${data.aws_partition.current.partition}:iam::*:role/${var.assume_role_name}",
]
}
}
module "lambda" {
source = "git::https://github.com/plus3it/terraform-aws-lambda.git?ref=v1.3.0"
function_name = local.name
description = "Create new IAM Account Role"
handler = "new_account_iam_role.lambda_handler"
policy = data.aws_iam_policy_document.lambda
runtime = "python3.8"
source_path = "${path.module}/lambda/src"
tags = var.tags
timeout = 300
environment = {
variables = {
ASSUME_ROLE_NAME = var.assume_role_name
ROLE_NAME = var.role_name
PERMISSION_POLICY = var.role_permission_policy
TRUST_POLICY_JSON = var.trust_policy_json
LOG_LEVEL = var.log_level
}
}
}
resource "random_string" "id" {
length = 13
special = false
}
resource "aws_cloudwatch_event_rule" "this" {
name = local.name
description = "Managed by Terraform"
event_pattern = <<-PATTERN
{
"source": ["aws.organizations"],
"detail-type": ["AWS API Call via CloudTrail"],
"detail": {
"eventSource": ["organizations.amazonaws.com"],
"eventName": [
"InviteAccountToOrganization",
"CreateAccount",
"CreateGovCloudAccount"
]
}
}
PATTERN
tags = var.tags
}
resource "aws_cloudwatch_event_target" "this" {
rule = aws_cloudwatch_event_rule.this.name
arn = module.lambda.function_arn
}
resource "aws_lambda_permission" "events" {
action = "lambda:InvokeFunction"
function_name = module.lambda.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.this.arn
}