diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bbca27b --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +env.txt +.terraform/* +aws-init/terraform.tfstate +key.pem \ No newline at end of file diff --git a/README.md b/README.md index c5a93f1..358e638 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # terraform-aws-gitea -terraform template for setup gitea in aws +This terraform template creates AWS infrastructure for the Gitea source control service. + +1) The template uses remote-state-backend, so it need to initialize the s3 bucket firstly - just run terraform-init.sh +2) Then apply terraform as usual - terraform apply +3) Finally navigate to you created gitea service (see terraform output for for dns name). Or ssh to server - ./connect.sh diff --git a/aws-init/aws-init.tf b/aws-init/aws-init.tf new file mode 100644 index 0000000..ae365ff --- /dev/null +++ b/aws-init/aws-init.tf @@ -0,0 +1,49 @@ +variable "name" { + description = "the name of infrastructure project" +} +variable "destroy" { + default = false +} +variable "tags" { + default = { + Purpose = "terraform state storage" + } +} + +# access_key, secret_key, region are provided by env varaibles +provider "aws" {} + +resource "aws_s3_bucket" "bucket" { + tags = "${var.tags}" + bucket = "${var.name}" + acl = "private" + versioning { + enabled = true + } + lifecycle { + prevent_destroy = true + } + force_destroy = "${var.destroy}" +} + +resource "aws_dynamodb_table" "table" { + tags = "${var.tags}" + name = "${var.name}" + billing_mode = "PAY_PER_REQUEST" + hash_key = "LockID" + attribute { + name = "LockID" + type = "S" + } + lifecycle { + prevent_destroy = true + } +} + +############## outputs ################# +output "bucket" { + value = "${aws_s3_bucket.bucket.bucket}" +} +output "table" { + value = "${aws_dynamodb_table.table.name}" +} \ No newline at end of file diff --git a/connect.sh b/connect.sh new file mode 100755 index 0000000..8b5efe9 --- /dev/null +++ b/connect.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +set -euxo pipefail + +#ip=$(terraform output -json | jq -r '.public_ip.value') +#terraform output -json | jq -r '.private_key.value' > key.pem +ip=$(terraform state show aws_instance.host | grep "public_ip " | sed 's/public_ip *= //') +terraform state show tls_private_key.key | perl -ne 'BEGIN{undef $/;} /(-----BEGIN RSA PRIVATE.*?PRIVATE KEY-----)/s and print "$1"' > key.pem + +chmod 400 key.pem +ssh -i key.pem -oStrictHostKeyChecking=no ec2-user@$ip +rm -f key.pem + +# generate key manually +# ssh-keygen -t rsa -b 4096 -C "skarbdev@gmail.com" -f sshkey -q -N "" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..dae5c2f --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,39 @@ +version: "2" + +networks: + gitea: + external: false + +services: + server: + image: gitea/gitea:latest + environment: + - USER_UID=1000 + - USER_GID=1000 + - DB_TYPE=postgres + - DB_HOST=db:5432 + - DB_NAME=gitea + - DB_USER=gitea + - DB_PASSWD=gitea + restart: always + networks: + - gitea + volumes: + - ./gitea:/data + ports: + - "3000:3000" + - "222:22" + depends_on: + - db + + db: + image: postgres:9.6 + restart: always + environment: + - POSTGRES_USER=gitea + - POSTGRES_PASSWORD=gitea + - POSTGRES_DB=gitea + networks: + - gitea + volumes: + - ./postgres:/var/lib/postgresql/data \ No newline at end of file diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..7d2da49 --- /dev/null +++ b/main.tf @@ -0,0 +1,115 @@ + +module "vpc" { + source = "./vpc" + + cidr = "10.0.0.0/16" + cidr_subnet = "10.0.1.0/24" + tags = "${var.tags}" +} + +data "aws_ami" "al2" { + owners = ["amazon"] + most_recent = true + name_regex = "^amzn2-ami-hvm-2.0.\\d+-x86_64-gp2$", + filter { + name = "name" + values = ["amzn2-ami-hvm-2.*"] + } +} + +resource "tls_private_key" "key" { + algorithm = "RSA" + ecdsa_curve = "4096" +} +resource "aws_key_pair" "key" { + public_key = "${tls_private_key.key.public_key_openssh}" +} +resource "aws_instance" "host" { + ami = "${data.aws_ami.al2.id}" + instance_type = "t2.micro" + subnet_id = "${module.vpc.subnet}" + key_name = "${aws_key_pair.key.key_name}" + vpc_security_group_ids = ["${aws_security_group.web.id}"] + monitoring = false # monitoring per 5 min for free (intead off 1 min payed) + #iam_instance_profile = "${var.iam_instance_profile}" + user_data = <