Last active
May 29, 2020 01:53
-
-
Save ichengchao/27e8d73e00d9f7d1f94e67a513884337 to your computer and use it in GitHub Desktop.
aliyun terraform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"database/sql" | |
"fmt" | |
"net/http" | |
"os" | |
"strconv" | |
"time" | |
_ "github.com/go-sql-driver/mysql" | |
) | |
func main() { | |
http.HandleFunc("/hello", indexHandler) | |
http.HandleFunc("/", rootHandler) | |
http.ListenAndServe(":8080", nil) | |
} | |
func rootHandler(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "ok") | |
} | |
func indexHandler(w http.ResponseWriter, r *http.Request) { | |
result := dolog() | |
fmt.Fprintf(w, result) | |
} | |
func dolog() string { | |
db, err := sql.Open("mysql", "root:password@tcp(mysql_ip:3306)/mytest?charset=utf8") | |
if err != nil { | |
panic(err) | |
} | |
insert(db) | |
return query(db) | |
} | |
// query data | |
func query(db *sql.DB) string { | |
rows, err := db.Query("select * from accesslog order by id desc") | |
if err != nil { | |
panic(err) | |
} | |
var result string | |
for rows.Next() { | |
var id int | |
var log string | |
err = rows.Scan(&id, &log) | |
//fmt.Println("id: " + strconv.Itoa(id) + ", log: " + log) | |
result += "id: " + strconv.Itoa(id) + ", log: " + log + "\n" | |
} | |
return result | |
} | |
func insert(db *sql.DB) { | |
stmt, err := db.Prepare("insert accesslog set log=?") | |
timeStr := time.Now().Format("2006-01-02 15:04:05") | |
hostname, err := os.Hostname() | |
stmt.Exec(timeStr + " @ " + hostname) | |
if err != nil { | |
panic(err) | |
return | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
provider "alicloud" { | |
profile = "default" | |
} | |
data "alicloud_regions" "current_region_ds" { | |
current = true | |
} | |
data "alicloud_images" "ubuntu" { | |
name_regex = "^ubuntu" | |
most_recent = false | |
owners = "system" | |
} | |
# 创建VPC | |
resource "alicloud_vpc" "charles_vpc" { | |
name = "charles_vpc" | |
cidr_block = "172.16.0.0/12" | |
} | |
# 创建交换机 | |
resource "alicloud_vswitch" "charles_vsw" { | |
name = "charles_vsw" | |
vpc_id = alicloud_vpc.charles_vpc.id | |
cidr_block = "172.16.0.0/24" | |
availability_zone = "cn-hangzhou-h" | |
} | |
# 创建安全组 | |
resource "alicloud_security_group" "charles_security_group" { | |
name = "charles_security_group" | |
vpc_id = alicloud_vpc.charles_vpc.id | |
} | |
# 创建ECS-> web1 | |
resource "alicloud_instance" "charles-web1" { | |
image_id = data.alicloud_images.ubuntu.ids.0 | |
internet_charge_type = "PayByBandwidth" | |
instance_type = "ecs.s6-c1m1.small" | |
system_disk_category = "cloud_efficiency" | |
security_groups = [alicloud_security_group.charles_security_group.id] | |
instance_name = "charles-web1" | |
host_name = "charlesweb1" | |
password = "pass_1234" | |
private_ip = "172.16.0.1" | |
vswitch_id = alicloud_vswitch.charles_vsw.id | |
} | |
# 创建ECS-> web2 | |
resource "alicloud_instance" "charles-web2" { | |
image_id = data.alicloud_images.ubuntu.ids.0 | |
internet_charge_type = "PayByBandwidth" | |
instance_type = "ecs.s6-c1m1.small" | |
system_disk_category = "cloud_efficiency" | |
security_groups = [alicloud_security_group.charles_security_group.id] | |
instance_name = "charles-web2" | |
host_name = "charlesweb2" | |
password = "pass_1234" | |
private_ip = "172.16.0.2" | |
vswitch_id = alicloud_vswitch.charles_vsw.id | |
} | |
# 创建SLB | |
resource "alicloud_slb" "charles_slb" { | |
name = "charles_slb" | |
vswitch_id = alicloud_vswitch.charles_vsw.id | |
internet_charge_type = "PayByTraffic" | |
} | |
# 创建EIP | |
resource "alicloud_eip" "charles_eip" {} | |
# 将EIP绑定到SLB | |
resource "alicloud_eip_association" "charles_eip_slb_asso" { | |
allocation_id = alicloud_eip.charles_eip.id | |
instance_id = alicloud_slb.charles_slb.id | |
} | |
# SLB的监听配置 | |
resource "alicloud_slb_listener" "charles_listener_http" { | |
load_balancer_id = alicloud_slb.charles_slb.id | |
backend_port = "8080" | |
frontend_port = "80" | |
protocol = "http" | |
bandwidth = "5" | |
} | |
# 配置负载均衡的后端服务器 | |
resource "alicloud_slb_backend_server" "charles_slb_backend_server" { | |
load_balancer_id = alicloud_slb.charles_slb.id | |
backend_servers { | |
server_id = alicloud_instance.charles-web1.id | |
weight = 100 | |
} | |
backend_servers { | |
server_id = alicloud_instance.charles-web2.id | |
weight = 100 | |
} | |
} | |
# RDS | |
resource "alicloud_db_instance" "charles_db_instance" { | |
engine = "MySQL" | |
engine_version = "5.6" | |
instance_type = "rds.mysql.s1.small" | |
instance_storage = "10" | |
vswitch_id = alicloud_vswitch.charles_vsw.id | |
instance_name = "charles_db_rds" | |
} | |
# RDS 账号 | |
resource "alicloud_db_account" "charles_account" { | |
instance_id = alicloud_db_instance.charles_db_instance.id | |
type = "Super" | |
name = "charles" | |
password = "pass_1234" | |
} | |
# RDS database | |
resource "alicloud_db_database" "charles_rds_db_test" { | |
instance_id = alicloud_db_instance.charles_db_instance.id | |
name = "mytest" | |
character_set = "utf8mb4" | |
description = "just for test" | |
} | |
# debug info | |
# output "outtest" { | |
# value = alicloud_instance.charles-web1 | |
# } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment