Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. // variables
  2. variable availability_zone {
  3. type = "string"
  4. default = "ap-southeast-2a"
  5. }
  6.  
  7. variable vpc_id {
  8. type = "string"
  9. default = "vpc-xxx"
  10. }
  11.  
  12. variable ami_id {
  13. type = "string"
  14. default = "ami-xxx"
  15. }
  16.  
  17. variable subnet_id {
  18. type = "string"
  19. default = "subnet-xxx"
  20. }
  21.  
  22. // instance
  23. resource "aws_instance" "jenkins_slave" {
  24. count = 2
  25. ami = "${var.ami_id}"
  26. instance_type = "m4.large"
  27. availability_zone = "${var.availability_zone}"
  28. security_groups = [
  29. "${aws_security_group.jenkins_slave.id}"
  30. ]
  31. root_block_device {
  32. volume_type = "gp2"
  33. volume_size = "50"
  34. }
  35. subnet_id = "${aws_subnet.jenkins_slave.id}"
  36. key_name = "jenkins"
  37. tags = {
  38. Name = "jenkins-slave-${count.index + 1}"
  39. }
  40. user_data = <<EOF
  41. #!/bin/bash
  42. sudo apt-get update
  43. sudo apt-get install -y openjdk-8-jdk
  44. sudo mkdir -p /var/lib/jenkins
  45. sudo chown -R ubuntu:ubuntu /var/lib/jenkins
  46. EOF
  47. }
  48.  
  49. // security group
  50. resource "aws_security_group" "jenkins_slave" {
  51. name = "jenkins-slave-sg"
  52. description = "Security group for jenkins slaves"
  53. vpc_id = "${var.vpc_id}"
  54. tags {
  55. Name = "sg-jenkins-slave"
  56. }
  57. }
  58.  
  59. resource "aws_security_group_rule" "in_ssh" {
  60. description = "Allow incoming SSH traffic"
  61. type = "ingress"
  62. from_port = 22
  63. to_port = 22
  64. protocol = "tcp"
  65. cidr_blocks = [
  66. "0.0.0.0/0"
  67. ]
  68. security_group_id = "${aws_security_group.jenkins_slave.id}"
  69. }
  70.  
  71. resource "aws_security_group_rule" "out_all" {
  72. description = "Allow all outoging traffic"
  73. type = "egress"
  74. from_port = 0
  75. to_port = 65535
  76. protocol = "All"
  77. cidr_blocks = [
  78. "0.0.0.0/0"
  79. ]
  80. security_group_id = "${aws_security_group.jenkins_slave.id}"
  81. }
  82.  
  83. // subnet
  84. resource "aws_subnet" "jenkins_slave" {
  85. availability_zone = "${var.availability_zone}"
  86. cidr_block = "10.0.1.0/24"
  87. vpc_id = "${var.vpc_id}"
  88. tags = {
  89. Name = "sn-jenkins-slave"
  90. }
  91. }
  92.  
  93. resource "aws_route" "jenkins_slave" {
  94. route_table_id = "${aws_route_table.jenkins_slave.id}"
  95. destination_cidr_block = "0.0.0.0/0"
  96. nat_gateway_id = "${aws_nat_gateway.jenkins_slave.id}"
  97. }
  98.  
  99. resource "aws_route_table_association" "jenkins_slave" {
  100. route_table_id = "${aws_route_table.jenkins_slave.id}"
  101. subnet_id = "${aws_subnet.jenkins_slave.id}"
  102. }
  103.  
  104. resource "aws_route_table" "jenkins_slave" {
  105. vpc_id = "${var.vpc_id}"
  106. tags {
  107. Name = "rt-jenkins-slave"
  108. }
  109. }
  110.  
  111. resource "aws_nat_gateway" "jenkins_slave" {
  112. allocation_id = "${aws_eip.jenkins_slave.id}"
  113. subnet_id = "${var.subnet_id}"
  114. tags {
  115. Name = "ngw-jenkins-slave"
  116. }
  117. }
  118.  
  119. resource "aws_eip" "jenkins_slave" {
  120. vpc = true
  121. tags {
  122. Name = "eip-jenkins-slave"
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement