-
Notifications
You must be signed in to change notification settings - Fork 4
NRL 1385 Set up EC2 for automated data refresh #903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jackleary
merged 25 commits into
develop
from
feature/jale13-nrl-1385-set-up-powerbi-gateway
May 30, 2025
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
8a6a522
NRL-1385 initial ec2 stuff
jackleary 8483dd2
NRL-1385 base ec2 set up
jackleary 37a2e8a
NRL-1385 use autoscaling ec2 for flexibility
jackleary b386a49
nrl-1385 Merge branch 'develop' into feature/jale13-nrl-1385-set-up-p…
jackleary 70c2e55
NRL-1385 create ec2 instance
jackleary bd836f3
NRL-1385 create vpc module
jackleary b7bf446
NRL-1385 call modules
jackleary 4aebec2
NRL-1385 add tags
jackleary 5464f16
NRL-1385 syntax fixes
jackleary d80addc
NRL-1385 create key pair to rdp into ec2
jackleary d9fa802
NRL-1385 update script
jackleary 20daf5a
NRL-1385 update script
jackleary b873b7a
NRL-1385 security fix
jackleary f8dc520
NRL-1385 security fix
jackleary 1f027c9
NRL-1385 make public temporarily. Will revert later
jackleary 779b838
NRL-1385 sort iam policies to allow ec2 access to Athena
jackleary 77a7179
NRL-1385 remove unused script
jackleary 1f73f2c
nrl-1385 Merge remote-tracking branch 'origin' into feature/jale13-nr…
jackleary 387342e
NRL-1385 Make VPC private
jackleary dcce041
NRL-1385 Point EC2 at custom AMI
jackleary 1ea3038
NRL-1385 Update readme
jackleary 9e72ff3
NRL-1385 rmeove public ip from ec2
jackleary 1bbcbd3
NRL-1385 remove unused security group
jackleary f8485c0
NRL-1385 Spelling
jackleary 2b1e371
NRL-1385 remove invocation of init script in user data
jackleary File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| module "vpc" { | ||
| source = "../modules/vpc" | ||
| vpc_cidr_block = var.vpc_cidr_block | ||
| enable_dns_hostnames = var.enable_dns_hostnames | ||
| vpc_public_subnets_cidr_block = var.vpc_public_subnets_cidr_block | ||
| vpc_private_subnets_cidr_block = var.vpc_private_subnets_cidr_block | ||
| aws_azs = var.aws_azs | ||
| name_prefix = "nhsd-nrlf--dev" | ||
| } | ||
|
|
||
| module "powerbi_gw_instance_v2" { | ||
| source = "../modules/ec2" | ||
| use_custom_ami = true | ||
| instance_type = var.instance_type | ||
| name_prefix = "nhsd-nrlf--dev-powerbi-gw-v2" | ||
| target_bucket_arn = module.dev-glue.target_bucket_arn | ||
| glue_kms_key_arn = module.dev-glue.aws_kms_key_arn | ||
| athena_kms_key_arn = module.dev-athena.kms_key_arn | ||
| athena_bucket_arn = module.dev-athena.bucket_arn | ||
|
|
||
| subnet_id = module.vpc.private_subnet_id | ||
| security_groups = [module.vpc.powerbi_gw_security_group_id] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| data "aws_ami" "windows-2019" { | ||
| most_recent = true | ||
| owners = ["amazon"] | ||
| filter { | ||
| name = "name" | ||
| values = ["Windows_Server-2019-English-Full-Base*"] | ||
| } | ||
| } | ||
|
|
||
| data "aws_ami" "PowerBI_Gateway" { | ||
| most_recent = true | ||
| owners = ["self"] | ||
| filter { | ||
| name = "name" | ||
| values = ["PowerBI_GW"] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| resource "aws_instance" "web" { | ||
| associate_public_ip_address = false | ||
| iam_instance_profile = aws_iam_instance_profile.powerbi_profile.name | ||
| ami = local.selected_ami_id | ||
| instance_type = var.instance_type | ||
| key_name = aws_key_pair.ec2_key_pair.key_name | ||
| subnet_id = var.subnet_id | ||
| security_groups = var.security_groups | ||
|
|
||
| user_data = file("${path.module}/scripts/user_data.tpl") | ||
|
|
||
| tags = { | ||
| Name = "${var.name_prefix}-ec2" | ||
| } | ||
|
|
||
| } | ||
|
|
||
| resource "tls_private_key" "instance_key_pair" { | ||
| algorithm = "RSA" | ||
| } | ||
|
|
||
| resource "aws_key_pair" "ec2_key_pair" { | ||
| key_name = "${var.name_prefix}_PowerBI-GateWay-Key" | ||
| public_key = tls_private_key.instance_key_pair.public_key_openssh | ||
| } | ||
|
|
||
| resource "local_file" "ssh_key_priv" { | ||
| filename = "${path.module}/keys/${aws_key_pair.ec2_key_pair.key_name}.pem" | ||
| content = tls_private_key.instance_key_pair.private_key_pem | ||
| } |
110 changes: 110 additions & 0 deletions
110
terraform/account-wide-infrastructure/modules/ec2/iam.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| resource "aws_iam_role" "ec2_service_role" { | ||
| name = "${var.name_prefix}-ec2_service_role" | ||
|
|
||
| assume_role_policy = jsonencode({ | ||
| "Version" : "2012-10-17", | ||
| "Statement" : [ | ||
| { | ||
| "Effect" : "Allow", | ||
| "Principal" : { | ||
| "Service" : "ec2.amazonaws.com" | ||
| }, | ||
| "Action" : "sts:AssumeRole" | ||
| } | ||
| ] | ||
| }) | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "ec2_service" { | ||
| statement { | ||
| actions = [ | ||
| "s3:GetBucketLocation", | ||
| "s3:GetObject", | ||
| "s3:ListBucket", | ||
| "s3:ListBucketMultipartUploads", | ||
| "s3:ListMultipartUploadParts", | ||
| "s3:AbortMultipartUpload", | ||
| "s3:CreateBucket", | ||
| "s3:PutObject", | ||
| "s3:PutBucketPublicAccessBlock" | ||
| ] | ||
|
|
||
| resources = compact([ | ||
| var.target_bucket_arn, | ||
| "${var.target_bucket_arn}/*", | ||
| var.athena_bucket_arn, | ||
| "${var.athena_bucket_arn}/*", | ||
| ]) | ||
| effect = "Allow" | ||
| } | ||
|
|
||
| statement { | ||
| actions = [ | ||
| "s3:ListBucket", | ||
| "s3:GetBucketLocation", | ||
| "s3:ListAllMyBuckets" | ||
| ] | ||
|
|
||
| resources = compact([ | ||
| "*" | ||
| ]) | ||
| effect = "Allow" | ||
| } | ||
|
|
||
| statement { | ||
| actions = [ | ||
| "kms:DescribeKey", | ||
| "kms:GenerateDataKey*", | ||
| "kms:Encrypt", | ||
| "kms:ReEncrypt*", | ||
| "kms:Decrypt", | ||
| ] | ||
|
|
||
| resources = [ | ||
| var.glue_kms_key_arn, | ||
| var.athena_kms_key_arn, | ||
| ] | ||
|
|
||
| effect = "Allow" | ||
| } | ||
|
|
||
| statement { | ||
| actions = [ | ||
| "athena:*", | ||
| ] | ||
| effect = "Allow" | ||
| resources = [ | ||
| "*" | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| actions = [ | ||
| "glue:*", | ||
| ] | ||
| effect = "Allow" | ||
| resources = [ | ||
| "*" | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| resource "aws_iam_policy" "ec2_service" { | ||
| name = "${var.name_prefix}-ec2" | ||
| policy = data.aws_iam_policy_document.ec2_service.json | ||
| } | ||
|
|
||
| resource "aws_iam_role_policy_attachment" "ec2_role_policy" { | ||
| role = aws_iam_role.ec2_service_role.name | ||
| policy_arn = aws_iam_policy.ec2_service.arn | ||
| } | ||
|
|
||
| resource "aws_iam_role_policy_attachment" "ec2_role_policy_ssm" { | ||
| role = aws_iam_role.ec2_service_role.name | ||
| policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore" | ||
| } | ||
|
|
||
| resource "aws_iam_instance_profile" "powerbi_profile" { | ||
| name = "${var.name_prefix}-powerbi_instance_profile" | ||
| role = aws_iam_role.ec2_service_role.name | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| locals { | ||
| selected_ami_id = var.use_custom_ami ? data.aws_ami.PowerBI_Gateway.id : data.aws_ami.windows-2019.id | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| output "instance_id" { | ||
| value = aws_instance.web.id | ||
| } | ||
|
|
||
| output "public_ip" { | ||
| value = aws_instance.web.public_ip | ||
| } |
25 changes: 25 additions & 0 deletions
25
terraform/account-wide-infrastructure/modules/ec2/scripts/user_data.tpl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
|
|
||
|
|
||
| <powershell> | ||
| Install-WindowsFeature -name Web-Server -IncludeManagementTools | ||
|
|
||
| $instanceId = (Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/instance-id -UseBasicParsing).content | ||
| $instanceAZ = (Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/placement/availability-zone -UseBasicParsing).content | ||
| $pubHostName = (Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/public-hostname -UseBasicParsing).content | ||
| $pubIPv4 = (Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/public-ipv4 -UseBasicParsing).content | ||
| $privHostName = (Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/local-hostname -UseBasicParsing).content | ||
| $privIPv4 = (Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/local-ipv4 -UseBasicParsing).content | ||
|
|
||
| New-Item -Path C:\inetpub\wwwroot\index.html -ItemType File -Force | ||
| Add-Content -Path C:\inetpub\wwwroot\index.html "<font face = "Verdana" size = "5">" | ||
| Add-Content -Path C:\inetpub\wwwroot\index.html "<center><h1>AWS Windows VM Deployed with Terraform</h1></center>" | ||
| Add-Content -Path C:\inetpub\wwwroot\index.html "<center> <b>EC2 Instance Metadata</b> </center>" | ||
| Add-Content -Path C:\inetpub\wwwroot\index.html "<center> <b>Instance ID:</b> $instanceId </center>" | ||
| Add-Content -Path C:\inetpub\wwwroot\index.html "<center> <b>AWS Availablity Zone:</b> $instanceAZ </center>" | ||
| Add-Content -Path C:\inetpub\wwwroot\index.html "<center> <b>Public Hostname:</b> $pubHostName </center>" | ||
| Add-Content -Path C:\inetpub\wwwroot\index.html "<center> <b>Public IPv4:</b> $pubIPv4 </center>" | ||
| Add-Content -Path C:\inetpub\wwwroot\index.html "<center> <b>Private Hostname:</b> $privHostName </center>" | ||
| Add-Content -Path C:\inetpub\wwwroot\index.html "<center> <b>Private IPv4:</b> $privIPv4 </center>" | ||
| Add-Content -Path C:\inetpub\wwwroot\index.html "</font>" | ||
|
|
||
| </powershell> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| variable "name_prefix" {} | ||
| variable "instance_type" {} | ||
| variable "security_groups" {} | ||
| variable "subnet_id" {} | ||
| variable "glue_kms_key_arn" {} | ||
| variable "athena_kms_key_arn" {} | ||
| variable "target_bucket_arn" {} | ||
| variable "athena_bucket_arn" {} | ||
| variable "use_custom_ami" {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
terraform/account-wide-infrastructure/modules/vpc/outputs.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| output "subnet_id" { | ||
| value = aws_subnet.public_subnet.id | ||
| } | ||
|
|
||
| output "private_subnet_id" { | ||
| value = aws_subnet.private_subnet.id | ||
| } | ||
|
|
||
| output "powerbi_gw_security_group_id" { | ||
| value = aws_security_group.powerbi_gw_sg.id | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| variable "aws_azs" {} | ||
| variable "enable_dns_hostnames" {} | ||
| variable "vpc_cidr_block" {} | ||
| variable "vpc_public_subnets_cidr_block" {} | ||
| variable "vpc_private_subnets_cidr_block" {} | ||
| variable "name_prefix" {} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.