Skip to content

Commit 5a9c747

Browse files
committed
Update main.tf
Accept node version and cpu architecture as parameters
1 parent b4d8778 commit 5a9c747

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

terraform/node/ec2/default/main.tf

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ data "aws_ami" "ami" {
4646
most_recent = true
4747
filter {
4848
name = "name"
49-
values = ["al20*-ami-minimal-*-x86_64"]
49+
values = ["al20*-ami-minimal-*-${var.cpu_architecture}"]
5050
}
5151
filter {
5252
name = "state"
5353
values = ["available"]
5454
}
5555
filter {
5656
name = "architecture"
57-
values = ["x86_64"]
57+
values = ["${var.cpu_architecture}"]
5858
}
5959
filter {
6060
name = "image-type"
@@ -108,7 +108,21 @@ resource "null_resource" "main_service_setup" {
108108
#!/bin/bash
109109
110110
# Set up environment
111-
sudo yum install nodejs unzip wget tmux aws-cli -y
111+
sudo yum install unzip wget tmux aws-cli -y
112+
113+
# Install nvm
114+
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
115+
export NVM_DIR="$HOME/.nvm"
116+
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
117+
118+
# Install the specified Node.js version, or use the system's version if 'none'
119+
if [[ "${var.language_version}" != "none" ]]; then
120+
nvm install ${var.language_version}
121+
nvm use ${var.language_version}
122+
else
123+
sudo yum install nodejs -y
124+
echo "Using the default Node.js version provided by the OS"
125+
fi
112126
113127
echo "Node version in use: $(node -v)"
114128
@@ -205,7 +219,21 @@ resource "null_resource" "remote_service_setup" {
205219
#!/bin/bash
206220
207221
# Set up environment
208-
sudo yum install nodejs unzip wget tmux aws-cli -y
222+
sudo yum install unzip wget tmux aws-cli -y
223+
224+
# Install nvm
225+
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
226+
export NVM_DIR="$HOME/.nvm"
227+
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
228+
229+
# Install the specified Node.js version, or use the system's version if 'none'
230+
if [[ "${var.language_version}" != "none" ]]; then
231+
nvm install ${var.language_version}
232+
nvm use ${var.language_version}
233+
else
234+
sudo yum install nodejs -y
235+
echo "Using the default Node.js version provided by the OS"
236+
fi
209237
210238
echo "Node version in use: $(node -v)"
211239

terraform/node/ec2/default/variables.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ variable "user" {
2525
default = "ec2-user"
2626
}
2727

28+
variable "cpu_architecture" {
29+
default = "x86_64" # an alternative value is "arm64"
30+
}
31+
32+
variable "language_version" {
33+
# none means to use the version packaged with the OS
34+
# other alternatives are "14", "16", "18", "20", "22"
35+
default = "none"
36+
}
37+
2838
variable "sample_app_zip" {
2939
default = "s3://<bucket-name>/<zip>"
3040
}

0 commit comments

Comments
 (0)