Skip to content

Commit fda6821

Browse files
Updated the module versions (#13) (#14)
* AD-183: Updated Module versions and added comments * AD-183: Updated Module versions and added comments * Set billing mode in dynamoDB to On-demand Co-authored-by: ankush-sqops <[email protected]>
1 parent f604bfa commit fda6821

File tree

6 files changed

+105
-18
lines changed

6 files changed

+105
-18
lines changed

IAM.md

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ The Policy required is:
4949
{
5050
"Sid": "VisualEditor2",
5151
"Effect": "Allow",
52+
"Action": [
53+
"ec2:DescribeAccountAttributes"
54+
],
55+
"Resource": [
56+
"*"
57+
]
58+
},
59+
{
60+
"Sid": "VisualEditor3",
61+
"Effect": "Allow",
5262
"Action": [
5363
"iam:AttachRolePolicy",
5464
"iam:CreatePolicy",
@@ -73,14 +83,21 @@ The Policy required is:
7383
]
7484
},
7585
{
76-
"Sid": "VisualEditor3",
86+
"Sid": "VisualEditor4",
7787
"Effect": "Allow",
7888
"Action": [
89+
"kms:CreateAlias",
7990
"kms:CreateKey",
91+
"kms:DeleteAlias",
8092
"kms:DescribeKey",
93+
"kms:DisableKey",
94+
"kms:EnableKey",
95+
"kms:EnableKeyRotation",
8196
"kms:GetKeyPolicy",
8297
"kms:GetKeyRotationStatus",
98+
"kms:ListAliases",
8399
"kms:ListResourceTags",
100+
"kms:PutKeyPolicy",
84101
"kms:ScheduleKeyDeletion",
85102
"kms:TagResource",
86103
"kms:UntagResource"
@@ -90,21 +107,76 @@ The Policy required is:
90107
]
91108
},
92109
{
93-
"Sid": "VisualEditor4",
110+
"Sid": "VisualEditor5",
94111
"Effect": "Allow",
95112
"Action": [
96113
"logs:AssociateKmsKey",
97114
"logs:CreateLogGroup",
98115
"logs:DeleteLogGroup",
116+
"logs:DeleteRetentionPolicy",
99117
"logs:DescribeLogGroups",
100118
"logs:DisassociateKmsKey",
101119
"logs:ListTagsLogGroup",
120+
"logs:PutRetentionPolicy",
102121
"logs:TagLogGroup",
103122
"logs:UntagLogGroup"
104123
],
105124
"Resource": [
106125
"*"
107126
]
127+
},
128+
{
129+
"Sid": "VisualEditor6",
130+
"Effect": "Allow",
131+
"Action": [
132+
"s3:CreateBucket",
133+
"s3:DeleteBucket",
134+
"s3:DeleteBucketWebsite",
135+
"s3:GetAccelerateConfiguration",
136+
"s3:GetBucketAcl",
137+
"s3:GetBucketCORS",
138+
"s3:GetBucketLocation",
139+
"s3:GetBucketLogging",
140+
"s3:GetBucketObjectLockConfiguration",
141+
"s3:GetBucketPolicy",
142+
"s3:GetBucketPublicAccessBlock",
143+
"s3:GetBucketRequestPayment",
144+
"s3:GetBucketTagging",
145+
"s3:GetBucketVersioning",
146+
"s3:GetBucketWebsite",
147+
"s3:GetEncryptionConfiguration",
148+
"s3:GetIntelligentTieringConfiguration",
149+
"s3:GetInventoryConfiguration",
150+
"s3:GetLifecycleConfiguration",
151+
"s3:GetMetricsConfiguration",
152+
"s3:GetObject",
153+
"s3:GetObjectAcl",
154+
"s3:GetReplicationConfiguration",
155+
"s3:ListAllMyBuckets",
156+
"s3:ListBucket",
157+
"s3:PutAccelerateConfiguration",
158+
"s3:PutBucketAcl",
159+
"s3:PutBucketCORS",
160+
"s3:PutBucketLogging",
161+
"s3:PutBucketObjectLockConfiguration",
162+
"s3:PutBucketPolicy",
163+
"s3:PutBucketPublicAccessBlock",
164+
"s3:PutBucketRequestPayment",
165+
"s3:PutBucketVersioning",
166+
"s3:PutBucketWebsite",
167+
"s3:PutEncryptionConfiguration",
168+
"s3:PutIntelligentTieringConfiguration",
169+
"s3:PutInventoryConfiguration",
170+
"s3:PutLifecycleConfiguration",
171+
"s3:PutMetricsConfiguration",
172+
"s3:PutObject",
173+
"s3:PutObjectLegalHold",
174+
"s3:PutObjectRetention",
175+
"s3:PutReplicationConfiguration"
176+
],
177+
"Resource": [
178+
"*"
179+
]
108180
}
109181
]
110182
}

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ In this module, we have implemented the following CIS Compliance checks for S3:
6666

6767
| Name | Source | Version |
6868
|------|--------|---------|
69-
| <a name="module_kms_key"></a> [kms\_key](#module\_kms\_key) | clouddrove/kms/aws | 0.15.0 |
70-
| <a name="module_log_bucket"></a> [log\_bucket](#module\_log\_bucket) | terraform-aws-modules/s3-bucket/aws | 3.10.0 |
71-
| <a name="module_s3_bucket"></a> [s3\_bucket](#module\_s3\_bucket) | terraform-aws-modules/s3-bucket/aws | 3.10.0 |
69+
| <a name="module_kms_key"></a> [kms\_key](#module\_kms\_key) | clouddrove/kms/aws | 1.3.1 |
70+
| <a name="module_log_bucket"></a> [log\_bucket](#module\_log\_bucket) | terraform-aws-modules/s3-bucket/aws | 4.1.2 |
71+
| <a name="module_s3_bucket"></a> [s3\_bucket](#module\_s3\_bucket) | terraform-aws-modules/s3-bucket/aws | 4.1.2 |
7272

7373
## Resources
7474

examples/state-storage-backend/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ No providers.
1414

1515
| Name | Source | Version |
1616
|------|--------|---------|
17-
| <a name="module_backend"></a> [backend](#module\_backend) | squareops/tfstate/aws | n/a |
17+
| <a name="module_backend"></a> [backend](#module\_backend) | ../../ | n/a |
1818

1919
## Resources
2020

examples/state-storage-backend/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ locals {
99
}
1010

1111
module "backend" {
12-
source = "squareops/tfstate/aws"
12+
source = "../../"
1313
logging = true
1414
bucket_name = "production-tfstate-bucket" #unique global s3 bucket name
1515
environment = local.environment

logging.tf

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Create a cloudtrail trail to track all management events data events ( only for S3 bucket and lambda function) and store them in log_bucket.
12
resource "aws_cloudtrail" "s3_cloudtrail" {
23
count = var.logging ? 1 : 0
34
depends_on = [aws_iam_role_policy_attachment.s3_cloudtrail_policy_attachment]
@@ -28,6 +29,7 @@ resource "aws_cloudtrail" "s3_cloudtrail" {
2829
)
2930
}
3031

32+
#Create a log_group in cloudwatch for storing logs generated by cloudtrail Trail.
3133
resource "aws_cloudwatch_log_group" "s3_cloudwatch" {
3234
count = var.logging && var.cloudwatch_logging_enabled ? 1 : 0
3335
name = format("%s-%s-S3", var.bucket_name, data.aws_caller_identity.current.account_id)
@@ -39,6 +41,7 @@ resource "aws_cloudwatch_log_group" "s3_cloudwatch" {
3941
)
4042
}
4143

44+
# Create an IAM role to be attached with cloudtrail trail
4245
resource "aws_iam_role" "s3_cloudtrail_cloudwatch_role" {
4346
count = var.logging && var.cloudwatch_logging_enabled ? 1 : 0
4447
name = format("%s-cloudtrail-cloudwatch-S3", var.bucket_name)
@@ -49,6 +52,7 @@ resource "aws_iam_role" "s3_cloudtrail_cloudwatch_role" {
4952
)
5053
}
5154

55+
# AWS IAM policy document that allows AWS CloudTrail to assume roles for accessing AWS services.
5256
data "aws_iam_policy_document" "cloudtrail_assume_role" {
5357
count = var.logging ? 1 : 0
5458
statement {
@@ -62,6 +66,7 @@ data "aws_iam_policy_document" "cloudtrail_assume_role" {
6266
}
6367
}
6468

69+
# AWS IAM policy defining permissions for AWS CloudTrail to interact with CloudWatch Logs for S3 logging.
6570
resource "aws_iam_policy" "s3_cloudtrail_cloudwatch_policy" {
6671
count = var.logging && var.cloudwatch_logging_enabled ? 1 : 0
6772
name = format("%s-cloudtrail-cloudwatch-S3", var.bucket_name)
@@ -99,17 +104,18 @@ EOF
99104
}
100105

101106

102-
107+
#Attach the IAM policy to the IAM role created above.
103108
resource "aws_iam_role_policy_attachment" "s3_cloudtrail_policy_attachment" {
104109
count = var.logging && var.cloudwatch_logging_enabled ? 1 : 0
105110
role = aws_iam_role.s3_cloudtrail_cloudwatch_role[0].name
106111
policy_arn = aws_iam_policy.s3_cloudtrail_cloudwatch_policy[0].arn
107112
}
108113

114+
# Create a S3 logger bucket to add bucket access logs and cloudtrial logs
109115
module "log_bucket" {
110116
count = var.logging ? 1 : 0
111117
source = "terraform-aws-modules/s3-bucket/aws"
112-
version = "3.10.0"
118+
version = "4.1.2"
113119
bucket = format("%s-%s-log-bucket", var.bucket_name, data.aws_caller_identity.current.account_id)
114120
force_destroy = true
115121
attach_elb_log_delivery_policy = true
@@ -168,11 +174,12 @@ module "log_bucket" {
168174
POLICY
169175
}
170176

177+
# Create KMS key used to encrypt cloudwatch log group and CloudTrail trail.
171178
module "kms_key" {
172179
count = var.logging ? 1 : 0
173180
depends_on = [data.aws_iam_policy_document.default]
174181
source = "clouddrove/kms/aws"
175-
version = "0.15.0"
182+
version = "1.3.1"
176183

177184
name = format("%s-%s-kms-03", var.bucket_name, data.aws_caller_identity.current.account_id)
178185
enabled = true
@@ -182,6 +189,8 @@ module "kms_key" {
182189
enable_key_rotation = true
183190
}
184191

192+
193+
# IAM policy document defining permissions for AWS KMS related to AWS CloudTrail encryption and decryption.
185194
data "aws_iam_policy_document" "default" {
186195
count = var.logging ? 1 : 0
187196
version = "2012-10-17"
@@ -238,7 +247,7 @@ data "aws_iam_policy_document" "default" {
238247
test = "StringEquals"
239248
variable = "kms:CallerAccount"
240249
values = [
241-
"${data.aws_caller_identity.current.account_id}"]
250+
data.aws_caller_identity.current.account_id]
242251
}
243252
condition {
244253
test = "StringLike"

main.tf

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ locals {
77
data "aws_region" "region" {}
88
data "aws_caller_identity" "current" {}
99

10+
#Create a KMS KeyValue pair for encrypting S3 buckets created for storing terrafrom state
1011
resource "aws_kms_key" "mykey" {
1112
description = "This key is used to encrypt bucket objects"
1213
deletion_window_in_days = 10
@@ -15,6 +16,7 @@ resource "aws_kms_key" "mykey" {
1516
local.tags,
1617
)
1718
}
19+
#IAM role created for EC2 instance to call AWS services on its behalf
1820
resource "aws_iam_role" "this" {
1921
assume_role_policy = <<EOF
2022
{
@@ -37,6 +39,7 @@ EOF
3739
)
3840
}
3941

42+
#Generates an Bucket policy in JSON format so as to attach it with S3 bucket
4043
data "aws_iam_policy_document" "bucket_policy" {
4144
statement {
4245
principals {
@@ -54,9 +57,10 @@ data "aws_iam_policy_document" "bucket_policy" {
5457
}
5558
}
5659

60+
# Create an S3 bucket for storing terraform state
5761
module "s3_bucket" {
5862
source = "terraform-aws-modules/s3-bucket/aws"
59-
version = "3.10.0"
63+
version = "4.1.2"
6064
bucket = format("%s-%s", var.bucket_name, data.aws_caller_identity.current.account_id)
6165
force_destroy = var.force_destroy
6266
attach_policy = true
@@ -91,19 +95,21 @@ module "s3_bucket" {
9195
object_ownership = "BucketOwnerPreferred"
9296
}
9397

98+
# Create a DynampDB table for locking terraform state
9499
resource "aws_dynamodb_table" "dynamodb_table" {
95-
name = format("%s-%s-%s", var.bucket_name, "lock-dynamodb", data.aws_caller_identity.current.account_id)
96-
hash_key = "LockID"
97-
read_capacity = 20
98-
write_capacity = 20
99-
100+
name = format("%s-%s-%s", var.bucket_name, "lock-dynamodb", data.aws_caller_identity.current.account_id)
101+
hash_key = "LockID"
102+
billing_mode = "PAY_PER_REQUEST"
100103
attribute {
101104
name = "LockID"
102105
type = "S"
103106
}
104107

105108
tags = merge(
106-
{ "Name" = format("%s-%s-%s", var.bucket_name, "lock-dynamodb", data.aws_caller_identity.current.account_id) },
109+
{
110+
"Name" = format("%s-%s-%s", var.bucket_name, "lock-dynamodb", data.aws_caller_identity.current.account_id),
111+
"Cost" = format("%s-%s-%s", var.bucket_name, "lock-dynamodb", data.aws_caller_identity.current.account_id)
112+
},
107113
local.tags,
108114
)
109115
}

0 commit comments

Comments
 (0)