Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update main.yml #19

Open
wants to merge 67 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
c33b88b
Create main.yml
Ayanfe5 Jun 3, 2023
969a366
Added Dockerfile
Ayanfe5 Jun 3, 2023
92e5eaa
Update main.yml
Ayanfe5 Jun 3, 2023
e86badf
Update main.yml
Ayanfe5 Jun 3, 2023
d263f60
Update Dockerfile
Ayanfe5 Jun 3, 2023
5f33b27
Update app.py
Ayanfe5 Jun 3, 2023
954423f
Update requirements.txt
Ayanfe5 Jun 3, 2023
984e3ff
Added terraform configuration file
Ayanfe5 Jun 5, 2023
f749d47
Create IaC.yml
Ayanfe5 Jun 5, 2023
4836fce
Update IaC.yml
Ayanfe5 Jun 5, 2023
82b9dc7
Rename IaC.yml to IaC-terraform.yml
Ayanfe5 Jun 5, 2023
84f0c90
Updated main.tf
Ayanfe5 Jun 5, 2023
b37618a
Updated main.tf file
Ayanfe5 Jun 5, 2023
1fcfbf7
Update main.tf
wahabajasco Jun 5, 2023
c062216
Adding environment variables
Ayanfe5 Jun 5, 2023
cc47633
Updating environment variables
Ayanfe5 Jun 5, 2023
1f36078
Updating environment variables
Ayanfe5 Jun 5, 2023
ed8643b
Updating environment variables
Ayanfe5 Jun 5, 2023
4e2887d
Update IaC-terraform.yml
Ayanfe5 Jun 5, 2023
a009ed5
Update IaC-terraform.yml
Ayanfe5 Jun 5, 2023
bfcf6f0
Update IaC-terraform.yml
Ayanfe5 Jun 6, 2023
d0046ed
Create main.tf
Ayanfe5 Jun 6, 2023
bf236bd
Create variables.tf
Ayanfe5 Jun 6, 2023
d1fb0df
Delete main.tf
Ayanfe5 Jun 6, 2023
25c2823
Delete variables.tf
Ayanfe5 Jun 6, 2023
45a31d3
Update main.tf
Ayanfe5 Jun 6, 2023
6ddd032
Update IaC-terraform.yml
Ayanfe5 Jun 6, 2023
7a8aa75
Create providers.tf
Ayanfe5 Jun 6, 2023
71efd65
Update providers.tf
Ayanfe5 Jun 6, 2023
64bdbe7
Update IaC-terraform.yml
Ayanfe5 Jun 6, 2023
17d8d6e
Update main.tf
Ayanfe5 Jun 6, 2023
db378c9
Update providers.tf
Ayanfe5 Jun 6, 2023
7e957ee
Update providers.tf
Ayanfe5 Jun 6, 2023
b284624
Update IaC-terraform.yml
Ayanfe5 Jun 6, 2023
0e1ac8f
Update providers.tf
Ayanfe5 Jun 6, 2023
63c9580
Update providers.tf
Ayanfe5 Jun 6, 2023
ca3a706
Update IaC-terraform.yml
Ayanfe5 Jun 6, 2023
2c2369b
Create inventory.txt
Ayanfe5 Jun 6, 2023
0f8f55e
Create playbooks.yml
Ayanfe5 Jun 6, 2023
5d4da1b
Rename playbooks.yml to playbook.yml
Ayanfe5 Jun 6, 2023
94a9dbf
Create vars.yml
Ayanfe5 Jun 6, 2023
c47bdfe
Create ansible.cfg
Ayanfe5 Jun 6, 2023
86ba491
Delete CHANGES.md
Ayanfe5 Jun 6, 2023
febba33
Update playbook.yml
Ayanfe5 Jun 6, 2023
cb00b79
Delete playbook.yml
Ayanfe5 Jun 6, 2023
2fba7d0
Create playbook.yml
Ayanfe5 Jun 6, 2023
876eea2
Update providers.tf
Ayanfe5 Jun 6, 2023
744153f
Update providers.tf
Ayanfe5 Jun 6, 2023
0f7a2a0
Update IaC-terraform.yml
Ayanfe5 Jun 6, 2023
89eac1d
Update providers.tf
Ayanfe5 Jun 6, 2023
aa77433
Create README.md
Ayanfe5 Jun 7, 2023
5d747b0
Create README.md
Ayanfe5 Jun 7, 2023
c6821c0
Create README.md
Ayanfe5 Jun 7, 2023
25878a2
Update providers.tf
Ayanfe5 Jun 7, 2023
9f0fb9a
Update providers.tf
Ayanfe5 Jun 7, 2023
780a3a0
Update README.md
Ayanfe5 Jun 7, 2023
c562147
Update README.md
Ayanfe5 Jun 7, 2023
cab4c4f
Update providers.tf
Ayanfe5 Jun 7, 2023
6558382
Update main.tf
Ayanfe5 Jun 7, 2023
e595a10
Update IaC-terraform.yml
Ayanfe5 Jun 7, 2023
b888560
Create terraform.tfvars
Ayanfe5 Jun 7, 2023
bdd90ef
Update terraform.tfvars
Ayanfe5 Jun 7, 2023
6505054
Update terraform.tfvars
Ayanfe5 Jun 7, 2023
6fb7320
Update IaC-terraform.yml
Ayanfe5 Jun 7, 2023
d3e6614
Update IaC-terraform.yml
Ayanfe5 Jun 7, 2023
ea77566
Update IaC-terraform.yml
Ayanfe5 Jun 7, 2023
0bff45b
Update IaC-terraform.yml
Ayanfe5 Jun 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
48 changes: 48 additions & 0 deletions .github/workflows/IaC-terraform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Deploy EC2 instance to AWS

on:
workflow_dispatch:

env:
AWS_REGION: us-west-1

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 1.0.0

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}

- name: Initialize Terraform
working-directory: IaC
run: terraform init -backend-config="bucket=appsilon-task" -backend-config="key=appsilon-terraform.tfstate" -backend-config="region=${{ env.AWS_REGION }}"

- name: Terraform Validation
working-directory: IaC
run: terraform validate

- name: Terraform Apply
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
working-directory: IaC
run: terraform apply -auto-approve -input=false

- name: Save Terraform state file to S3
working-directory: IaC
run: |
aws s3 cp .terraform/terraform.tfstate s3://appsilon-task/appsilon-terraform.tfstate

32 changes: 32 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Build and Push

on:
push:
branches:
- main
schedule:
- cron: "0 19 * * 6" # Every Saturday at 7 PM

jobs:
build-and-push:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Build and push image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ayanfe5/hello-world:latest
20 changes: 20 additions & 0 deletions Ansible/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Creating New User and Installations using Ansible

After spinning up the server, these commands were ran on it to updates and also installed ansible:

```bash
sudo apt update
sudo apt install ansible -y
```
All the IP addresses of the target PC I configured was added to the inventory.txt file, whilst the public key of the host server where ansible is installed on was added to the authorized key file of the target server, so as to enable ease of logging in without password. The commands below were the steps I took to run the playbook.yml

```bash
mkdir Appsilonansible
cd Appsilonansible
sudo nano inventory.txt
sudo nano ansible.cfg
sudo nano vars.yml
sudo nano playbook.yml
ansible-playbook -i inventory.txt playbook.yml
```
After, running all these commands, the task was successful, I navigated into the target system to confirm all the changes were made.
2 changes: 2 additions & 0 deletions Ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[defaults]
inventory = inventory.txt
2 changes: 2 additions & 0 deletions Ansible/inventory.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target]
target_instance ansible_host=13.52.216.75
69 changes: 69 additions & 0 deletions Ansible/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
- name: Configure new users and install packages
hosts: all
become: true

tasks:
- name: Create the script locally
copy:
content: |
#!/bin/bash
mount
dest: nice-script.sh
mode: 0755
delegate_to: localhost

- name: Create the directory on the remote machine
file:
path: "/better-place/{{ item }}"
state: directory
mode: '0755'
recurse: yes
loop: "{{ ansible_users }}"
vars:
ansible_users:
- john

- name: Create the user
user:
name: "{{ item }}"
uid: 1234
createhome: yes
home: "/better-place/{{ item }}"
loop: "{{ ansible_users }}"
vars:
ansible_users:
- john

- name: Grant sudo access without password
lineinfile:
dest: /etc/sudoers
line: "{{ item }} ALL=(ALL) NOPASSWD: /usr/bin/whoami"
validate: 'visudo -cf %s'
loop: "{{ ansible_users }}"
vars:
ansible_users:
- john

- name: Install packages
apt:
name: "{{ item }}"
state: present
loop:
- tmux
- vim

- name: Fetch latest Terraform version
uri:
url: "https://releases.hashicorp.com/terraform/"
return_content: yes
register: terraform_versions

- name: Parse latest Terraform version
set_fact:
latest_terraform_version: "{{ terraform_versions.content | regex_findall('terraform/([\\d.]+)/') | last }}"

- name: Download and install Terraform CLI
get_url:
url: "https://releases.hashicorp.com/terraform/{{ latest_terraform_version }}/terraform_{{ latest_terraform_version }}_linux_amd64.zip"
dest: /tmp/terraform.zip
3 changes: 3 additions & 0 deletions Ansible/vars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
ansible_users:
- john
3 changes: 0 additions & 3 deletions CHANGES.md

This file was deleted.

20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use an official Python runtime as the base image
FROM python:3.8-alpine

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY hello/app.py /app

# Copy the requirements.txt file from the hello folder into the container at /app
COPY hello/requirements.txt /app/requirements.txt

# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Expose the port on which the Flask app will run
EXPOSE 5000

# Define the command to run your application
CMD ["python", "app.py"]
26 changes: 26 additions & 0 deletions IaC/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Creating an EC2 Instance on AWS using Terraform (IaC)

IaC was used in spinning up my EC2 instance on AWS and the tool used was terraform.

I created a providers.tf, variables.tf and main.tf files to contain all the necessary dependencies required for each IaC job to run seamlessly.
Every of the content in the file was tailored to reference AWS since that is the provider I am using with terraform.
These commands were ran to initialize and also test the terraform script on my local machine.

```bash
Terraform init
Terraform validate
Terraform plan
Terraform apply
```
Also in the IaC-terraform.yml workflow, I specified that terraform should save the statefile to my S3 bucket already created with the public access and bucket policy updated and I also added the IAM user credential (Access key and Secret key) to secrets options in github secret for ease of accessing AWS platform.

## Specifications used in the terraform files

```bash
AMI:Canonical, Ubuntu, 22.04 LTS, amd64 jammy image build on 2023-05-16
AMI ID:ami-0f8e81a3da6e2510a
Instance type: t2.micro
Size: 1vCPU, 1Gb RAM
region: us-west-1
S3 Bucket: appsilon-task
```
11 changes: 11 additions & 0 deletions IaC/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "aws_instance" "appsilon-task" {
ami = "ami-0f8e81a3da6e2510a" # Ubuntu 22.04 LTS
instance_type = "t2.micro"

tags = {
Name = "Ubuntu EC2 Instance"
}
key_name = "appsilon-key"
vpc_security_group_ids = ["sg-068f9f3a590c31fae"]

}
23 changes: 23 additions & 0 deletions IaC/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Configure the AWS provider and version
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3"
}
}

backend "s3" {
bucket = "appsilon-task"
key = "appsilon-terraform.tfstate"
region = "us-west-1"
}

#required_version = ">= 1.1.0"
}

provider "aws" {
access_key = var.aws_access_key
secret_key = var.aws_secret_access_key
region = "us-west-1"
}
6 changes: 6 additions & 0 deletions IaC/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
aws_access_key = "AKIAZGJDMTD7E3QJJB4N"
aws_secret_access_key = "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"




9 changes: 9 additions & 0 deletions IaC/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "aws_access_key" {
description = "AWS access key"
type = string
}

variable "aws_secret_access_key" {
description = "AWS secret access key"
type = string
}
1 change: 0 additions & 1 deletion assets/.flaskenv

This file was deleted.

51 changes: 0 additions & 51 deletions assets/app.py

This file was deleted.

Loading