From d5210cba4306a745a3119f4a5a39ff6fb88e9524 Mon Sep 17 00:00:00 2001 From: azhar <87310169+azharism@users.noreply.github.com> Date: Thu, 1 Dec 2022 00:38:15 +0530 Subject: [PATCH] Update main.yml --- terraform/.github/workflows/main.yml | 146 +++++++++++++++------------ 1 file changed, 82 insertions(+), 64 deletions(-) diff --git a/terraform/.github/workflows/main.yml b/terraform/.github/workflows/main.yml index 2c7cdfd..239d57a 100644 --- a/terraform/.github/workflows/main.yml +++ b/terraform/.github/workflows/main.yml @@ -1,64 +1,82 @@ -# name: "Terraform Deployment" -# on: -# # Manual trigger -# workflow_dispatch: -# push: -# paths: -# - '*/**' -# pull_request: -# branches: [ main ] -# defaults: -# run: -# shell: bash -# jobs: -# terraform: -# name: ${{matrix.runner}} - ${{ matrix.environment }} -# runs-on: [ '${{ matrix.runner }}'] -# strategy: -# max-parallel: 1 -# matrix: -# include: -# - environment: test -# runner: ubuntu-latest -# - environment: prod -# runner: ubuntu-18.04 -# env: -# AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} -# AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} -# AWS_DEFAULT_REGION: us-east-2 -# steps: -# - uses: actions/checkout@v2 -# - uses: hashicorp/setup-terraform@v1 -# with: -# terraform_wrapper: false -# - name: Terraform Init -# id: init -# run: | -# rm -rf .terraform -# terraform init -upgrade=true -no-color -input=false -# - name: Terraform Plan -# id: plan -# run: | -# terraform plan -input=false -no-color -# - name: Terraform Apply -# if: github.ref == 'refs/heads/main' -# id: apply -# run: terraform apply -auto-approve -input=false -# - name: Terraform destroy -# if: github.ref == 'refs/heads/destroy' -# id: destroy -# run: terraform destroy -auto-approve -input=false -# comment: -# runs-on: ubuntu-latest -# needs: terraform -# if: github.event_name == 'pull_request' -# steps: -# - uses: actions/github-script@v5 -# with: -# script: | -# github.rest.issues.createComment({ -# issue_number: context.issue.number, -# owner: context.repo.owner, -# repo: context.repo.repo, -# body: '👋 Thanks for reporting!' -# }) \ No newline at end of file +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "3.26.0" + } + random = { + source = "hashicorp/random" + version = "3.0.1" + } + } + required_version = ">= 1.1.0" + + cloud { + organization = "manto" + + workspaces { + name = "Github" + } + } +} + +provider "aws" { + region = "us-east-1" +} + +resource "random_pet" "sg" {} + +data "aws_ami" "ubuntu" { + most_recent = true + + filter { + name = "name" + values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + + owners = ["629974703195"] # Canonical +} + +resource "aws_instance" "web" { + ami = data.aws_ami.ubuntu.id + instance_type = "t2.micro" + vpc_security_group_ids = [aws_security_group.web-sg.id] + + user_data = <<-EOF + #!/bin/bash + sudo apt-get update -y + sudo apt install docker.io -y + sudo apt install docker-compose -y + + echo + git clone https://github.com/azharism/Snipe-IT-GitHub-Actions.git + cd /snipe-it/ + sudo docker-compose up + EOF +} + +resource "aws_security_group" "web-sg" { + name = "${random_pet.sg.id}-sg" + ingress { + from_port = 0 + to_port = 6553 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + // connectivity to ubuntu mirrors is required to run `apt-get update` and `apt-get install apache2` + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } +} + +output "web-address" { + value = "${aws_instance.web.public_ip}:8000" +}