generated from ipdxco/github-as-code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaws.tf
145 lines (116 loc) · 2.69 KB
/
aws.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# terraform init
# export AWS_ACCESS_KEY_ID=
# export AWS_SECRET_ACCESS_KEY=
# export AWS_REGION=
# export TF_VAR_name=
# terraform apply
terraform {
required_providers {
aws = {
version = "4.5.0"
}
}
required_version = "~> 1.2.9"
}
provider "aws" {}
variable "name" {
description = "The name to use for S3 bucket, DynamoDB table and IAM users."
type = string
}
resource "aws_s3_bucket" "this" {
bucket = var.name
tags = {
Name = "GitHub Management"
Url = "https://github.com/pl-strflt/github-mgmt-template"
}
}
resource "aws_s3_bucket_ownership_controls" "this" {
bucket = aws_s3_bucket.this.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_acl" "this" {
depends_on = [ aws_s3_bucket_ownership_controls.this ]
bucket = aws_s3_bucket.this.id
acl = "private"
}
resource "aws_dynamodb_table" "this" {
name = var.name
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
tags = {
Name = "GitHub Management"
Url = "https://github.com/pl-strflt/github-mgmt-template"
}
}
resource "aws_iam_user" "ro" {
name = "${var.name}-ro"
tags = {
Name = "GitHub Management"
Url = "https://github.com/pl-strflt/github-mgmt-template"
}
}
resource "aws_iam_user" "rw" {
name = "${var.name}-rw"
tags = {
Name = "GitHub Management"
Url = "https://github.com/pl-strflt/github-mgmt-template"
}
}
data "aws_iam_policy_document" "ro" {
statement {
actions = ["s3:ListBucket"]
resources = ["${aws_s3_bucket.this.arn}"]
effect = "Allow"
}
statement {
actions = ["s3:GetObject"]
resources = ["${aws_s3_bucket.this.arn}/*"]
effect = "Allow"
}
statement {
actions = ["dynamodb:GetItem"]
resources = ["${aws_dynamodb_table.this.arn}"]
effect = "Allow"
}
}
data "aws_iam_policy_document" "rw" {
statement {
actions = ["s3:ListBucket"]
resources = ["${aws_s3_bucket.this.arn}"]
effect = "Allow"
}
statement {
actions = [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
]
resources = ["${aws_s3_bucket.this.arn}/*"]
effect = "Allow"
}
statement {
actions = [
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:DeleteItem",
]
resources = ["${aws_dynamodb_table.this.arn}"]
effect = "Allow"
}
}
resource "aws_iam_user_policy" "ro" {
name = "${var.name}-ro"
user = aws_iam_user.ro.name
policy = data.aws_iam_policy_document.ro.json
}
resource "aws_iam_user_policy" "rw" {
name = "${var.name}-rw"
user = aws_iam_user.rw.name
policy = data.aws_iam_policy_document.rw.json
}