From da0b5c87372653a159e7bf07be43f380f41b2aba Mon Sep 17 00:00:00 2001 From: Andy Miles Date: Thu, 21 Nov 2024 11:22:39 -0800 Subject: [PATCH] latest version in Monday's lesson --- terraform/main.tf | 14 ++++------- terraform/main.tf_former | 52 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 terraform/main.tf_former diff --git a/terraform/main.tf b/terraform/main.tf index 0300f73..a173596 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -12,15 +12,11 @@ resource "aws_instance" "py_server" { 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 + + # Downloading app.py from S3 + # must first aws s3 cp /Users/andy/ws/projects/andy/tfgha/src/tfgha/app.py s3://proquickly/apps/tfgha/app.py + aws s3 cp s3://proquickly/apps/tfgha/app.py /home/ubuntu/app/app.py + cd /home/ubuntu/app nohup python3 app.py & EOF diff --git a/terraform/main.tf_former b/terraform/main.tf_former new file mode 100644 index 0000000..0300f73 --- /dev/null +++ b/terraform/main.tf_former @@ -0,0 +1,52 @@ +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"] + } +}