Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. variable "name" { default = "test" }
  2. variable "region" { default = "ap-northeast-1" }
  3.  
  4. variable "vpc_cidr" { default = "172.16.0.0/16" }
  5. variable "az" { default = "ap-northeast-1a" }
  6. variable "public_subnet" { default = "172.16.0.0/24" }
  7.  
  8. variable "web_instance_type" { default = "t2.micro" }
  9. variable "web_instance_ami_id" { default = "ami-383c1956" }
  10.  
  11. provider "aws" {
  12. region = "${var.region}"
  13. }
  14.  
  15. resource "aws_key_pair" "site_key" {
  16. key_name = "${var.name}"
  17. public_key = "${file("site_key.pub")}"
  18. }
  19.  
  20. resource "aws_vpc" "vpc" {
  21. cidr_block = "${var.vpc_cidr}"
  22. enable_dns_support = true
  23. enable_dns_hostnames = true
  24. }
  25.  
  26. resource "aws_internet_gateway" "public" {
  27. vpc_id = "${aws_vpc.vpc.id}"
  28. }
  29.  
  30. resource "aws_subnet" "public" {
  31. vpc_id = "${aws_vpc.vpc.id}"
  32. cidr_block = "${var.public_subnet}"
  33. availability_zone = "${var.az}"
  34. map_public_ip_on_launch = true
  35. }
  36.  
  37. resource "aws_route_table" "public" {
  38. vpc_id = "${aws_vpc.vpc.id}"
  39.  
  40. route {
  41. cidr_block = "0.0.0.0/0"
  42. gateway_id = "${aws_internet_gateway.public.id}"
  43. }
  44. }
  45.  
  46. resource "aws_route_table_association" "public" {
  47. subnet_id = "${aws_subnet.public.id}"
  48. route_table_id = "${aws_route_table.public.id}"
  49. }
  50.  
  51. resource "aws_network_acl" "acl" {
  52. vpc_id = "${aws_vpc.vpc.id}"
  53. subnet_ids = ["${aws_subnet.public.id}"]
  54.  
  55. ingress {
  56. protocol = "-1"
  57. rule_no = 100
  58. action = "allow"
  59. cidr_block = "0.0.0.0/0"
  60. from_port = 0
  61. to_port = 0
  62. }
  63.  
  64. egress {
  65. protocol = "-1"
  66. rule_no = 100
  67. action = "allow"
  68. cidr_block = "0.0.0.0/0"
  69. from_port = 0
  70. to_port = 0
  71. }
  72. }
  73.  
  74. resource "aws_security_group" "web" {
  75. name = "${var.name}-web"
  76. vpc_id = "${aws_vpc.vpc.id}"
  77. description = "${var.name}-SG"
  78.  
  79. ingress {
  80. from_port = 22
  81. to_port = 22
  82. protocol = "tcp"
  83. cidr_blocks = ["0.0.0.0/0"]
  84. }
  85.  
  86. egress {
  87. from_port = 0
  88. to_port = 0
  89. protocol = "-1"
  90. cidr_blocks = ["0.0.0.0/0"]
  91. }
  92. }
  93.  
  94. resource "aws_instance" "web" {
  95. ami = "${var.web_instance_ami_id}"
  96. instance_type = "${var.web_instance_type}"
  97. vpc_security_group_ids = ["${aws_security_group.web.id}"]
  98. subnet_id = "${aws_subnet.public.id}"
  99. key_name = "${aws_key_pair.site_key.key_name}"
  100. associate_public_ip_address = true
  101.  
  102. root_block_device {
  103. volume_type = "gp2"
  104. volume_size = 8
  105. }
  106.  
  107. user_data = <<EOT
  108. #!/usr/bin/env bash
  109.  
  110. yum update -y
  111. EOT
  112. }
  113.  
  114. output "web_public_ip" { value = "${aws_instance.web.public_ip}" }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement