Skip to content

Commit 779b838

Browse files
committed
NRL-1385 sort iam policies to allow ec2 access to Athena
1 parent 1f027c9 commit 779b838

File tree

8 files changed

+171
-16
lines changed

8 files changed

+171
-16
lines changed

terraform/account-wide-infrastructure/dev/ec2.tf

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ module "vpc" {
88
}
99

1010

11-
module "web" {
12-
source = "../modules/ec2"
13-
instance_type = var.instance_type
14-
name_prefix = "nhsd-nrlf--dev"
11+
module "ec2" {
12+
source = "../modules/ec2"
13+
instance_type = var.instance_type
14+
name_prefix = "nhsd-nrlf--dev"
15+
target_bucket_arn = module.dev-glue.target_bucket_arn
16+
glue_kms_key_arn = module.dev-glue.aws_kms_key_arn
17+
athena_kms_key_arn = module.dev-athena.kms_key_arn
18+
athena_bucket_arn = module.dev-athena.bucket_arn
19+
1520

1621
subnet_id = module.vpc.subnet_id
1722
security_groups = module.vpc.security_group

terraform/account-wide-infrastructure/modules/athena/outputs.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ output "workgroup" {
55
output "bucket" {
66
value = aws_s3_bucket.athena
77
}
8+
9+
output "bucket_arn" {
10+
value = aws_s3_bucket.athena.arn
11+
}
12+
13+
output "kms_key_arn" {
14+
value = aws_kms_key.athena.arn
15+
}

terraform/account-wide-infrastructure/modules/athena/s3.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ resource "aws_s3_bucket_policy" "athena" {
2626
}
2727
}
2828
},
29+
{
30+
Sid : "AllowAthenaAccess",
31+
Effect : "Allow",
32+
Principal : {
33+
Service : "athena.amazonaws.com"
34+
},
35+
Action : [
36+
"s3:PutObject",
37+
"s3:GetBucketLocation",
38+
"s3:GetObject",
39+
"s3:ListBucket"
40+
],
41+
Resource : [
42+
aws_s3_bucket.athena.arn,
43+
"${aws_s3_bucket.athena.arn}/*",
44+
]
45+
},
2946
]
3047
})
3148
}

terraform/account-wide-infrastructure/modules/ec2/data.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@ data "aws_ami" "windows-2019" {
66
values = ["Windows_Server-2019-English-Full-Base*"]
77
}
88
}
9+
10+
data "aws_ami" "PowerBI_Gateway" {
11+
most_recent = true
12+
owners = ["self"]
13+
filter {
14+
name = "name"
15+
values = ["PowerBI_Gateway"]
16+
}
17+
}

terraform/account-wide-infrastructure/modules/ec2/ec2.tf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Create the Linux EC2 Web server
21
resource "aws_instance" "web" {
3-
# associate_public_ip_address = false
4-
ami = data.aws_ami.windows-2019.id
5-
instance_type = var.instance_type
6-
key_name = aws_key_pair.ec2_key_pair.key_name
7-
subnet_id = var.subnet_id
8-
security_groups = var.security_groups
2+
# associate_public_ip_address =
3+
iam_instance_profile = aws_iam_instance_profile.powerbi_profile.name
4+
ami = data.aws_ami.PowerBI_Gateway.id
5+
instance_type = var.instance_type
6+
key_name = aws_key_pair.ec2_key_pair.key_name
7+
subnet_id = var.subnet_id
8+
security_groups = var.security_groups
99

1010
user_data = file("${path.module}/scripts/user_data.tpl")
1111

@@ -27,6 +27,6 @@ resource "aws_key_pair" "ec2_key_pair" {
2727

2828
# Saving Key Pair for ssh login for Client if needed
2929
resource "local_file" "ssh_key" {
30-
filename = "${aws_key_pair.ec2_key_pair.key_name}.pem"
30+
filename = "${path.module}/keys/${aws_key_pair.ec2_key_pair.key_name}.pem"
3131
content = tls_private_key.instance_key_pair.private_key_pem
3232
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
resource "aws_iam_role" "ec2_service_role" {
2+
name = "${var.name_prefix}-ec2_service_role"
3+
4+
assume_role_policy = jsonencode({
5+
"Version" : "2012-10-17",
6+
"Statement" : [
7+
{
8+
"Effect" : "Allow",
9+
"Principal" : {
10+
"Service" : "ec2.amazonaws.com"
11+
},
12+
"Action" : "sts:AssumeRole"
13+
}
14+
]
15+
})
16+
}
17+
18+
data "aws_iam_policy_document" "ec2_service" {
19+
statement {
20+
actions = [
21+
"s3:GetBucketLocation",
22+
"s3:GetObject",
23+
"s3:ListBucket",
24+
"s3:ListBucketMultipartUploads",
25+
"s3:ListMultipartUploadParts",
26+
"s3:AbortMultipartUpload",
27+
"s3:CreateBucket",
28+
"s3:PutObject",
29+
"s3:PutBucketPublicAccessBlock"
30+
]
31+
32+
resources = compact([
33+
var.target_bucket_arn,
34+
"${var.target_bucket_arn}/*",
35+
var.athena_bucket_arn,
36+
"${var.athena_bucket_arn}/*",
37+
])
38+
effect = "Allow"
39+
}
40+
41+
statement {
42+
actions = [
43+
"s3:ListBucket",
44+
"s3:GetBucketLocation",
45+
"s3:ListAllMyBuckets"
46+
]
47+
48+
resources = compact([
49+
"*"
50+
])
51+
effect = "Allow"
52+
}
53+
54+
statement {
55+
actions = [
56+
"kms:DescribeKey",
57+
"kms:GenerateDataKey*",
58+
"kms:Encrypt",
59+
"kms:ReEncrypt*",
60+
"kms:Decrypt",
61+
]
62+
63+
resources = [
64+
var.glue_kms_key_arn,
65+
var.athena_kms_key_arn,
66+
]
67+
68+
effect = "Allow"
69+
}
70+
71+
statement {
72+
actions = [
73+
"athena:*",
74+
]
75+
effect = "Allow"
76+
resources = [
77+
"*"
78+
]
79+
}
80+
81+
statement {
82+
actions = [
83+
"glue:*",
84+
]
85+
effect = "Allow"
86+
resources = [
87+
"*"
88+
]
89+
}
90+
}
91+
92+
resource "aws_iam_policy" "ec2_service" {
93+
name = "${var.name_prefix}-ec2"
94+
policy = data.aws_iam_policy_document.ec2_service.json
95+
}
96+
97+
resource "aws_iam_role_policy_attachment" "ec2_service" {
98+
role = aws_iam_role.ec2_service_role.name
99+
policy_arn = aws_iam_policy.ec2_service.arn
100+
}
101+
102+
resource "aws_iam_instance_profile" "powerbi_profile" {
103+
name = "powerbi_profile"
104+
role = aws_iam_role.ec2_service_role.name
105+
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
variable "name_prefix" {
2-
type = string
3-
description = "The prefix to apply to all resources in the module."
4-
}
1+
variable "name_prefix" {}
52
variable "instance_type" {}
63
variable "security_groups" {}
74
variable "subnet_id" {}
5+
variable "glue_kms_key_arn" {}
6+
variable "athena_kms_key_arn" {}
7+
variable "target_bucket_arn" {}
8+
variable "athena_bucket_arn" {}

terraform/account-wide-infrastructure/modules/glue/outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@ output "target_bucket_name" {
33
value = aws_s3_bucket.target-data-bucket.id
44
}
55

6+
output "target_bucket_arn" {
7+
description = "Arn of destination bucket"
8+
value = aws_s3_bucket.target-data-bucket.arn
9+
}
10+
611
output "source_bucket_name" {
712
description = "Name of source bucket"
813
value = aws_s3_bucket.source-data-bucket.id
914
}
1015

16+
output "aws_kms_key_arn" {
17+
description = "Arn of kms key"
18+
value = aws_kms_key.glue.arn
19+
}
20+
1121
output "glue_crawler_name" {
1222
value = "s3//${aws_s3_bucket.source-data-bucket.id}/"
1323
}

0 commit comments

Comments
 (0)