From cb3eafc2114058517a16991783f06f3c1ac5cc6d Mon Sep 17 00:00:00 2001 From: damienmwene Date: Wed, 16 Oct 2024 18:41:55 +0100 Subject: [PATCH 1/6] Updated tf files --- aws_migration/main.tf | 2 +- aws_migration/variables.tf | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/aws_migration/main.tf b/aws_migration/main.tf index 2264992..1af98e2 100644 --- a/aws_migration/main.tf +++ b/aws_migration/main.tf @@ -1,5 +1,5 @@ provider "aws" { - region = "eu-north-1" + region = "us-east-1" } # VPC diff --git a/aws_migration/variables.tf b/aws_migration/variables.tf index 0fa5695..79689a6 100644 --- a/aws_migration/variables.tf +++ b/aws_migration/variables.tf @@ -1,7 +1,7 @@ variable "ami" { description = "The AMI ID for the EC2 instance" type = string - default = "ami-0ebf6e3febf87e406" + default = "ami-00f251754ac5da7f0" } variable "instance_type" { @@ -13,5 +13,5 @@ variable "instance_type" { variable "availability_zone" { description = "The availability zone for the subnet" type = string - default = "eu-north-1" + default = "us-east-1" } \ No newline at end of file From 3266908aeb2f031027e3d4afbe542740d69992f7 Mon Sep 17 00:00:00 2001 From: babah1139 <103774473+babah1139@users.noreply.github.com> Date: Thu, 17 Oct 2024 11:12:56 +0200 Subject: [PATCH 2/6] updating tf code (#1) --- aws_migration/.gitignore | 4 ++++ aws_migration/main.tf | 27 ++++++--------------------- aws_migration/provider.tf | 13 +++++++++++++ aws_migration/userdata.sh | 11 +++++++++++ aws_migration/variables.tf | 8 +++++--- 5 files changed, 39 insertions(+), 24 deletions(-) create mode 100644 aws_migration/.gitignore create mode 100644 aws_migration/provider.tf create mode 100644 aws_migration/userdata.sh diff --git a/aws_migration/.gitignore b/aws_migration/.gitignore new file mode 100644 index 0000000..f18d1b9 --- /dev/null +++ b/aws_migration/.gitignore @@ -0,0 +1,4 @@ +terraform.tfstate +terraform.tfstate.backup +.terraform +.terraform.* diff --git a/aws_migration/main.tf b/aws_migration/main.tf index 1af98e2..f6ac528 100644 --- a/aws_migration/main.tf +++ b/aws_migration/main.tf @@ -1,10 +1,7 @@ -provider "aws" { - region = "us-east-1" -} # VPC resource "aws_vpc" "wordpress_vpc" { - cidr_block = "10.0.0.0/16" + cidr_block = "10.0.0.0/16" enable_dns_support = true enable_dns_hostnames = true tags = { @@ -14,9 +11,9 @@ resource "aws_vpc" "wordpress_vpc" { # Subnet resource "aws_subnet" "wordpress_subnet" { - vpc_id = aws_vpc.wordpress_vpc.id - cidr_block = "10.0.1.0/24" - availability_zone = var.availability_zone + vpc_id = aws_vpc.wordpress_vpc.id + cidr_block = "10.0.1.0/24" + availability_zone = var.availability_zone map_public_ip_on_launch = true } @@ -71,22 +68,10 @@ resource "aws_instance" "wordpress_instance" { instance_type = var.instance_type subnet_id = aws_subnet.wordpress_subnet.id - security_groups = [aws_security_group.wordpress_sg.name] + security_groups = [aws_security_group.wordpress_sg.id] associate_public_ip_address = true - user_data = <<-EOF - #!/bin/bash - yum update -y - yum install -y httpd mysql php php-mysql - systemctl start httpd - systemctl enable httpd - cd /var/www/html - wget https://wordpress.org/latest.tar.gz - tar -xzf latest.tar.gz - mv wordpress/* . - chown -R apache:apache /var/www/html - systemctl restart httpd - EOF + user_data = file("userdata.sh") tags = { Name = "bloomscoach-instance" diff --git a/aws_migration/provider.tf b/aws_migration/provider.tf new file mode 100644 index 0000000..5087924 --- /dev/null +++ b/aws_migration/provider.tf @@ -0,0 +1,13 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "5.72.0" + } + } +} + +provider "aws" { + region = "us-east-1" + profile = "wp-migration" +} \ No newline at end of file diff --git a/aws_migration/userdata.sh b/aws_migration/userdata.sh new file mode 100644 index 0000000..1b8f553 --- /dev/null +++ b/aws_migration/userdata.sh @@ -0,0 +1,11 @@ +#!/bin/bash +yum update -y +yum install -y httpd mysql php php-mysql +systemctl start httpd +systemctl enable httpd +cd /var/www/html +wget https://wordpress.org/latest.tar.gz +tar -xzf latest.tar.gz +mv wordpress/* . +chown -R apache:apache /var/www/html +systemctl restart httpd \ No newline at end of file diff --git a/aws_migration/variables.tf b/aws_migration/variables.tf index 79689a6..99e7473 100644 --- a/aws_migration/variables.tf +++ b/aws_migration/variables.tf @@ -1,17 +1,19 @@ variable "ami" { description = "The AMI ID for the EC2 instance" type = string - default = "ami-00f251754ac5da7f0" + default = "ami-00f251754ac5da7f0" + } variable "instance_type" { description = "The EC2 instance type" type = string - default = "t2.micro" + default = "t2.micro" } variable "availability_zone" { description = "The availability zone for the subnet" type = string - default = "us-east-1" + default = "us-east-1a" + } \ No newline at end of file From bb1377d7f7e2216cee5194c3856315deaa01dc74 Mon Sep 17 00:00:00 2001 From: damienmwene <146186797+damienmwene@users.noreply.github.com> Date: Sat, 19 Oct 2024 12:38:03 +0100 Subject: [PATCH 3/6] Create deploy.yml --- .github/workflows/deploy.yml | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..d0997cb --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,35 @@ +name: Deploy to EC2 using AWS CLI + +on: + push: + branches: + - Dev + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v2 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: us-east-1 + + - name: Install AWS CLI + run: | + sudo apt-get update + sudo apt-get install -y awscli + + - name: Deploy to EC2 via SSM + run: | + aws ssm send-command \ + --instance-ids "i-004aee8ed40389322" \ + --document-name "AWS-RunShellScript" \ + --comment "GitHub Actions EC2 deployment" \ + --parameters commands='cd /var/www/html && git pull origin main && sudo systemctl restart httpd' \ + --output text From aabd20f486810fcbe00950b798212ca13c26c59e Mon Sep 17 00:00:00 2001 From: damienmwene <146186797+damienmwene@users.noreply.github.com> Date: Sat, 19 Oct 2024 12:53:54 +0100 Subject: [PATCH 4/6] Updating deploy.yml --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index d0997cb..89f2144 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -31,5 +31,5 @@ jobs: --instance-ids "i-004aee8ed40389322" \ --document-name "AWS-RunShellScript" \ --comment "GitHub Actions EC2 deployment" \ - --parameters commands='cd /var/www/html && git pull origin main && sudo systemctl restart httpd' \ + --parameters commands='cd /var/www/html && git pull origin Dev && sudo systemctl restart httpd' \ --output text From d2cf5cf8edba593472558eeb478c96e4e4d7e23e Mon Sep 17 00:00:00 2001 From: damienmwene <146186797+damienmwene@users.noreply.github.com> Date: Sat, 19 Oct 2024 13:02:38 +0100 Subject: [PATCH 5/6] Updated deploy.yml --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 89f2144..0a85cd9 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -14,7 +14,7 @@ jobs: uses: actions/checkout@v3 - name: Configure AWS credentials - uses: aws-actions/configure-aws-credentials@v2 + uses: aws-actions/configure-aws-credentials@v3 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} From 76f94b655145f84e4f97af2aecbe6a580dc43346 Mon Sep 17 00:00:00 2001 From: babah1139 <103774473+babah1139@users.noreply.github.com> Date: Mon, 21 Oct 2024 08:02:03 +0200 Subject: [PATCH 6/6] Tf update2 (#3) * updating tf code * update ami with wp gold image --- aws_migration/main.tf | 3 ++- aws_migration/userdata.sh | 2 +- aws_migration/variables.tf | 3 +-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/aws_migration/main.tf b/aws_migration/main.tf index 6210144..8c90278 100644 --- a/aws_migration/main.tf +++ b/aws_migration/main.tf @@ -72,7 +72,8 @@ resource "aws_instance" "wordpress_instance" { security_groups = [aws_security_group.wordpress_sg.id] associate_public_ip_address = true - user_data = file("userdata.sh") + # user_data = file("userdata.sh") + tags = { Name = "bloomscoach-instance" diff --git a/aws_migration/userdata.sh b/aws_migration/userdata.sh index 1b8f553..034e582 100644 --- a/aws_migration/userdata.sh +++ b/aws_migration/userdata.sh @@ -1,6 +1,6 @@ #!/bin/bash yum update -y -yum install -y httpd mysql php php-mysql +yum install -y httpd mysql php7.4 php-mysql systemctl start httpd systemctl enable httpd cd /var/www/html diff --git a/aws_migration/variables.tf b/aws_migration/variables.tf index ab8519f..19c559f 100644 --- a/aws_migration/variables.tf +++ b/aws_migration/variables.tf @@ -1,8 +1,8 @@ variable "ami" { description = "The AMI ID for the EC2 instance" type = string - default = "ami-00f251754ac5da7f0" + default = "ami-0025c36cba2a6c3b9" # "ami-00f251754ac5da7f0" } @@ -16,5 +16,4 @@ variable "availability_zone" { description = "The availability zone for the subnet" type = string default = "us-east-1a" - } \ No newline at end of file