-
Notifications
You must be signed in to change notification settings - Fork 0
/
tf_main.tf
134 lines (114 loc) · 3.53 KB
/
tf_main.tf
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
## CONFIGURE AWS PROVIDER ##
provider "aws" {
shared_credentials_files = [var.aws_creds_path]
region = var.aws_region
}
## define aws instance AMI to use
data "aws_ami" "amazon_linux_2" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-ebs"]
}
}
## create Keypair for ssh access
resource "tls_private_key" "rsa_4096" {
algorithm = "RSA"
rsa_bits = 4096
}
# create key pair for connecting to EC2 via SSH
resource "aws_key_pair" "key_pair" {
key_name = var.key_name
public_key = tls_private_key.rsa_4096.public_key_openssh
}
resource "local_file" "private_key" {
content = tls_private_key.rsa_4096.private_key_pem
filename = var.key_name
provisioner "local-exec" {
command = "chmod 400 ${var.key_name}"
}
}
## Network: subnets and VPC
resource "aws_default_vpc" "default" {
tags = {
Name = "Default webserver VPC"
}
}
## Security group
resource "aws_security_group" "main" {
name = "Webserver SG"
description = "Webserver Security Group"
vpc_id = aws_default_vpc.default.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
## Create iam instance profile
resource "aws_iam_role" "role" {
path = "/"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "attachment" {
role = aws_iam_role.role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
}
resource "aws_iam_instance_profile" "profile" {
role = aws_iam_role.role.name
}
## Create a new host with instance type
resource "aws_instance" "fcx_backend_graphql_api" {
# ami = data.aws_ami.ubuntu.id
ami = data.aws_ami.amazon_linux_2.id
instance_type = var.instance_type
iam_instance_profile = aws_iam_instance_profile.profile.name
key_name = aws_key_pair.key_pair.key_name
vpc_security_group_ids = [aws_security_group.main.id]
# bootstrap EC2 instance
# user_data = base64encode(templatefile("${path.module}/tf_dockerwork.sh", { accountId = var.accountId, aws_region = var.aws_region }))
user_data = <<-EOF
#!/bin/bash
sudo yum update -y
sudo amazon-linux-extras install docker -y
sudo service docker start
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
usermod -a -G docker ec2-user
sudo yum install amazon-ecr-credential-helper -y
echo '{"credsStore": "ecr-login"}' > ~/.docker/config.json
aws --region ${var.aws_region} ecr get-authorization-token
aws ecr get-login-password --region ${var.aws_region} | docker login --username AWS --password-stdin ${var.accountId}.dkr.ecr.${var.aws_region}.amazonaws.com
EOF
tags = {
Name = "fcx-backend-graphql-api"
}
}