使用Terraform和Wercker,在提交git push后创建EC2实例
总结
你好。我是HashiCorp Advent Calendar 2015第12天的@ikemonn。
在这篇文章中,我将介绍如何使用Wercker和Terraform来创建EC2。
作为流程,大致如下。
-
- 将代码推送到用于开发的develop分支
-
- 通过钩子将推送操作连接到Wercker,进行terraform plan
-
- 一旦terraform plan通过,自动创建pull请求
- 一旦pull请求被合并,通过Wercker点击部署按钮,执行terraform apply并创建EC2实例。
准备好了
我会首先完成以下事项。
-
- 在Wercker上注册账号
-
- 在Wercker上注册目标仓库
-
- 通过curl命令获取GitHub的访问令牌,以便能够创建pull请求
- 在S3上准备用于管理tfstate的bucket (在Amazon S3上管理和共享Terraform的状态管理文件terraform.tfstate – Qiita)
准备使用Terraform
这次要创建的是t2.micro实例。
准备一个类似这样的tf文件。
resource "aws_instance" "sample" {
ami = "ami-908a2f90"
instance_type = "t2.micro"
availability_zone = "ap-northeast-1a"
tags {
Name = "terraform_sample"
}
vpc_security_group_ids = ["${var.default_security_group}"]
key_name = "${var.lc_key_name}"
}
region = "ap-northeast-1"
default_security_group = “sg-xxxxxxx"
variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "region" {}
variable "default_security_group" {}
variable "lc_key_name" {}
provider "aws" {
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
region = "${var.region}"
}
TF_VAR_aws_access_key=“youraccesskey"
TF_VAR_aws_secret_key=“yoursecretkey"
TF_VAR_lc_key_name=“yourkey"
为了确保即使有多个开发人员也可以正常工作,我们决定将tfstate放在之前准备好的S3中。
terraform remote config -backend=S3 -backend-config=“bucket=your-terraform-state-bucket" -backend-config="key=terraform.tfstate" -backend-config="encrypt=true"
在这里,我认为在本地环境中确认Terraform是否可执行会减少后续操作的麻烦。
Wercker的准备
刚才我将Wercker与代码库进行了关联,如果将代码推送到任意分支,wercker.yaml文件中描述的处理程序将在根目录下执行。
.
├── terraform
│ ├── common_variables.tf
│ ├── sample_ec2.tf
│ └── terraform.tfvars
└── wercker.yml
这次我们将使用wget来安装terraform,并使用它来执行terraform plan和apply。
同时,我们还会一起安装其他必要的组件。
由于构建和部署过程冗长,我认为使用box和step将使代码更简洁。
box: debian
build:
steps:
- script:
name: install some tools
code: |
sudo apt-get update
sudo apt-get -f install
sudo apt-get install -y wget unzip curl
- script:
name: setup terraform
code: |
# create terraform directory and export PATH
mkdir -p $HOME/terraform
export PATH=$PATH:$HOME/terraform
# download terraform
VERSION=0.6.8
cd $HOME/terraform
wget https://releases.hashicorp.com/terraform/${VERSION}/terraform_${VERSION}_linux_amd64.zip
unzip terraform_${VERSION}_linux_amd64.zip
rm terraform_${VERSION}_linux_amd64.zip
- script:
name: terraform remote config
code: |
cd terraform
terraform remote config -backend=S3 -backend-config="bucket=your-terraform-state-bucket" -backend-config="key=terraform.tfstate" -backend-config="encrypt=true"
terraform plan
- script:
name: create pull request
code: |
curl -u "[username]:${GITHUB_TOKEN}" -d '{"title": "Terraform plan Success","body": "You can merge this pull request","head": "[username]:[fromBranch]","base": "[toBranch]"}' https://api.github.com/repos/[username]/[yourBranch]/pulls
deploy:
steps:
- script:
name: install some tools
code: |
sudo apt-get update
sudo apt-get -f install
sudo apt-get install -y wget unzip curl
- script:
name: setup terraform
code: |
# create terraform directory and export PATH
mkdir -p $HOME/terraform
export PATH=$PATH:$HOME/terraform
# download terraform
VERSION=0.6.8
cd $HOME/terraform
wget https://releases.hashicorp.com/terraform/${VERSION}/terraform_${VERSION}_linux_amd64.zip
unzip terraform_${VERSION}_linux_amd64.zip
rm terraform_${VERSION}_linux_amd64.zip
- script:
name: terraform remote config
code: |
cd terraform
terraform remote config -backend=S3 -backend-config="bucket=your-terraform-state-bucket" -backend-config="key=terraform.tfstate" -backend-config="encrypt=true"
terraform plan
- script:
name: terraform apply
code: |
terraform apply
在此之后,您可以在Wercker上设置环境变量,例如AWS的密钥和GitHub的令牌,以及其他必要的环境变量,请确保进行设置。
从 git push 到部署完成
然后,我们将尝试将其推送到适当的分支上。
Wercker会检测到推送并执行在build阶段中定义的处理。

当terraform计划成功后,将向主分支发送来自开发分支的拉取请求。

当合并了 pull req后,会发现合并已完成,然后在主分支上触发构建。

如果构建成功,请按下部署按钮。
在此步骤中,将执行terraform apply以进行部署阶段的处理。

当您查看AWS控制台时,可以确认实例已经成功创建。

提升方案
-
- Werckerの部分が冗長なのでBoxやStepを使ってすっきりさせたい
- terraform planの結果をpull reqのbodyに渡したい
请提供以下内容的中文同义词:参考
- wercker の step を自作するクイックメモ – ようへいの日々精進XP