DEV Community

Cover image for Use EC2 user data script run a flask app🌐
JJ Dev
JJ Dev

Posted on

Use EC2 user data script run a flask app🌐

Creating Employee Directory Application automatically with AWS

🔥You can create an EC2 Amazon Linux 2023 user data script
It will run script after you start EC2.

Launch EC2 instance

  1. select the Amazon Machine Image
    Amazon Machine Images can come from AWS, like this Amazon Linux 2023 AMI, for example. This is the one that we will be using, but you can also browse more AMIs that would include AMIs coming from the AWS Marketplace.
    Image description

  2. just a lab, you can choose free tier eligible
    Image description

  3. Next, we have to decide if we want to configure a key pair.
    We are going to select that we are going to proceed without a key pair for this, and the reason for that is because I don't intend to SSH into this instance to connect to it.

  4. Network settings
    Edit Keep the default VPC selection
    Subnet: Choose the first subnet in the dropdown list
    Auto-assign Public IP: Enable🖱️
    Image description
    Image description

  5. Then expanding the advanced details section
    under IAM instance profile choose S3DynamoDBFullAccessRole
    Image description

  6. pasting user data below (change AWS_DEFAULT_REGION)
    click Launch instances
    Image description

For Amazon Linux 2023

#!/bin/bash -ex
wget https://aws-tc-largeobjects.s3-us-west-2.amazonaws.com/DEV-AWS-MO-GCNv2/FlaskApp.zip
unzip FlaskApp.zip
cd FlaskApp/
yum -y install python3-pip
pip install -r requirements.txt
yum -y install stress
export PHOTOS_BUCKET=${SUB_PHOTOS_BUCKET}
export AWS_DEFAULT_REGION=ap-northeast-1
export DYNAMO_MODE=on
FLASK_APP=application.py /usr/local/bin/flask run --host=0.0.0.0 --port=80 
Enter fullscreen mode Exit fullscreen mode

For Amazon Linux 2

#!/bin/bash -ex
wget https://aws-tc-largeobjects.s3-us-west-2.amazonaws.com/DEV-AWS-MO-GCNv2/FlaskApp.zip
unzip FlaskApp.zip
cd FlaskApp/
yum -y install python3 mysql
pip3 install -r requirements.txt
amazon-linux-extras install epel
yum -y install stress
export PHOTOS_BUCKET=${SUB_PHOTOS_BUCKET}
export AWS_DEFAULT_REGION=<INSERT REGION HERE>
export DYNAMO_MODE=on
FLASK_APP=application.py /usr/local/bin/flask run --host=0.0.0.0 --port=80
Enter fullscreen mode Exit fullscreen mode

open address🔚

your Employee Directory Application running~
when you start the instance, the script will execute.

Image description

Image description

Top comments (0)