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

feat/playbook-ec2 #15

Merged
merged 5 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 14 additions & 30 deletions url-shortener/frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,38 +1,22 @@
FROM node:18-alpine AS deps
LABEL authors="arnavrneo"
FROM node:latest

RUN apk add --no-cache libc6-compat
WORKDIR /app
# Set the working directory inside the container
WORKDIR /app

COPY package.json package-lock.json ./
RUN npm install --production
# Copy package.json and package-lock.json to the container
COPY package*.json ./

FROM node:18-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Install dependencies
RUN npm install

ENV NEXT_TELEMETRY_DISABLED 1
# Copy the app source code to the container
COPY . .

RUN npm run build
# Build the Next.js app
RUN npm run build

FROM node:18-alpine AS runner
WORKDIR /app

ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json

USER nextjs

EXPOSE 3000

ENV PORT 3000
# Expose the port the app will run on
EXPOSE 3000

# Start the app
CMD ["npm", "start"]
79 changes: 79 additions & 0 deletions url-shortener/playbook/1_create_ec2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
-
name: Create EC2 instances
hosts: localhost_group
connection: local
gather_facts: false

# vars:
# region: us-east-1
# image: ami-080e1f13689e07408
# instance_type: t2.micro
# sec_group: "sg-0cb1234c398b2a4fe"

vars_prompt:
- name: aws_profile
prompt: "Enter aws profile: "
private: False

- name: region
prompt: "Enter region: "
private: False
default: us-east-1

- name: image
prompt: "Enter ami-id: "
private: False
default: ami-080e1f13689e07408

- name: instance_type
prompt: "Enter instance type: "
private: False
default: t2.micro

- name: sec_group
prompt: "Enter security group id: "
private: False
#default: "sg-0cb1234c398b2a4fe"

tasks:
- name: Create SG
amazon.aws.ec2_security_group:
name: "{{ sec_group }}"
description: "Test sg"
region: "{{ region }}"
aws_profile: "{{ aws_profile }}"
#aws_profile: test-ec2
#aws_access_key: "{{ ec2_access_key }}"
#aws_secret_key: "{{ ec2_secret_key }}"
rules:
- proto: tcp
ports:
- 22
cidr_ip: 0.0.0.0/0
rule_desc: allow_ssh

- proto: tcp
ports:
- 3000
cidr_ip: 0.0.0.0/0
rule_desc: frontend-exposed


- name: Create an instance
amazon.aws.ec2_instance:
key_name: ansible
aws_profile: "{{ aws_profile }}"
#aws_profile: test-ec2
instance_type: "{{ instance_type }}"
region: "{{ region }}"
image_id: "{{ image }}"
security_group: "{{ sec_group }}"
network:
assign_public_ip: true
wait: true
exact_count: 1
tags:
Ansible: urlShortenerServer

...
38 changes: 38 additions & 0 deletions url-shortener/playbook/2_configure_ec2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---

- name: Configure EC2 instance
hosts: ansible
connection: ssh
become: true

tasks:
- name: Update the machine
command: sudo apt-get clean && update -y

- name: Installing essentials
command: apt-get install apt-transport-https ca-certificates curl software-properties-common -y

- name: Installing Curl
command: apt-get install curl -y

- name: Pre-steps
command: "{{ item }}"
with_items:
- curl -fsSL https://get.docker.com -o get-docker.sh
- sudo sh get-docker.sh

- name: Adding user to group
command: sudo usermod -aG docker ${USER}

- name: Restarting docker service
command: sudo service docker restart

- name: Updating the machine 2
command: sudo apt-get update

- name: Installing Python Pip
command: sudo apt-get install python-pip -y

- name: Installing Docker compose
command: sudo apt-get install docker-compose-plugin
...
16 changes: 16 additions & 0 deletions url-shortener/playbook/3_docker_book.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
-
name: Setting up and running docker compose
hosts: ansible
#connection: local
become: yes
#become_user: root

vars_prompt:
- name: docker_input
prompt: "1) Setup Files\n2) Compose up\n3) Compose rm\n4) Copy .env files"
private: False

roles:
- docker
...
6 changes: 5 additions & 1 deletion url-shortener/playbook/ansible.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[defaults]
inventory = hosts
inventory = inventory/hosts
host_key_checking = False
enable_plugins = aws_ec2

[inventory]
enable_plugins = aws_ec2
27 changes: 27 additions & 0 deletions url-shortener/playbook/ansible.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAjBk5iof4j6ooAAnEAMa/UNEhvT8xEWgCOVUOsK3hhor+RGtD
FFpBvVGFsQnMgoxf0wy3lh4r9yL/FoUmIr8jKaXawMhHfBkl09KmvKbtJNbo4BjG
VvAJ/8wDtiESLV0eZ7+3LbUyb7DLTTTVUI/c2qOTkKf0xOmp2d6cZVeLyyw70vpk
rqJihmohKuHb6NYMY8Y5TvxYVa5JGHr//d1VtVZjORw2S5DLJUzRli2fF39iqcxy
GCx6Dp+6Skj9oXy/TrH0JJEzrqknfFDQRVnYHkw7rt9tRWRmjcOKqSNhIuNGysWG
4mkvbkuRgxEbe3Sn4GSj/T9meDF7xhKtVAmiwQIDAQABAoIBAD5pAkwnNGBzkawq
rYkXAKU09M3Ed8xkRBFcRdYJL91qz8iU6+YGKyD2TxNF5TgG5q0zDhmuyTEoAvD6
UbnK4hQskV2uGsI6xbIuNh72UdBGeKlUAA88kYfFT6Xz55AT2nanXifbf2NoI7j6
H0jrPZycoCEh4RVTblnjJFK1BE52dleudyExdGTdSXY2ZLOnfgVZFB/crrTzDHKo
AlJdA6WLXlnI8nSHUMte7EyFlyUk+pVaJiwaT96JOvK4sNWS/oTdrZDhONs58Gfr
AcXzbUBUpVcWRYa46rj0WlnYd9g4piz0h2z31e2gtJzJezpBkHSMfZPoZtjG3bsK
qWTrmh0CgYEAvsv+xrLj4GmFq5cGd3Vp9jhd6U9aZ/eQCkOayQxNFeVp01r5iU4a
hHiDU0047QfwoZfEcJaTFIF5idiOjxDeVoIeKWrHwtNsSNHYJGF9uHdI9gqc23dW
NJG82ozjWJSOl/fhsTMwvIhEPu7VzkHoQDWFEPafV3SvUBxRwg7cZsMCgYEAu/nZ
YtVOYPJ2vk8WAKwZ1LsQH06Rxfu8f4LyUnBR3EQQeqwvzx0H9IZCngGHEHmRuGmz
y9KQfyyzw2FpurfVZYeXwqLb2Td1jave6FUj1OknyZggrqUcYQVAA7eqtNp5I62O
RqD13pu3vIaoMLhlr2uOOgeUyMsBh8c/cAxkICsCgYEApRWap0/njaef6dzt0nS8
LzeMQi/P2H9CmS7vgZ9sH1L1otNnb98tYItpja0WYTjqgtbvOD+AmVCB2byPdMgy
qIVDFbD3kOIgIk23B5t3DSHg9koueFIkO64RysP2fhFj46euFwVk2OvKqO07/4/V
JaFpNkk9i41Blz45m4ACQcMCgYEAqK5qTvBPJ1wtBlX9zpihjW5X52AgG+PsoRgm
z6ePLfO+Pfbp6BeLHfilKxj/1N7izvO7rjnEBPteTQPLVwzEaBmnK7ftRR0pVfpX
S3Grd9HKLHNhUJdFwiJt8WgNvcEXV9bUh7jFKf/ef0TvBNlUFniT5qsK7qyNMQSx
fqD/8IUCgYBFOTjmxqA9lZ1OGzfG4HMly2HmNgBWJ4YDpT74a048V74U7gRIuk/P
tesMxPbmXaRqIZJiRRE6j/5Sv5XSZaGnJaV/359cJ1hbWddki4VEGs5NtP8pJMvY
ERICQvJSNadOgeqqoouDPNe97F2k0NALVP7CNLQyOLN7RduImDA7nA==
-----END RSA PRIVATE KEY-----
15 changes: 0 additions & 15 deletions url-shortener/playbook/dockerBook.yaml

This file was deleted.

45 changes: 0 additions & 45 deletions url-shortener/playbook/ec2_playbook.yaml

This file was deleted.

14 changes: 14 additions & 0 deletions url-shortener/playbook/group_vars/ansible
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
ansible_ssh_private_key_file: ansible.pem
ansible_user: ubuntu
ansible_become: true
#ansible_ssh_common_args: '-o StrictHostKeyChecking=no' # not in prod
# docker_github_token: !vault |
# $ANSIBLE_VAULT;1.1;AES256
# 61333462616136663461316334366130633061393365383763633936616233646334343833666437
# 6465386231656632353038343631333464313533623133330a326230666663366236616337626238
# 63376531323461303935616133653936306630383463666265386236313739633165396634323731
# 3164643166613230630a363361363532323139353837353532663464386631383731353561316165
# 65303061363465653963303662393432633563363736333863626365396532333236336634313031
# 3365373731373437663830623434363332633532636662326231
...
27 changes: 27 additions & 0 deletions url-shortener/playbook/group_vars/ansible.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAjBk5iof4j6ooAAnEAMa/UNEhvT8xEWgCOVUOsK3hhor+RGtD
FFpBvVGFsQnMgoxf0wy3lh4r9yL/FoUmIr8jKaXawMhHfBkl09KmvKbtJNbo4BjG
VvAJ/8wDtiESLV0eZ7+3LbUyb7DLTTTVUI/c2qOTkKf0xOmp2d6cZVeLyyw70vpk
rqJihmohKuHb6NYMY8Y5TvxYVa5JGHr//d1VtVZjORw2S5DLJUzRli2fF39iqcxy
GCx6Dp+6Skj9oXy/TrH0JJEzrqknfFDQRVnYHkw7rt9tRWRmjcOKqSNhIuNGysWG
4mkvbkuRgxEbe3Sn4GSj/T9meDF7xhKtVAmiwQIDAQABAoIBAD5pAkwnNGBzkawq
rYkXAKU09M3Ed8xkRBFcRdYJL91qz8iU6+YGKyD2TxNF5TgG5q0zDhmuyTEoAvD6
UbnK4hQskV2uGsI6xbIuNh72UdBGeKlUAA88kYfFT6Xz55AT2nanXifbf2NoI7j6
H0jrPZycoCEh4RVTblnjJFK1BE52dleudyExdGTdSXY2ZLOnfgVZFB/crrTzDHKo
AlJdA6WLXlnI8nSHUMte7EyFlyUk+pVaJiwaT96JOvK4sNWS/oTdrZDhONs58Gfr
AcXzbUBUpVcWRYa46rj0WlnYd9g4piz0h2z31e2gtJzJezpBkHSMfZPoZtjG3bsK
qWTrmh0CgYEAvsv+xrLj4GmFq5cGd3Vp9jhd6U9aZ/eQCkOayQxNFeVp01r5iU4a
hHiDU0047QfwoZfEcJaTFIF5idiOjxDeVoIeKWrHwtNsSNHYJGF9uHdI9gqc23dW
NJG82ozjWJSOl/fhsTMwvIhEPu7VzkHoQDWFEPafV3SvUBxRwg7cZsMCgYEAu/nZ
YtVOYPJ2vk8WAKwZ1LsQH06Rxfu8f4LyUnBR3EQQeqwvzx0H9IZCngGHEHmRuGmz
y9KQfyyzw2FpurfVZYeXwqLb2Td1jave6FUj1OknyZggrqUcYQVAA7eqtNp5I62O
RqD13pu3vIaoMLhlr2uOOgeUyMsBh8c/cAxkICsCgYEApRWap0/njaef6dzt0nS8
LzeMQi/P2H9CmS7vgZ9sH1L1otNnb98tYItpja0WYTjqgtbvOD+AmVCB2byPdMgy
qIVDFbD3kOIgIk23B5t3DSHg9koueFIkO64RysP2fhFj46euFwVk2OvKqO07/4/V
JaFpNkk9i41Blz45m4ACQcMCgYEAqK5qTvBPJ1wtBlX9zpihjW5X52AgG+PsoRgm
z6ePLfO+Pfbp6BeLHfilKxj/1N7izvO7rjnEBPteTQPLVwzEaBmnK7ftRR0pVfpX
S3Grd9HKLHNhUJdFwiJt8WgNvcEXV9bUh7jFKf/ef0TvBNlUFniT5qsK7qyNMQSx
fqD/8IUCgYBFOTjmxqA9lZ1OGzfG4HMly2HmNgBWJ4YDpT74a048V74U7gRIuk/P
tesMxPbmXaRqIZJiRRE6j/5Sv5XSZaGnJaV/359cJ1hbWddki4VEGs5NtP8pJMvY
ERICQvJSNadOgeqqoouDPNe97F2k0NALVP7CNLQyOLN7RduImDA7nA==
-----END RSA PRIVATE KEY-----
8 changes: 8 additions & 0 deletions url-shortener/playbook/inventory/aws_ec2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plugin: aws_ec2
profile: test-ec2
hostnames:
- ip-address
regions:
- us-east-1
groups:
ansible: "'urlShortenerServer' in tags.Ansible"
File renamed without changes.
34 changes: 34 additions & 0 deletions url-shortener/playbook/roles/docker/tasks/copy_env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
- name: Copying .env's
block:
- name: Copying frontend .env
ansible.builtin.copy:
src: frontend/.env
dest: url-shortener/url-shortener/frontend/
mode: '0644'
register: frontend_copy
when: docker_input == "4"

- name: Copying go server .env
ansible.builtin.copy:
src: go/.env
dest: url-shortener/url-shortener/servers/go/
mode: '0644'
register: go_copy
when: docker_input == "4"

- name: Copying express js .env
ansible.builtin.copy:
src: express_js/.env
dest: url-shortener/url-shortener/servers/express-js
mode: '0644'
register: express_copy
when: docker_input == "4"

- name: Debug Output
ansible.builtin.debug:
var:
- frontend_copy
- go_copy
- express_copy
...
10 changes: 10 additions & 0 deletions url-shortener/playbook/roles/docker/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: Copying the required files
ansible.builtin.import_tasks: setup_files.yaml

- name: Running Docker Compose
ansible.builtin.import_tasks: docker_compose.yaml

- name: Copying env files
ansible.builtin.import_tasks: copy_env.yaml
...
7 changes: 0 additions & 7 deletions url-shortener/playbook/roles/docker/tasks/main.yml

This file was deleted.

Loading
Loading