Deploy golang simple web app as service on terraform and ansible
Deploy golang simple web app as service on terraform and ansible
Простейший веб-сервер на go
Терраформом создастся машина в digital ocean
Далее создастся dns запись в aws
На машине приложение на go поднимется как сервис с помощью ansible
terraform.tfvars
do_token="***"
local_ssh_key_path="~/.ssh/id_rsa.pub"
email="email_at_domain_local"
aws_access_key = "***"
aws_secret_key = "***"
variables.tf
variable "do_token" {}
variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "aws_region" {
default = "eu-west-1"
}
variable "local_ssh_public_key" {
default = "~/.ssh/id_rsa.pub"
}
variable "local_ssh_private_key" {
default = "~/.ssh/id_rsa"
}
variable "devs" {
description = "Settings"
type = map(any)
default = {
your_login = "your_user_name",
general_domain = "domain.local"
}
}
provider.tf
terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.0"
}
}
}
provider "digitalocean" {
token = var.do_token
}
provider "aws" {
access_key = var.aws_access_key
secret_key = var.aws_secret_key
region = var.aws_region
}
resources.tf
resource "digitalocean_volume" "temp_vol1" {
region = "nyc1"
name = "temp_vol1"
size = 5
initial_filesystem_type = "ext4"
}
resource "digitalocean_ssh_key" "local_ssh_pkey" {
name = "local_ssh_public_key"
public_key = file(var.local_ssh_public_key)
}
resource "digitalocean_droplet" "foobar" {
name = "foobar"
size = "s-1vcpu-1gb"
image = "ubuntu-22-04-x64"
region = "nyc1"
ssh_keys = [digitalocean_ssh_key.local_ssh_pkey.id]
volume_ids = [digitalocean_volume.temp_vol1.id]
}
locals {
vps_ip = digitalocean_droplet.foobar.ipv4_address
}
data "aws_route53_zone" "zone" {
name = var.devs.general_domain
}
resource "aws_route53_record" "record" {
zone_id = data.aws_route53_zone.zone.id
name = var.devs.your_login
allow_overwrite = true
type = "A"
ttl = "300"
records = [local.vps_ip]
}
resource "local_file" "hosts" {
filename = "${path.module}/hosts.yml"
content = templatefile("${path.module}/hosts.tmpl",
{
droplet = digitalocean_droplet.foobar
}
)
provisioner "local-exec" {
command = "ANSIBLE_CONFIG=${path.module}/ansible.cfg ansible-playbook playbook.yaml -i hosts.yml"
}
}
hosts.tmpl
all:
hosts:
${droplet.name}:
ansible_ssh_host: ${droplet.ipv4_address}
ansible_ssh_private_key_file: /root/.ssh/id_rsa
ansible_ssh_public_key_file: /root/.ssh/id_rsa.pub
ansible.cfg
[defaults]
host_key_checking=false
inventory=./hosts.yml
playbook.yaml
- hosts:
- all
become: true
roles:
- role: go
roles/go/files/go_app.service
[Unit]
Description=openroad flow backend server daemon
[Service]
Type=simple
ExecStart=/tmp/main
Restart=on-failure
RestartSec=10s
WorkingDirectory=/tmp
[Install]
WantedBy=multi-user.target
roles/go/files/main.go
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
// API routes
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello world!")
})
http.HandleFunc("/hi", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi")
})
port := ":80"
fmt.Println("Server is running on port" + port)
// Start server on port specified above
log.Fatal(http.ListenAndServe(port, nil))
}
roles/go/handlers/main.yml
---
- name: Start go_app
systemd:
name: go_app
state: started
enabled: yes
roles/go/tasks/main.yml
---
- name: Install golang
apt:
name: golang
state: present
update_cache: true
- name: Copy golang script
copy:
src: "{{ role_path }}/files/main.go"
dest: /tmp/main.go
- name: Build golang script
shell: cd /tmp && go build /tmp/main.go
#- name: Start golang script
# shell: go run /tmp/main.go
- name: Copy golang service
copy:
src: "{{ role_path }}/files/go_app.service"
dest: "/etc/systemd/system/go_app.service"
- name: Apply daemon-reload
command: systemctl daemon-reload
notify:
- Start go_app
# terraform init
# terraform apply
Комментарии пользователей
Анонимам нельзя оставоять комментарии, зарегистрируйтесь!