Skip to content
Draft
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
48 changes: 29 additions & 19 deletions .github/workflows/packer.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
name: Build AMI with Packer
name: Deploy

on:
push:
branches:
- main
- feature/packer-pipeline
- feature/*

jobs:
packer_build:
name: Packer Build
name: AMI build with Packer
runs-on: ubuntu-latest
environment: dev
environment: tmp
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: us-east-1
AMI_NAME: default_webserver_image # amazon-linux-sandbox

outputs:
ami_id: ${{ steps.build.outputs.ami_id }}

steps:
- name: сheckout
run: |
git clone https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git
git clone https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git \
--branch ${{ github.ref_name }}
pwd
ls -lah
cd ${{ github.event.repository.name }}
Expand All @@ -37,19 +43,13 @@ jobs:
#TODO: добавить возможность пропуска, хз вручную как то, апровы или что еще, придумать короче
# wokrlfow dipstach с входными параметрами

- name: build
id: build
# if: steps.changes.outcome != 'success'
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: us-east-1
AMI_NAME: default_webserver
- name: deregister previous AMI
run: |
image_info=$(aws ec2 describe-images --owners self \
image_info=$(aws ec2 describe-images --owners self \
--filters Name=name,Values=$AMI_NAME \
--query 'Images[0].{ImageId: ImageId, SnapshotId: BlockDeviceMappings[0].Ebs.SnapshotId}')

# TODO: else логирование
if [ "$image_info" != "null" ]; then
image_id=$(echo $image_info | jq -r '.ImageId')
snapshot_id=$(echo $image_info| jq -r '.SnapshotId')
Expand All @@ -64,6 +64,9 @@ jobs:
fi
fi

- name: build
id: build
run: |
cd ${{ github.event.repository.name }}
cd packer
packer init .
Expand All @@ -72,16 +75,24 @@ jobs:
ami_id_val=$(jq -r '(.last_run_uuid as $uid | .builds[] | select(.packer_run_uuid == $uid) | .artifact_id | split(":")[1])' manifest.json)
echo "ami_id=${ami_id_val}" >> "$GITHUB_OUTPUT"

# - name: artifact
# uses: actions/upload-artifact@v4
# with:
# name: manifest.json
# path:


terraform_deploy:
name: Terraform Deploy
runs-on: ubuntu-latest
environment: dev
environment: tmp
needs: packer_build

steps:
- name: сheckout
run: |
git clone https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git
git clone https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git \
--branch ${{ github.ref_name }}
pwd
ls -lah
cd ${{ github.event.repository.name }}
Expand All @@ -97,6 +108,5 @@ jobs:
aws sts get-caller-identity --region=us-east-1

terraform init
terraform plan -var="ami_id=ami-0d02f4dc282064d6a"
# terraform apply -var="ami_id=ami-0d02f4dc282064d6a" -auto-approve

terraform plan -var="ami_id=${{ needs.packer_build.outputs.ami_id }}"
terraform apply -var="ami_id=${{ needs.packer_build.outputs.ami_id }}" -auto-approve
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
.DS_Store


.terraform/
.terraform/

terraform.tfstate

services/
23 changes: 0 additions & 23 deletions manifest.json

This file was deleted.

23 changes: 8 additions & 15 deletions packer/apache-server.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packer {
}
}

source "amazon-ebs" "default_webserver" {
source "amazon-ebs" "amazon-linux-sandbox" {
region = "us-east-1"
ami_name = var.ami_name
instance_type = "t2.micro"
Expand All @@ -29,31 +29,24 @@ source "amazon-ebs" "default_webserver" {
}

build {
name = "default_webserver"
name = "amazon-linux-sandbox"
sources = [
"source.amazon-ebs.default_webserver"
"source.amazon-ebs.amazon-linux-sandbox"
]

provisioner "shell" {
environment_vars = [
"GREETING=kek",
]
inline = [
# "sudo dnf group install -y 'Development Tools'",
# "sudo dnf install -y nc",
# "sudo dnf install -y telnet",
# "sudo dnf install -y jq",
# "sudo dnf install -y httpd",
# "sudo systemctl start httpd",
# "sudo systemctl enable httpd",
# "sudo systemctl status httpd",
# "echo '<Region> - <Node #&gt' | sudo tee /var/www/html/index.html > /dev/null",§ qA
"echo LOLKEK"

scripts = [
"./scripts/install.sh",
"./scripts/configure.sh"
]
expect_disconnect = true
}

post-processor "manifest" {
output = "manifest.json"
output = "manifest.json"
}
}
2 changes: 1 addition & 1 deletion packer/apache-server_variables.pkr.hcl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "ami_name" {
type = string
default = "default"
default = "amazon-linux-sandbox-v1"
}
14 changes: 0 additions & 14 deletions packer/manifest-2.json

This file was deleted.

14 changes: 14 additions & 0 deletions packer/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"builds": [
{
"name": "amazon-linux-sandbox",
"builder_type": "amazon-ebs",
"build_time": 1728025626,
"files": null,
"artifact_id": "us-east-1:ami-03497412185c7e9f5",
"packer_run_uuid": "3ba4f35c-847d-0f25-f22f-53e994445300",
"custom_data": null
}
],
"last_run_uuid": "3ba4f35c-847d-0f25-f22f-53e994445300"
}
24 changes: 0 additions & 24 deletions packer/sample_for_jq.json

This file was deleted.

3 changes: 3 additions & 0 deletions packer/scripts/configure.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo '<Region> - <Node #&gt' | sudo tee /var/www/html/index.html > /dev/null
13 changes: 13 additions & 0 deletions packer/scripts/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

sudo dnf update
sudo dnf group install -y 'Development Tools'
sudo dnf install -y nc
sudo dnf install -y telnet
sudo dnf install -y jq
# TODO: yaml query yq
# TODO: tree
sudo dnf install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
sudo systemctl status httpd
28 changes: 25 additions & 3 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ data "aws_vpc" "default_west" {
}



module "sg_access_to_web_servers_east" {
source = "./sg_access_to_web_servers"

Expand Down Expand Up @@ -70,7 +71,6 @@ resource "aws_security_group" "access_to_jumpbox" {
}



resource "aws_security_group" "ssh_access_to_web_servers" {
provider = aws.us_east
vpc_id = data.aws_vpc.default_east.id
Expand All @@ -90,7 +90,7 @@ resource "aws_instance" "jump_box" {
provider = aws.us_east
ami = var.ami_id
instance_type = "t2.micro"
associate_public_ip_address = true
associate_public_ip_address = false

security_groups = [
aws_security_group.access_to_jumpbox.name,
Expand Down Expand Up @@ -122,18 +122,38 @@ resource "aws_instance" "web_server_1" {
http_tokens = "required"
}

user_data = file("${path.module}/scripts/init-index-html.sh")

tags = {
Name = "web_server_1"
Target = "test-ec2-part1"
}
}


data "aws_ami" "ami_source" {
provider = aws.us_east

filter {
name = "image-id"
values = [var.ami_id]
}
}

resource "aws_ami_copy" "ami_west" {
name = data.aws_ami.ami_source.name
source_ami_id = data.aws_ami.ami_source.id
source_ami_region = "us-east-1"
description = "AMI copied from us-east-1 to us-west-1"

provider = aws.us_west
}



resource "aws_instance" "web_server_2" {
provider = aws.us_west
ami = var.ami_id
ami = aws_ami_copy.ami_west.id
instance_type = "t2.micro"
associate_public_ip_address = true
key_name = module.key_pairs.name_us_west
Expand All @@ -144,6 +164,8 @@ resource "aws_instance" "web_server_2" {
http_tokens = "required"
}

user_data = file("${path.module}/scripts/init-index-html.sh")

tags = {
Name = "web_server_2"
}
Expand Down
8 changes: 8 additions & 0 deletions terraform/scripts/init-index-html.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

token=`curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 300"`

region=`curl -s -H "X-aws-ec2-metadata-token: $token" http://169.254.169.254/latest/meta-data/placement/region`
node=`curl -s -H "X-aws-ec2-metadata-token: $token" http://169.254.169.254/latest/meta-data/placement/availability-zone`

echo "$region - $node" | sudo tee /var/www/html/index.html
9 changes: 0 additions & 9 deletions terraform/terraform.tfstate

This file was deleted.

2 changes: 1 addition & 1 deletion terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "ami_id" {
description = "AMI id"
description = "AMI ID"
type = string
}
18 changes: 0 additions & 18 deletions test.sh

This file was deleted.