Skip to content
Open
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/.terraform
.vscode
ssh_keys
*.tfstate*
95 changes: 95 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM hashicorp/terraform:1.1.3

RUN apk update && apk upgrade
RUN apk add bash groff openssh git vim jq make curl

RUN apk -v --update add \
python3 \
py3-pip \
groff \
less \
mailcap \
&& \
pip3 install --upgrade awscli s3cmd python-magic && \
apk -v --purge del && \
rm /var/cache/apk/*

RUN mkdir -p /infra
WORKDIR /infra

RUN echo 'alias t=terraform' > ~/.bashrc && \
terraform -install-autocomplete

ENTRYPOINT "bash"
19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: "3.7"

x-aws-vault-environment: &aws-vault-environment
- AWS_VAULT
- AWS_DEFAULT_REGION
- AWS_REGION
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AWS_SESSION_TOKEN
- AWS_SECURITY_TOKEN
- AWS_SESSION_EXPIRATION

services:
infra:
build: .
volumes:
- .:/infra
- ~/.ssh:/root/.ssh
environment: *aws-vault-environment
22 changes: 22 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module "application" {
source = "./modules/application"
host = "ssh://${var.instance_user}@${module.instance.public_ip}:22"
image = var.image
}

module "instance" {
source = "./modules/instance"
instance_ami = var.instance_ami
user = var.instance_user
ssh_key = module.ssh.public_key_openssh
ec2_sg_id = module.networking.ec2_sg_id
}

module "networking" {
source = "./modules/networking"
}

module "ssh" {
source = "./modules/ssh"
env = var.env
}
16 changes: 16 additions & 0 deletions modules/application/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
resource "docker_image" "app" {
name = var.image
}

resource "docker_container" "app" {
image = docker_image.app.latest
name = "app"
restart = "unless-stopped"
env = [
"PORT=4000",
]
ports {
internal = "4000"
external = "80"
}
}
9 changes: 9 additions & 0 deletions modules/application/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "host" {
description = "instance host"
type = string
}

variable "image" {
description = "dockerhub image"
type = string
}
16 changes: 16 additions & 0 deletions modules/application/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
terraform {
required_version = ">= 0.15.3"

required_providers {
docker = {
source = "kreuzwerker/docker"
version = "2.11.0"
}
}
}

provider "docker" {
host = var.host


}
9 changes: 9 additions & 0 deletions modules/instance/config.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variant: fcos
version: 1.2.0
passwd:
users:
- name: ${user}
ssh_authorized_keys:
- ${key}
groups:
- docker
41 changes: 41 additions & 0 deletions modules/instance/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# IP
resource "aws_eip" "eip" {
vpc = true
}

resource "aws_eip_association" "eip_assoc" {
instance_id = aws_instance.app_server.id
allocation_id = aws_eip.eip.id
}

# User Data
data "ct_config" "config" {
content = templatefile("${path.module}/config.tpl", {
user = var.user
key = var.ssh_key
})
strict = true
}

# Instance
resource "aws_instance" "app_server" {
ami = var.instance_ami
instance_type = "t2.micro"
user_data = data.ct_config.config.rendered

tags = {
Name = "Study AppServer"
}

vpc_security_group_ids = [ var.ec2_sg_id
]
}









3 changes: 3 additions & 0 deletions modules/instance/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "public_ip" {
value = aws_eip.eip.public_ip
}
20 changes: 20 additions & 0 deletions modules/instance/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
variable "user" {
description = "instance user"
type = string
}


variable "ssh_key" {
description = "public key"
type = string
}

variable "instance_ami" {
description = "AWS AMI"
type = string
}

variable "ec2_sg_id" {
description = "EC2 security group id"
type = string
}
10 changes: 10 additions & 0 deletions modules/instance/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = ">= 0.15.3"

required_providers {
ct = {
source = "poseidon/ct"
version = "0.8.0"
}
}
}
15 changes: 15 additions & 0 deletions modules/networking/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
data "aws_vpc" "default" {
default = true
}

module "ec2_sg" {
source = "terraform-aws-modules/security-group/aws"

name = "ec2_sg"
description = "Security group for ec2_sg"
vpc_id = data.aws_vpc.default.id

ingress_cidr_blocks = ["0.0.0.0/0"]
ingress_rules = ["http-80-tcp", "https-443-tcp", "all-icmp", "ssh-tcp"]
egress_rules = ["all-all"]
}
3 changes: 3 additions & 0 deletions modules/networking/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "ec2_sg_id" {
value = module.ec2_sg.security_group_id
}
9 changes: 9 additions & 0 deletions modules/ssh/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "tls_private_key" "test" {
algorithm = "RSA"
}

resource "local_file" "private_key" {
content = tls_private_key.test.private_key_pem
filename = "ssh_keys/${var.env}.pem"
file_permission = "0600"
}
11 changes: 11 additions & 0 deletions modules/ssh/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
output "private_key_pem" {
value = tls_private_key.test.private_key_pem
}

output "public_key_pem" {
value = tls_private_key.test.public_key_pem
}

output "public_key_openssh" {
value = tls_private_key.test.public_key_openssh
}
4 changes: 4 additions & 0 deletions modules/ssh/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable "env" {
description = "ssh env"
type = string
}
3 changes: 3 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "public_ip" {
value = module.instance.public_ip
}
5 changes: 5 additions & 0 deletions terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
region = "us-east-1"
instance_ami = "ami-09e2e5104f310ffb5"
instance_user = "core"
image = "kaitoendo/ping-pong:latest"
env = "staging"
24 changes: 24 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
variable "region" {
description = "aws region"
type = string
}

variable "instance_ami" {
description = "AWS AMI"
type = string
}

variable "instance_user" {
description = "instance user"
type = string
}

variable "image" {
description = "container image you will run on the instance"
type = string
}

variable "env" {
description = "environment"
type = string
}
Loading