Skip to content

Commit

Permalink
feat: ansibleでEC2へgithubからpullしてnext.jsのデプロイまで
Browse files Browse the repository at this point in the history
  • Loading branch information
abcdegolira committed Jul 8, 2024
1 parent ba9cfcc commit 8329cb6
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 1 deletion.
38 changes: 38 additions & 0 deletions .tf/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .tf/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[defaults]
host_key_checking = False
43 changes: 42 additions & 1 deletion .tf/ec2.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
# ec2.tf

resource "null_resource" "generate_ssh_key" {
provisioner "local-exec" {
command = "ssh-keygen -t rsa -f ec2-keypair -y"
}

# キーが既に存在する場合は再生成しないようにする
triggers = {
key_file = "~/.ssh/ec2-keypair"
}
}


# EC2 keypair
resource "aws_key_pair" "ssh_key" {
key_name = "ssh_key"
public_key = file("~/.ssh/ec2-keypair.pub")
#public_key = var.ssh_public_key_path
}

# EC2 instance
resource "aws_instance" "main" {
ami = "ami-0c3fd0f5d33134a76"
ami = "ami-0e0820ad173f20fbb"
instance_type = "t2.micro"
subnet_id = aws_subnet.public.id
associate_public_ip_address = true
Expand All @@ -18,4 +33,30 @@ resource "aws_instance" "main" {
Project = var.project
Env = var.environment
}


provisioner "remote-exec" {
inline = [
"echo 'Wait until SSH is ready'",
#"sudo yum update -y", # Amazon Linux 2の場合
"sudo yum install -y python3"
]

connection {
type = "ssh"
user = "ec2-user"
private_key = file("~/.ssh/ec2-keypair")
#private_key = var.ssh_private_key_path
host = self.public_ip
}
}

provisioner "local-exec" {
#command = "ansible-playbook -i '${self.public_ip},' --private-key ${path.module}/.ssh/ec2-keypair setup_nextjs.yml"
#command = "ansible-playbook -i '${self.public_ip},' --private-key file('~/.ssh/ec2-keypair') setup_nextjs.yml"
#command = "ansible-playbook -i '${self.public_ip},' --private-key ${var.ssh_private_key_path} setup_nextjs.yml"
command = "ansible-playbook -i '${self.public_ip},' -u ec2-user --private-key ${var.ssh_private_key_path} setup_nextjs.yml"

}

}
127 changes: 127 additions & 0 deletions .tf/setup_nextjs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
---
- hosts: all
become: yes #権限昇格してsudo使えるようにする
vars:
node_version: "18.14.1"
project_path: "/home/ec2-user/nextjs-responsive-clear"
tasks:
- name: hello
shell: echo "hello"

#- name: rm yum cache
# shell: sudo rm -R /var/cache/yum/x86_64/2/nodesource/

#Amazon Linux Extrasのトピックnginxを有効にする
#- name: Enable amzn2extra-nginx repository
# ansible.builtin.shell: amazon-linux-extras enable nginx1
# changed_when: false



- name: Install Node.js repository
shell: |
sudo yum install -y gcc-c++ make
curl -sL https://deb.nodesource.com/setup_{{ node_version }} | sudo -E bash -
yum install -y nodejs
- name: Install Node.js and npm
yum:
name:
- nodejs
- gcc-c++
- make
state: present


- name: Install git
yum:
name: git
state: present

- name: Install Next.js
npm:
name: next
global: yes

- name: Check Node.js and npm installation
shell: |
node --version
npm --version
register: node_npm_version

- debug:
var: node_npm_version.stdout_lines

- name: Install pip on Debian/Ubuntu
yum:
name:
- python3-pip
state: present

- name: Install pexpect on the target node
pip:
name: pexpect
state: present

- name: Create project directory
file:
path: "{{ project_path }}"
state: directory
mode: '0755'

- name: Clone/Pull Next.js project from GitHub
git:
repo: "https://github.com/H-Hp/responsive-clear"
dest: "{{ project_path }}"
version: main # または使用するブランチ/タグ
force: yes

- name: Install Node.js dependencies
npm:
path: "{{ project_path }}"

- name: Build the Next.js application
command: npm run build
args:
chdir: '{{ project_path }}'

- name: Start the Next.js application
command: npm run start
args:
chdir: '{{ project_path }}'

# - name: Start Next.js development server
# command: npm run dev
# args:
#chdir: "{{ project_path }}"
# async: 1000
# poll: 0
#echo -e "\n" | npx create-next-app responsive-clear
# - name: Create Next.js project
#shell: |
#command: echo -e "\n" | npm init next-app responsive-clear
# expect:
# command: npx create-next-app responsive-clear --typescript --eslint --tailwind --src-dir --app --import-alias "@/*"
# responses:
# 'Would you like to use TypeScript': 'y\n'
# 'Would you like to use ESLint': 'y\n'
# 'Would you like to use Tailwind CSS': 'y\n'
# 'Would you like to use `src/` directory': 'y\n'
# 'Would you like to use App Router': 'y\n'
# 'Would you like to customize the default import alias': '\n'
# 'Would you like to use experimental `app/` directory with this project': 'y\n'
# timeout: 300
# args:
# chdir: /home/ec2-user
# creates: /home/ec2-user/my-next-app

# - name: Install project dependencies
# npm:
# path: /home/ec2-user/my-next-app

# - name: Start Next.js application
# shell: npm run dev
# async: 1000
# poll: 0
# args:
# chdir: /home/ec2-user/my-next-app
9 changes: 9 additions & 0 deletions .tf/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ variable "vpc_cidr" {
variable "subnet_cidr" {
type = string
description = "public subnet cidr"
}

variable "ssh_private_key_path" {
description = "Path to the SSH key"
type = string
}
variable "ssh_public_key_path" {
description = "Path to the SSH key"
type = string
}

0 comments on commit 8329cb6

Please sign in to comment.