From 8c7771b28846af96859925fc4913dad88293b4c2 Mon Sep 17 00:00:00 2001 From: Andy Miles Date: Thu, 5 Dec 2024 16:21:16 -0800 Subject: [PATCH] added PROJ to bash script in main.tf --- README.md | 2 ++ terraform/main.tf | 7 +++--- terraform/main.tf_former | 52 ---------------------------------------- 3 files changed, 6 insertions(+), 55 deletions(-) delete mode 100644 terraform/main.tf_former diff --git a/README.md b/README.md index 134d82f..34f8026 100644 --- a/README.md +++ b/README.md @@ -57,4 +57,6 @@ is setup correctly with poetry. TODO: install the project as a module with poetry build and pip or poetry install from the dist dir whl file. +TODO: Variable for bash script in main.tf for project and package name etc + Also note that the dealy in deployment during the time the EC2 instance is up is caused by the time it takes to clone the github repo. diff --git a/terraform/main.tf b/terraform/main.tf index 4b219db..9eba27f 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -32,18 +32,19 @@ resource "aws_instance" "py_server" { key_name = aws_key_pair.deployer.key_name user_data = <<-EOF #!/bin/bash + PROJ=tfgha sudo apt-get update sudo apt-get install -y python3 python3-pip git curl python3 -m pip install -U poetry cd /home/ubuntu - git clone https://github.com/proquickly/tfgha.git - cd /home/ubuntu/tfgha + git clone https://github.com/proquickly/$PROJ.git + cd /home/ubuntu/$PROJ /usr/local/bin/poetry lock /usr/local/bin/poetry install - cd /home/ubuntu/tfgha/src/tfgha + cd /home/ubuntu/$PROJ/src/$PROJ nohup poetry run python app.py & EOF diff --git a/terraform/main.tf_former b/terraform/main.tf_former deleted file mode 100644 index 0300f73..0000000 --- a/terraform/main.tf_former +++ /dev/null @@ -1,52 +0,0 @@ -provider "aws" { - region = "us-west-2" -} - -resource "aws_instance" "py_server" { - ami = "ami-06946f6c9b153d494" - instance_type = "t2.micro" - - user_data = <<-EOF - #!/bin/bash - sudo apt-get update - sudo apt-get install -y python3 python3-pip - pip3 install flask requests - mkdir -p /home/ubuntu/app - cat < /home/ubuntu/app/app.py - from flask import Flask - app = Flask(__name__) - @app.route('/') - def hello(): - return "Hello from Python on AWS via terraform in our lesson" - if __name__ == '__main__': - app.run(host='0.0.0.0', port=5000) - EOL - cd /home/ubuntu/app - nohup python3 app.py & - EOF - - tags = { - Name = "FlaskAppInstance" - } - - vpc_security_group_ids = [aws_security_group.allow_http.id] -} - -resource "aws_security_group" "allow_http" { - name = "allow_http_flask_web" - description = "Allow inbound HTTP traffic" - - ingress { - from_port = 5000 - to_port = 5000 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - egress { - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] - } -}