Skip to content

Commit 70c2e55

Browse files
committed
NRL-1385 create ec2 instance
1 parent b386a49 commit 70c2e55

File tree

7 files changed

+99
-196
lines changed

7 files changed

+99
-196
lines changed
Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,8 @@
1-
# Initial AMI to use
2-
data "aws_ami" "windows" {
1+
data "aws_ami" "windows-2019" {
32
most_recent = true
3+
owners = ["amazon"]
44
filter {
55
name = "name"
6-
values = ["Windows_Server-2022-English-Full-Base-*"]
6+
values = ["Windows_Server-2019-English-Full-Base*"]
77
}
8-
filter {
9-
name = "virtualization-type"
10-
values = ["hvm"]
11-
}
12-
owners = ["801119661308"] # Canonical
13-
}
14-
15-
# AMI to use
16-
data "aws_ami" "final" {
17-
most_recent = true
18-
filter {
19-
name = "name"
20-
values = ["PowerBI-On-Premise-Gateway"]
21-
}
22-
filter {
23-
name = "virtualization-type"
24-
values = ["hvm"]
25-
}
26-
owners = ["self"]
27-
}
28-
29-
# Subnets
30-
data "aws_subnets" "subnets" {
31-
filter {
32-
name = "tag:Type"
33-
values = ["private"]
34-
}
35-
}
36-
37-
# VPC
38-
data "aws_vpc" "account_vpc" {
39-
filter {
40-
name = "tag:Name"
41-
values = [var.account_name]
42-
}
43-
}
44-
45-
# Security group of db
46-
data "aws_security_group" "db_sg" {
47-
filter {
48-
name = "group-name"
49-
values = [var.db_sg_name]
50-
}
51-
52-
vpc_id = data.aws_vpc.account_vpc.id
538
}
Lines changed: 8 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,11 @@
1-
module "autoscaling" {
2-
source = "registry.terraform.io/terraform-aws-modules/autoscaling/aws"
3-
version = "6.5.2"
1+
# Create the Linux EC2 Web server
2+
resource "aws_instance" "web" {
3+
ami = data.aws_ami.windows-2019.id
4+
instance_type = var.instance_type
5+
key_name = var.instance_key
6+
subnet_id = var.subnet_id
7+
security_groups = var.security_groups
48

5-
name = "PowerBI-On-Premise-Gateway"
9+
user_data = file("./modules/web/userdata.tpl")
610

7-
min_size = 0
8-
max_size = 1
9-
desired_capacity = 1
10-
11-
# Autoscaling Schedule
12-
schedules = {
13-
morning_start = {
14-
min_size = -1
15-
max_size = -1
16-
desired_capacity = 1
17-
recurrence = "50 5 * * 0-6"
18-
time_zone = "Europe/Paris"
19-
}
20-
21-
morning_stop = {
22-
min_size = -1
23-
max_size = -1
24-
desired_capacity = 0
25-
recurrence = "30 6 * * 0-6"
26-
time_zone = "Europe/Paris"
27-
}
28-
29-
noon_start = {
30-
min_size = -1
31-
max_size = -1
32-
desired_capacity = 1
33-
recurrence = "50 11 * * 0-6"
34-
time_zone = "Europe/Paris"
35-
}
36-
37-
noon_stop = {
38-
min_size = -1
39-
max_size = -1
40-
desired_capacity = 0
41-
recurrence = "30 12 * * 0-6"
42-
time_zone = "Europe/Paris"
43-
}
44-
45-
evening_start = {
46-
min_size = -1
47-
max_size = -1
48-
desired_capacity = 1
49-
recurrence = "50 17 * * 0-6"
50-
time_zone = "Europe/Paris"
51-
}
52-
53-
evening_stop = {
54-
min_size = -1
55-
max_size = -1
56-
desired_capacity = 0
57-
recurrence = "30 18 * * 0-6"
58-
time_zone = "Europe/Paris"
59-
}
60-
}
61-
62-
wait_for_capacity_timeout = 0
63-
health_check_type = "EC2"
64-
health_check_grace_period = 300
65-
enable_monitoring = false
66-
67-
#image_id = data.aws_ami.windows.id // Phase 2 : we let the Auto Scaling Group use the AMI we've juste created.
68-
image_id = data.aws_ami.final.id
69-
launch_template_version = "$Latest"
70-
instance_type = "m5a.large"
71-
72-
instance_market_options = {
73-
market_type = "spot"
74-
}
75-
76-
# Refresh instances when redeploying
77-
instance_refresh = {
78-
strategy = "Rolling"
79-
triggers = ["tag"]
80-
}
81-
82-
# Assign a role to the instance
83-
create_iam_instance_profile = true
84-
iam_role_name = "powerbi-gateway-role"
85-
iam_role_description = "Allow the Power BI Gateway to be managed by"
86-
iam_role_policies = {
87-
AmazonSSMManagedInstanceCore = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
88-
}
89-
key_name = aws_key_pair.ec2_key_pair.key_name
90-
91-
vpc_zone_identifier = data.aws_subnets.subnets.ids
92-
93-
security_groups = [module.security-group-outbound.security_group_id]
94-
95-
user_data = filebase64("./userdata.txt")
96-
update_default_version = true
97-
}
98-
99-
############################
100-
# Add rule to db managed group
101-
############################
102-
# module "upgrade_db_sg" {
103-
# source = "registry.terraform.io/terraform-aws-modules/security-group/aws"
104-
# version = "4.13.1"
105-
106-
# create_sg = false
107-
# security_group_id = data.aws_security_group.db_sg.id
108-
# ingress_with_source_security_group_id = [
109-
# {
110-
# description = "Allow incoming connections from Power BI Gateway"
111-
# rule = "postgresql-tcp"
112-
# source_security_group_id = module.security-group-outbound.security_group_id
113-
# },
114-
# ]
115-
# }
116-
117-
############################
118-
# Key pair for RDP access
119-
############################
120-
resource "tls_private_key" "instance_key_pair" {
121-
algorithm = "RSA"
122-
}
123-
124-
resource "aws_key_pair" "ec2_key_pair" {
125-
key_name = "PowerBI-GateWay-Key"
126-
public_key = tls_private_key.instance_key_pair.public_key_openssh
127-
}
128-
129-
# Saving Key Pair for ssh login for Client if needed
130-
resource "local_file" "ssh_key" {
131-
filename = "${aws_key_pair.ec2_key_pair.key_name}.pem"
132-
content = tls_private_key.instance_key_pair.private_key_pem
13311
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "instance_id" {
2+
value = aws_instance.web.id
3+
}
4+
5+
output "public_ip" {
6+
value = aws_instance.web.public_ip
7+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Requires Module -Name DataGateway
2+
3+
# Check if the required module is installed
4+
try {
5+
Import-Module DataGateway
6+
}
7+
catch {
8+
Write-Warning "The DataGateway module is not installed. Please install it first using Install-Module -Name DataGateway."
9+
return
10+
}
11+
12+
# Replace with your desired values
13+
$GatewayName = "MyGatewayCluster"
14+
$GatewayMemberName = "MyGatewayMember"
15+
$GatewayAdminUser = "[email protected]" # Optional, replace with user's email
16+
17+
# 1. Add a new gateway cluster
18+
Write-Host "Adding a new gateway cluster..."
19+
try {
20+
Add-DataGatewayCluster -Name $GatewayName -OverwriteExistingGateway
21+
}
22+
catch {
23+
Write-Error "Error adding gateway cluster: $($_.Exception.Message)"
24+
return
25+
}
26+
27+
# 2. Add a member to the gateway cluster
28+
Write-Host "Adding a gateway member to the cluster..."
29+
try {
30+
Add-DataGatewayClusterMember -ClusterId $GatewayName -Name $GatewayMemberName -OverwriteExistingGateway
31+
}
32+
catch {
33+
Write-Error "Error adding gateway member: $($_.Exception.Message)"
34+
return
35+
}
36+
37+
# 3. (Optional) Add users as gateway administrators
38+
if ($GatewayAdminUser) {
39+
Write-Host "Adding user as a gateway administrator..."
40+
try {
41+
Add-DataGatewayClusterUser -ClusterId $GatewayName -UserEmail $GatewayAdminUser -Permission "Admin"
42+
}
43+
catch {
44+
Write-Error "Error adding gateway admin: $($_.Exception.Message)"
45+
return
46+
}
47+
}
48+
49+
Write-Host "Gateway cluster and member added successfully."
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
3+
<powershell>
4+
Install-WindowsFeature -name Web-Server -IncludeManagementTools
5+
6+
$instanceId = (Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/instance-id -UseBasicParsing).content
7+
$instanceAZ = (Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/placement/availability-zone -UseBasicParsing).content
8+
$pubHostName = (Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/public-hostname -UseBasicParsing).content
9+
$pubIPv4 = (Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/public-ipv4 -UseBasicParsing).content
10+
$privHostName = (Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/local-hostname -UseBasicParsing).content
11+
$privIPv4 = (Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/local-ipv4 -UseBasicParsing).content
12+
13+
New-Item -Path C:\inetpub\wwwroot\index.html -ItemType File -Force
14+
Add-Content -Path C:\inetpub\wwwroot\index.html "<font face = "Verdana" size = "5">"
15+
Add-Content -Path C:\inetpub\wwwroot\index.html "<center><h1>AWS Windows VM Deployed with Terraform</h1></center>"
16+
Add-Content -Path C:\inetpub\wwwroot\index.html "<center> <b>EC2 Instance Metadata</b> </center>"
17+
Add-Content -Path C:\inetpub\wwwroot\index.html "<center> <b>Instance ID:</b> $instanceId </center>"
18+
Add-Content -Path C:\inetpub\wwwroot\index.html "<center> <b>AWS Availablity Zone:</b> $instanceAZ </center>"
19+
Add-Content -Path C:\inetpub\wwwroot\index.html "<center> <b>Public Hostname:</b> $pubHostName </center>"
20+
Add-Content -Path C:\inetpub\wwwroot\index.html "<center> <b>Public IPv4:</b> $pubIPv4 </center>"
21+
Add-Content -Path C:\inetpub\wwwroot\index.html "<center> <b>Private Hostname:</b> $privHostName </center>"
22+
Add-Content -Path C:\inetpub\wwwroot\index.html "<center> <b>Private IPv4:</b> $privIPv4 </center>"
23+
Add-Content -Path C:\inetpub\wwwroot\index.html "</font>"
24+
25+
</powershell>

terraform/account-wide-infrastructure/modules/ec2/userdata.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
variable "aws_region" {
2-
description = "Default region where to deploy resources"
3-
type = string
4-
}
5-
6-
## Account
7-
variable "account_name" {
8-
description = "Account where to deploy VPC"
9-
type = string
10-
}
11-
12-
## Account
13-
variable "db_sg_name" {
14-
description = "Name of edeal security group"
1+
variable "name_prefix" {
152
type = string
3+
description = "The prefix to apply to all resources in the module."
164
}
5+
variable "common_tags" {}
6+
variable "instance_type" {}
7+
variable "instance_key" {}
8+
variable "security_groups" {}
9+
variable "subnet_id" {}

0 commit comments

Comments
 (0)