Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. ##############################
  2. # This script setup whole infrastructure on AWS to run ec2 instance this will include VPC Setup
  3. # (Including : "VPC", "Subnet", "internet Gateway", "Route Table", "Security Group to allow SSH")
  4. # also it sets up EC2 Instance along with keypairs
  5. ##############################
  6. ### Provider Setup
  7. provider "aws" {
  8. region = "us-east-2"
  9. }
  10. ### Variables declearation
  11. variable "cidr_vpc" {
  12. description = "CIDR block for the VPC"
  13. default = "192.168.0.0/16"
  14. }
  15. variable "cidr_subnet" {
  16. description = "CIDR block for the subnet"
  17. default = "192.168.1.0/24"
  18. }
  19. variable "availability_zone" {
  20. description = "availability zone to create subnet"
  21. default = "us-east-2a"
  22. }
  23.  
  24. variable "instance_type" {
  25. description = "type for aws EC2 instance"
  26. default = "t2.micro"
  27. }
  28. variable "environment_tag" {
  29. description = "Environment tag"
  30. default = "Production"
  31. }
  32. variable "public_key_path" {
  33. description = "Public key path"
  34. default = "/Users/krypton/.ssh/id_rsa.pub"
  35. }
  36. ### ++++++++++++++++++
  37. ### Keypair creation
  38. resource "aws_key_pair" "ec2key" {
  39. key_name = "publicKey"
  40. public_key = "${file(var.public_key_path)}"
  41. }
  42.  
  43. #### AWS VPC Creation
  44. resource "aws_vpc" "vpc" {
  45. cidr_block = "${var.cidr_vpc}"
  46. enable_dns_support = true
  47. enable_dns_hostnames = true
  48. tags = {
  49. Environment = "${var.environment_tag}"
  50. }
  51. }
  52.  
  53. resource "aws_internet_gateway" "igw" {
  54. vpc_id = "${aws_vpc.vpc.id}"
  55. tags = {
  56. Environment = "${var.environment_tag}"
  57. }
  58. }
  59.  
  60. ### ++++++++++++++++++
  61.  
  62. #### Subnet Creation
  63. resource "aws_subnet" "subnet_public" {
  64. vpc_id = "${aws_vpc.vpc.id}"
  65. cidr_block = "${var.cidr_subnet}"
  66. map_public_ip_on_launch = "true"
  67. availability_zone = "${var.availability_zone}"
  68. tags = {
  69. Environment = "${var.environment_tag}"
  70. }
  71. }
  72. ### ++++++++++++++++++
  73.  
  74. #### Route Table Creation
  75.  
  76. resource "aws_route_table" "rtb_public" {
  77. vpc_id = "${aws_vpc.vpc.id}"
  78.  
  79. route {
  80. cidr_block = "0.0.0.0/0"
  81. gateway_id = "${aws_internet_gateway.igw.id}"
  82. }
  83.  
  84. tags = {
  85. Environment = "${var.environment_tag}"
  86. }
  87. }
  88. ### ++++++++++++++++++
  89.  
  90. #### Route Table Association
  91. resource "aws_route_table_association" "rta_subnet_public" {
  92. subnet_id = "${aws_subnet.subnet_public.id}"
  93. route_table_id = "${aws_route_table.rtb_public.id}"
  94. }
  95. ### ++++++++++++++++++
  96.  
  97. #### Security Group
  98. resource "aws_security_group" "sg_22" {
  99. name = "sg_22"
  100. vpc_id = "${aws_vpc.vpc.id}"
  101.  
  102. ingress {
  103. from_port = 22
  104. to_port = 22
  105. protocol = "tcp"
  106. cidr_blocks = ["0.0.0.0/0"]
  107. }
  108.  
  109. egress {
  110. from_port = 0
  111. to_port = 0
  112. protocol = "-1"
  113. cidr_blocks = ["0.0.0.0/0"]
  114. }
  115.  
  116. tags = {
  117. Environment = "${var.environment_tag}"
  118. }
  119. }
  120. ### ++++++++++++++++
  121.  
  122. #### AMI naming setup
  123. data "aws_ami" "ubuntu" {
  124. most_recent = true
  125.  
  126. filter {
  127. name = "name"
  128. values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]
  129. }
  130.  
  131. filter {
  132. name = "virtualization-type"
  133. values = ["hvm"]
  134. }
  135. owners = ["099720109477"]
  136. }
  137. ## ++++++++++++++
  138.  
  139. #### EC2 Launch Defination
  140. resource "aws_instance" "web-1" {
  141. ami = "${data.aws_ami.ubuntu.id}"
  142. instance_type = "t2.micro"
  143. key_name = "${aws_key_pair.ec2key.key_name}"
  144. subnet_id = "${aws_subnet.subnet_public.id}"
  145. vpc_security_group_ids = ["${aws_security_group.sg_22.id}"]
  146. }
  147. ## ++++++++++++++
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement