Last active
December 13, 2016 16:30
-
-
Save jkkitakita/f30c0dca8268cc38ef20869110375115 to your computer and use it in GitHub Desktop.
[初心者歓迎] Packer + Ansible + TerraformでWordPressを構築 ref: http://qiita.com/jkkitakita/items/805bc6dcfebcdbd47c00
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
~/hashicorp/packer | |
~/h/packer ❯❯❯ packer build -var-file=variables.json packer-wordpress.json | |
amazon-ebs output will be in this color. | |
==> amazon-ebs: Prevalidating AMI Name... | |
amazon-ebs: Found Image ID: ami-504f2037 | |
~~ 中略 ~~ | |
Build 'amazon-ebs' finished. | |
==> Builds finished. The artifacts of successful builds are: | |
--> amazon-ebs: AMIs were created: | |
ap-northeast-1: ami-504f2037 | |
~/h/packer ❯❯❯ |
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
~/h/terraform ❯❯❯ terraform apply | |
data.aws_ami.wordpress: Refreshing state... | |
aws_instance.wordpress: Creating... | |
ami: "" => "ami-504f2037" // Packerで作成したAMIを取得できてる。 | |
availability_zone: "" => "<computed>" | |
ebs_block_device.#: "" => "<computed>" | |
ephemeral_block_device.#: "" => "<computed>" | |
instance_state: "" => "<computed>" | |
instance_type: "" => "t2.micro" | |
key_name: "" => "jkkitakita" | |
network_interface_id: "" => "<computed>" | |
placement_group: "" => "<computed>" | |
private_dns: "" => "<computed>" | |
private_ip: "" => "<computed>" | |
public_dns: "" => "<computed>" | |
public_ip: "" => "<computed>" | |
root_block_device.#: "" => "<computed>" | |
security_groups.#: "" => "<computed>" | |
source_dest_check: "" => "true" | |
subnet_id: "" => "<computed>" | |
tags.%: "" => "1" | |
tags.Name: "" => "wordpress01" | |
tenancy: "" => "<computed>" | |
vpc_security_group_ids.#: "" => "<computed>" | |
aws_instance.wordpress: Still creating... (10s elapsed) | |
aws_instance.wordpress: Still creating... (20s elapsed) | |
aws_instance.wordpress: Creation complete | |
Apply complete! Resources: 1 added, 0 changed, 0 destroyed. | |
The state of your infrastructure has been saved to the path | |
below. This state is required to modify and destroy your | |
infrastructure, so keep it safe. To inspect the complete state | |
use the `terraform show` command. | |
State path: terraform.tfstate | |
~/h/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
provider "aws" { | |
region = "${var.region}" | |
} |
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
{ | |
"builders": [{ | |
"type": "amazon-ebs", | |
"region": "{{user `aws_region`}}", | |
"source_ami": "{{user `aws_source_ami_id`}}", | |
"instance_type": "{{user `aws_instance_type`}}", | |
"ssh_username": "{{user `ssh_username`}}", | |
"ssh_timeout": "5m", | |
"ami_name": "Packer-ansible-{{timestamp}}", | |
"tags": { | |
"Name": "aws-{{user `aws_region`}}", | |
"Environment": "{{user `aws_environment`}}", | |
"Release": "Latest" | |
} | |
}], | |
"provisioners": [ | |
{ | |
"type": "shell", | |
"execute_command": "echo {{user `ssh_username`}} | {{ .Vars }} sudo -E -S sh '{{ .Path }}'", | |
"inline": [ | |
"mkdir -p /ops/{{user `scripts_dir`}}", | |
"chmod a+w /ops/{{user `scripts_dir`}}" | |
] | |
}, | |
{ | |
"type": "file", | |
"source": "{{user `scripts_dir`}}/.", | |
"destination": "/ops/{{user `scripts_dir`}}" | |
}, | |
{ | |
"type": "shell", | |
"execute_command": "echo {{user `ssh_username`}} | {{ .Vars }} sudo -E -S sh '{{ .Path }}'", | |
"inline": [ | |
"bash /ops/{{user `scripts_dir`}}/bootstrap.sh", | |
"bash /ops/{{user `scripts_dir`}}/ansible.sh" | |
] | |
}, | |
{ | |
"type": "ansible-local", | |
"group_vars": "./ansible/group_vars", | |
"playbook_file": "./ansible/site.yml", | |
"role_paths": ["./ansible/roles/common", "./ansible/roles/mysql", "./ansible/roles/nginx", "./ansible/roles/php-fpm", "./ansible/roles/wordpress"], | |
"staging_directory": "/tmp/packer-provisioner-ansible-local" | |
} | |
] | |
} |
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 "aws" { | |
region = "${var.region}" | |
} | |
~/h/terraform ❯❯❯ more variables.tf | |
variable "region" { | |
default = "ap-northeast-1" | |
} | |
variable "ec2_config" { | |
default = { | |
key_name = "jkkitakita" | |
instance_type = "t2.micro" | |
} | |
} |
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
{ | |
"scripts_dir": "scripts", | |
"ssh_username": "ec2-user", | |
"aws_region": "ap-northeast-1", | |
"aws_instance_type": "t2.micro", | |
"aws_environment": "production", | |
"aws_source_ami_id": "ami-########" | |
} |
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
data "aws_ami" "wordpress" { | |
filter { | |
name = "state" | |
values = ["available"] | |
} | |
filter { | |
name = "tag:Release" | |
values = ["Latest"] // PackerでAMI作成時につけたタグ | |
} | |
most_recent = true // 一番新しいものを取得 | |
} | |
resource "aws_instance" "wordpress" { | |
ami = "${data.aws_ami.wordpress.id}" | |
key_name = "${lookup(var.ec2_config, "key_name")}" | |
instance_type = "${lookup(var.ec2_config, "instance_type")}" | |
tags { | |
"Name" = "${format("wordpress%02d", count.index + 1)}" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment