- Download Putty and WinSCP.
- Create an Ubuntu EC2 instance using AWS console.
- Allow
All traffic
fromAnywhere
in security groups of EC2 Instance. - Create a Key pair in .pem or .ppk format and download it.
- Now select the created instance and click on
connect
. - Navigate to SSH client and copy the
public DNS
. - Open WinSCP and paste the copied
public DNS
inHost name
. - Now, Navigate to
EC2 Instance Connect
and copy theusername
and paste it inusername
in WinSCP. - For
Password
in WinSCP click onAdvanced
--> navigate to SSH --> Authentication, then inprivate key file
input box browse for .pem or .ppk file that we earlier downloaded. - If file is in .pem format than it will get automatically converted to .ppk and click
Yes
if it ask for permissions. - click on Login
and click on
Yesto do the authentication and for connecting to Host EC2 machine on AWS.
- Drop the files from your local machine to Host EC2 ubuntu machine by drag and drop or by right click in file --> upload button.
- A Putty terminal will get opened.
- Install Python EC2 using Putty
sudo apt install python3
- Update all packages and install pip python package manager.
sudo apt-get update && sudo apt-get install python3-pip
- Install all requirements for the Flask app to run using:
pip3 install -r requirements.txt
- Run python app using:
python app.py
-
Paste the
public DNS
that we have copied into browser with port:8080 and see your app runing on EC2. -
Use below command to run your app even after closing Putty terminal.
screen -R deploy python3 app.py
- To close the running app repeate above step again and ctrl+c to close session which created using above command.
* Dockerfile
* compose.yaml (If using docker-compose for creating docker image)
* .gitHub\workflows\main.yaml
While running the CI/CD pipeline using GitHub Actions for first time, below script should not be in comments.
- name: Stop and remove container if running
run: |
docker ps -q --filter "name=texts" | grep -q . && docker stop texts && docker rm -fv texts
You can test with docker images using docker compose in localhost
docker init
to create the necessary Docker assets to containerize your application with the followung steps as follows:
docker init
-
docker init
provides some default configuration, but you'll need to answer a few questions about your application. For example, this application uses Flask to run. Refer to the following example to answer the prompts from docker init and use the same answers for your prompts. -
Following files will be added:
- Run the application using following command in a terminal.
docker compose up --build
# detached mode
docker compose up --build -d
-
Open a browser and view the application at http://localhost:5000
-
In the terminal, run the following command to stop the application.
docker compose down
1. Build docker image of the source code
2. Push your docker image to ECR
3. Launch Your EC2
4. Pull Your image from ECR in EC2
5. Launch your docker image in EC2
- Login to AWS console
- Create IAM user for deployment --> attach following policies
AmazonEC2ContainerRegistryFullAccess
AmazonEC2FullAccess
- Navigate to
Security Credentials
for this IAM user -->Access keys
-->Create Access Keys
- Create Access Keys using
CLI
option --> Download your access keys in .csv file.
- Create ECR repository in AWS by searching for ECR -->
Get started
- Keep the ECR repository private.
- Provide a name to ECR repository.
- Copy the ECR repository URI.
URI : 566373416292.dkr.ecr.us-east-1.amazonaws.com/myapp
- Create a ubuntu EC2 virtual machine on AWS.
- Do configuration as per your requirements.
- Create a Key-pair for your EC2 instance and download it.
- In
Network Settings
check the following:
Allow SSH traffic from the 0.0.0.0/0 (Anywhere)
Allow HTTPS traffic from the Internet
Allow HTTP traffic from the Internet
- Click on
Launch Instance
to create a new instance of EC2. - Click on
Instance ID
for this EC2 instance -->Connect
- Navigate to
EC2 Instance connect
-->Connect
then a terminal of EC2 ubuntu machine will get opened in browser.
- Update and upgrade the packages using commands on terminal
sudo apt-get update -y
sudo apt-get upgrade
- Download docker using:
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
- Add the user "ubuntu" to the "docker" group
sudo usermod -aG docker ubuntu
- switching your primary group to the "docker" group
newgrp docker
- Go to your GitHub Project -->
Settings
-->Actions
-->Runners
-->New self-hosted runner
- select the
Linux
and run the following commands on Ubuntu EC2 instance terminal for downloading GitHub Actions Runner.
# Create a folder
$ mkdir actions-runner && cd actions-runner
# Download the latest runner package
$ curl -o actions-runner-linux-x64-2.314.1.tar.gz -L https://github.com/actions/runner/releases/download/v2.314.1/actions-runner-linux-x64-2.314.1.tar.gz
# Optional: Validate the hash
$ echo "6c726a118bbe02cd32e222f890e1e476567bf299353a96886ba75b423c1137b5 actions-runner-linux-x64-2.314.1.tar.gz" | shasum -a 256 -c
# Extract the installer
$ tar xzf ./actions-runner-linux-x64-2.314.1.tar.gz
- Configure the GitHub Actions Runner using Commands:
# Create the runner and start the configuration experience
$ ./config.sh --url https://github.com/Gourav052003/Iris-WebApp-Deployement-on-AWS --token ATOIALLTYUWFB54KS62L5NTGAKAKI
# user name of runner group as --> self-hosted
# Last step, run it!
$ ./run.sh
- Navigate to
Settings
-->Secrets and variables
-->Actions
- Add following secrets by clicking on
New repositiry secret
AWS_ACCESS_KEY_ID= in .csv file we downloaded
AWS_SECRET_ACCESS_KEY= in .csv file we downloaded
AWS_REGION = us-east-1
AWS_ECR_LOGIN_URI = 566373416292.dkr.ecr.ap-south-1.amazonaws.com
ECR_REPOSITORY_NAME = myapp
- open EC2 instance -->
Security
--> click onsecurity groups
- Edit
Inbound Rules
-->Add Rule
--> save rules
Custom TCP, Anywhere traffic , port 5000
- open Public IP with port :5000 in EC2 instance to see your app running on EC2