From 9fb599201b30c62fec821f91a79a312542188c6f Mon Sep 17 00:00:00 2001 From: babah1139 <103774473+babah1139@users.noreply.github.com> Date: Mon, 21 Oct 2024 07:42:15 +0200 Subject: [PATCH] update main (#2) * Updated tf files * updating tf code (#1) * Create deploy.yml * Updating deploy.yml * Updated deploy.yml --------- Co-authored-by: damienmwene Co-authored-by: damienmwene <146186797+damienmwene@users.noreply.github.com> --- .github/workflows/deploy.yml | 35 +++++++++++++++++++++++++++++++++++ aws_migration/.gitignore | 4 ++++ aws_migration/main.tf | 28 +++++++--------------------- aws_migration/provider.tf | 13 +++++++++++++ aws_migration/userdata.sh | 11 +++++++++++ aws_migration/variables.tf | 9 ++++++--- 6 files changed, 76 insertions(+), 24 deletions(-) create mode 100644 .github/workflows/deploy.yml create mode 100644 aws_migration/.gitignore create mode 100644 aws_migration/provider.tf create mode 100644 aws_migration/userdata.sh diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..0a85cd9 --- /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@v3 + 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 Dev && sudo systemctl restart httpd' \ + --output text 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..6210144 100644 --- a/aws_migration/main.tf +++ b/aws_migration/main.tf @@ -1,10 +1,8 @@ -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 +12,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 +69,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..ab8519f 100644 --- a/aws_migration/variables.tf +++ b/aws_migration/variables.tf @@ -1,17 +1,20 @@ 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