File size: 2,450 Bytes
8202fe8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Provides a resource to manage EC2 Fleets.
#
# Usage:
# Configure the credentials first with `aws configure`
# Create a file named `terraform.tfvars` and set the values of the variables defined in `variables.tf`
#
# terraform init      Initialize a Terraform working directory
# terraform validate  Validates the Terraform files
# terraform fmt       Rewrites config files to canonical format
# terraform plan      Generate and show an execution plan
# terraform apply     Builds or changes infrastructure
# terraform destroy   Destroy Terraform-managed infrastructure

provider "aws" {
  region = var.region
}

locals {
  common_tags = {
    Name    = "guardai"
    project = var.project_tag
  }
}

resource "aws_security_group" "guardai_ami_sg" {
  ingress {
    from_port = 22
    to_port   = 22
    protocol  = "tcp"

    cidr_blocks = [
      "0.0.0.0/0",
    ]
  }

  ingress {
    from_port = 80
    to_port   = 80
    protocol  = "tcp"

    cidr_blocks = [
      "0.0.0.0/0",
    ]
  }

  ingress {
    from_port = 443
    to_port   = 443
    protocol  = "tcp"

    cidr_blocks = [
      "0.0.0.0/0",
    ]
  }


  # outbound internet access
  # allowed: any egress traffic to anywhere
  egress {
    from_port = 0
    to_port   = 0
    protocol  = "-1"

    cidr_blocks = [
      "0.0.0.0/0",
    ]
  }

  tags = local.common_tags
}

# Provides an EC2 launch template resource.
# Can be used to create EC2 instances or auto scaling groups.
resource "aws_launch_template" "guardai_ami_lt" {
  name_prefix = "guard_ai_launch_template"
  image_id    = var.ami_id
  key_name    = var.key_name

  vpc_security_group_ids = [
    aws_security_group.guardai_ami_sg.id,
  ]

  instance_type = var.instance_type

  tag_specifications {
    # Tags of EC2 instances
    resource_type = "instance"
    tags = local.common_tags
  }

  tag_specifications {
    # Tags of EBS volumes
    resource_type = "volume"
    tags = local.common_tags
  }
}


# Assign elastic IP to the instance
resource "aws_eip_association" "eip_assoc" {
  instance_id   = aws_instance.main_instance.id
  allocation_id  = var.eip_id
}


# Launch an EC2 instance
resource "aws_instance" "main_instance" {
  launch_template {
    id      = aws_launch_template.guardai_ami_lt.id
    version = "$Latest"
  }

  # The tags of the Fleet resource itself.
  # To tag instances at launch, specify the tags in the Launch Template.
  tags = local.common_tags
  availability_zone = var.zone
}