Slide 1

Slide 1 text

TerraformͰ AWSͷΠϯϑϥߏ੒ߏஙΛ ࣗಈԽ͢Δʢೖ໳ʣ 2018/9/26 (Wed) ୈ130ճ PHPษڧձ@౦ژ @Khigashiguchi

Slide 2

Slide 2 text

ࣗݾ঺հ • ౦ޱ ࿨ᏻ @Khigashiguchi • Server Side EngineerʢPHP / Goʣ • BASE, Inc / BASE Product Division • Blog: http:// khigashigashi.hatenablog.com/

Slide 3

Slide 3 text

5FSSBGPSNͰ"84ͷΠϯϑϥߏ੒ߏஙΛࣗಈԽ͢Δ ೖ໳ IUUQLIJHBTIJHBTIJIBUFOBCMPHDPNFOUSZ

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

What is Terraform? • HashiCorp͕࡞͍ͬͯΔίʔυ͔ΒΠϯϑ ϥϦιʔεΛ࡞੒ɾ؅ཧ͢ΔͨΊͷπʔϧ • Infrastructure as Code • AWS, GCP, Azure, Heroku ͳͲଟ͘ͷSaaS ʹ෯޿͘ରԠ

Slide 6

Slide 6 text

Providers https://www.terraform.io/docs/providers/

Slide 7

Slide 7 text

Terraform Merit for PHPer Work • SREνʔϜͳͲͱͷΠϯϑϥʹؔ͢Δձ࿩ ͷ५׈Խ • TerraformͷϑΝΠϧΛϕʔεʹͨ͠ߏ੒ ৘ใڞ༗ • ϕϯμʔʹറΒΕͳ͍πʔϧ • ಛఆϕϯμʔͷΈͰ͸ͳ͍ͷͰԣల͕͖ ͘

Slide 8

Slide 8 text

Terraform Merit for PHPer Hobby • ݸਓΞϓϦ΍ֶश࣌ͷΠϯϑϥߏங • Ұ౓ίʔυͱͯ͠อଘ͓͚ͯ͠Δ͜ͱʹ ΑΔ࠶ར༻ੑ

Slide 9

Slide 9 text

Getting started Terraform: Example case • ʮEIP(Elastic IP)Λඥ͚ͮͨEC2 InstanceΛ ࡞Δʯ 1. Configuration fileʢ֦ுࢠ .tfʣͷ࡞੒ 2. ॳظԽʢterraform initʣ 3. ߏஙʢterraform applyʣ 4. ࡟আʢterraform destroyʣ

Slide 10

Slide 10 text

IUUQTHJUIVCDPN,IJHBTIJHVDIJUFSSBGPSNTOJQQFUTUSFFNBTUFS TJNQMFEFNP Demonstration

Slide 11

Slide 11 text

Configuration fileʢ֦ுࢠ .tfʣͷ࡞੒ $ ls main.tf variables.tf terraform.tfvars .tf ֦ுࢠ

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Configuration fileʢ֦ுࢠ .tfʣͷ࡞੒ variable "aws_access_key" {} variable "aws_secret_key" {} variable "aws_region" { default = "ap-northeast-1" } variables.tf

Slide 17

Slide 17 text

Configuration fileʢ֦ுࢠ .tfʣͷ࡞੒ variable "aws_access_key" {} variable "aws_secret_key" {} variable "aws_region" { default = "ap-northeast-1" } variables.tf Input variablesͱͯ͠ఆٛ .tfvars ϑΝΠϧ͕༗Ε͹ͦ͜ʹهࡌ͞Εͨ஋Λࢀরɻ ͳ͚Ε͹ίϚϯυΠϯλʔϑΣʔεʹͯೖྗɻ

Slide 18

Slide 18 text

Configuration fileʢ֦ுࢠ .tfʣͷ࡞੒ aws_access_key = "YOUR-ACCESS-KEY" aws_secret_key = "YOUR-SECRET-KEY" aws_region = "ap-northeast-1" terreform.tfvars variablesʹ୅ೖ͍ͨ͠஋Λઃఆ͢Δ

Slide 19

Slide 19 text

ॳظԽʢ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!

Slide 20

Slide 20 text

ߏஙʢ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:

Slide 21

Slide 21 text

ߏஙʢ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ʹ໰୊͕ͳ͚Ε͹࣮ࡍʹ࣮ߦ͢Δɻ

Slide 22

Slide 22 text

ߏஙʢterraform applyʣ -> % terraform apply (omit) Apply complete! Resources: 2 added, 0 changed, 0 destroyed. Outputs: ip = xx.xxx.xxx.xxx ࣮ߦ׬ྃޙ݁Ռ͕දࣔ͞ΕΔ

Slide 23

Slide 23 text

࡟আʢ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

Slide 24

Slide 24 text

࡟আʢ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͕࡞੒͞ΕΔ ໰୊ͳ͚Ε͹࣮ߦ͢Δ

Slide 25

Slide 25 text

࡟আʢterraform destroyʣ -> % terraform destroy (omit) aws_instance.example: Destruction complete after 1m20s ࣮ߦ׬ྃޙ݁Ռ͕දࣔ͞ΕΔ

Slide 26

Slide 26 text

How to entrance Terraform • Terraform͸ɺΠϯϑϥߏ੒ɾߏஙΛίʔ υͱͯ͠දݱ͢Δπʔϧ • →Πϯϑϥߏ੒ɾߏஙʹ͍ͭͯͷجૅ஌ ͕ࣝඞཁ

Slide 27

Slide 27 text

Step5: ECSλεΫఆٛ ʮAmazon Web Services جૅ͔ΒͷωοτϫʔΫˍαʔ όʔߏஙɹվగ൛ʯ IUUQTXXXOJLLFJCQDPKQBUDMQVCNLUCPPL

Slide 28

Slide 28 text

How to entrance Terraform • 1. TerraformͷงғؾΛެࣜIntroduction͔ Β஌Δ • https://www.terraform.io/intro/ index.html • ެࣜυΩϡϝϯτͰɺTerraformͷ࢖͍ ํ͸େ࿮೺ѲͰ͖Δ

Slide 29

Slide 29 text

How to entrance Terraform • 2. TerraformͰ࣮ݱ͍ͨ͠Πϯϑϥߏ੒ʹ ͍ͭͯߟ͑ΔɾֶͿ • Consoleը໘ͰҰճ࡞ͬͨ΋ͷΛίʔυ ͱͯ͠࠶ݱ͢ΔΞϓϩʔν͕΍Γ΍͔ͬ͢ ͨ

Slide 30

Slide 30 text

5FSSBGPSNͰ"84ͷΠϯϑϥߏ੒ߏஙΛࣗಈԽ͢Δ ೖ໳ IUUQLIJHBTIJHBTIJIBUFOBCMPHDPNFOUSZ ʮAmazon Web Services جૅ͔Βͷωοτ ϫʔΫˍαʔόʔߏஙɹվగ൛ʯͰͷ಺༰Λ 5FSSBGPSNͰ࣮ݱ͢Δ

Slide 31

Slide 31 text

·ͱΊ • TerraformʹΑͬͯΠϯϑϥΛίʔυͱͯ͠ఆ ٛ • TerraformΛ࢖͍ͬͯΔݱ৔Ͱ͋Ε͹ɺΠϯϑ ϥʹ͍ͭͯ࿩͢५׈༉ͱͯ͠༗ޮ • ݸਓ؀ڥͱͯ͠࢖͏ʹ΋ྑ޷

Slide 32

Slide 32 text

͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·͠ ͨɻ @Khigashiguchi