Share
Embed
TerraformでAWSのインフラ構成構築を自動化する(入門) at 第130回 PHP勉強会@東京 by @Khigashiguchi
Kazuki Higashiguchi
September 26, 2018
Transcript
32 slides
-
ࣗݾհ • ౦ޱ ᏻ @Khigashiguchi • Server Side EngineerʢPHP /
Goʣ • BASE, Inc / BASE Product Division • Blog: http:// khigashigashi.hatenablog.com/ -
What is Terraform? • HashiCorp͕࡞͍ͬͯΔίʔυ͔ΒΠϯϑ ϥϦιʔεΛ࡞ɾཧ͢ΔͨΊͷπʔϧ • Infrastructure as Code
• AWS, GCP, Azure, Heroku ͳͲଟ͘ͷSaaS ʹ෯͘ରԠ -
Terraform Merit for PHPer Work • SREνʔϜͳͲͱͷΠϯϑϥʹؔ͢Δձ ͷ५Խ • TerraformͷϑΝΠϧΛϕʔεʹͨ͠ߏ
ใڞ༗ • ϕϯμʔʹറΒΕͳ͍πʔϧ • ಛఆϕϯμʔͷΈͰͳ͍ͷͰԣల͕͖ ͘ -
Getting started Terraform: Example case • ʮEIP(Elastic IP)Λඥ͚ͮͨEC2 InstanceΛ ࡞Δʯ
1. Configuration fileʢ֦ுࢠ .tfʣͷ࡞ 2. ॳظԽʢterraform initʣ 3. ߏஙʢterraform applyʣ 4. আʢterraform destroyʣ -
Configuration fileʢ֦ுࢠ .tfʣͷ࡞ provider "aws" { access_key = "${var.aws_access_key}" secret_key
= "${var.aws_secret_key}" region = "${var.aws_region}" } resource "aws_instance" "example" { ami = "ami-08847abae18baa040" // Amazon Linux 2 AMI (HVM), SSD Volume Type instance_type = "t2.micro" } resource "aws_eip" "ip" { instance = "${aws_instance.example.id}" } output "ip" { value = "${aws_eip.ip.public_ip}" } main.tf -
Configuration fileʢ֦ுࢠ .tfʣͷ࡞ provider "aws" { access_key = "${var.aws_access_key}" secret_key
= "${var.aws_secret_key}" region = "${var.aws_region}" } resource "aws_instance" "example" { ami = "ami-08847abae18baa040" // Amazon Linux 2 AMI (HVM), SSD Volume Type instance_type = "t2.micro" } resource "aws_eip" "ip" { instance = "${aws_instance.example.id}" } output "ip" { value = "${aws_eip.ip.public_ip}" } providerࢦఆɺࠓճ”aws”Λઃఆ ${var.xxx}ʹ͍ͭͯ࣍ʹvariables.tfϑΝΠϧʹͯ main.tf -
Configuration fileʢ֦ுࢠ .tfʣͷ࡞ provider "aws" { access_key = "${var.aws_access_key}" secret_key
= "${var.aws_secret_key}" region = "${var.aws_region}" } resource "aws_instance" "example" { ami = "ami-08847abae18baa040" // Amazon Linux 2 AMI (HVM), SSD Volume Type instance_type = "t2.micro" } resource "aws_eip" "ip" { instance = "${aws_instance.example.id}" } output "ip" { value = "${aws_eip.ip.public_ip}" } EC2 InstanceΛཱͯΔɻ ແྉͰ͑ΔAMIɾInstance TypeΛࢦఆ main.tf -
Configuration fileʢ֦ுࢠ .tfʣͷ࡞ provider "aws" { access_key = "${var.aws_access_key}" secret_key
= "${var.aws_secret_key}" region = "${var.aws_region}" } resource "aws_instance" "example" { ami = "ami-08847abae18baa040" // Amazon Linux 2 AMI (HVM), SSD Volume Type instance_type = "t2.micro" } resource "aws_eip" "ip" { instance = "${aws_instance.example.id}" } output "ip" { value = "${aws_eip.ip.public_ip}" } EIP (Elastic IP)Λ࡞͠ɺ ্هͰ࡞͢ΔEC2 Instanceʹؔ࿈͚Δɻ main.tf -
Configuration fileʢ֦ுࢠ .tfʣͷ࡞ variable "aws_access_key" {} variable "aws_secret_key" {} variable
"aws_region" { default = "ap-northeast-1" } variables.tf -
Configuration fileʢ֦ுࢠ .tfʣͷ࡞ variable "aws_access_key" {} variable "aws_secret_key" {} variable
"aws_region" { default = "ap-northeast-1" } variables.tf Input variablesͱͯ͠ఆٛ .tfvars ϑΝΠϧ͕༗Εͦ͜ʹهࡌ͞ΕͨΛࢀরɻ ͳ͚ΕίϚϯυΠϯλʔϑΣʔεʹͯೖྗɻ -
Configuration fileʢ֦ுࢠ .tfʣͷ࡞ aws_access_key = "YOUR-ACCESS-KEY" aws_secret_key = "YOUR-SECRET-KEY" aws_region
= "ap-northeast-1" terreform.tfvars variablesʹೖ͍ͨ͠Λઃఆ͢Δ -
ॳظԽʢterraform initʣ -> % terraform init Initializing provider plugins... -
Checking for available provider plugins on https:// releases.hashicorp.com... - Downloading plugin for provider "aws" (1.37.0)... The following providers do not have any version constraints in configuration, so the latest version was installed. To prevent automatic upgrades to new major versions that may contain breaking changes, it is recommended to add version = "..." constraints to the corresponding provider blocks in configuration, with the constraint strings suggested below. * provider.aws: version = "~> 1.37" Terraform has been successfully initialized! -
ߏஙʢterraform applyʣ -> % terraform apply An execution plan has
been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: (omit) Plan: 2 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: -
ߏஙʢterraform applyʣ -> % terraform apply An execution plan has
been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: (omit) Plan: 2 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes ConfigurationϑΝΠϧ͔Β࣮ߦPlan͕࡞͞ΕΔɻ ࣮ߦPlanʹ͕ͳ͚Ε࣮ࡍʹ࣮ߦ͢Δɻ -
ߏஙʢterraform applyʣ -> % terraform apply (omit) Apply complete! Resources:
2 added, 0 changed, 0 destroyed. Outputs: ip = xx.xxx.xxx.xxx ࣮ߦྃޙ݁Ռ͕දࣔ͞ΕΔ -
আʢterraform destroyʣ -> % terraform destroy aws_instance.example: Refreshing state... (ID:
i-027c1c02033735238) aws_eip.ip: Refreshing state... (ID: eipalloc-02ca8955cb6dd27e3) An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: - aws_eip.ip - aws_instance.example Plan: 0 to add, 0 to change, 2 to destroy. Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes -
আʢterraform destroyʣ -> % terraform destroy aws_instance.example: Refreshing state... (ID:
i-027c1c02033735238) aws_eip.ip: Refreshing state... (ID: eipalloc-02ca8955cb6dd27e3) An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: - aws_eip.ip - aws_instance.example Plan: 0 to add, 0 to change, 2 to destroy. Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes applyͱಉ༷ʹ࣮ߦplan͕࡞͞ΕΔ ͳ͚Ε࣮ߦ͢Δ -
আʢterraform destroyʣ -> % terraform destroy (omit) aws_instance.example: Destruction complete
after 1m20s ࣮ߦྃޙ݁Ռ͕දࣔ͞ΕΔ -
How to entrance Terraform • 1. TerraformͷงғؾΛެࣜIntroduction͔ ΒΔ • https://www.terraform.io/intro/
index.html • ެࣜυΩϡϝϯτͰɺTerraformͷ͍ ํେѲͰ͖Δ -
How to entrance Terraform • 2. TerraformͰ࣮ݱ͍ͨ͠Πϯϑϥߏʹ ͍ͭͯߟ͑ΔɾֶͿ • Consoleը໘ͰҰճ࡞ͬͨͷΛίʔυ
ͱͯ͠࠶ݱ͢ΔΞϓϩʔν͕Γ͔ͬ͢ ͨ