-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
216 lines (177 loc) · 6.6 KB
/
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
variable "aws_region" {
description = "AWS region"
type = string
default = "us-east-1"
}
# Configure the AWS Provider
provider "aws" {
region = var.aws_region
}
# Get the current AWS account details
data "aws_caller_identity" "current" {}
# Get the default VPC
data "aws_vpc" "default" {
default = true
}
locals {
account_id = data.aws_caller_identity.current.account_id
default_vpc_id = data.aws_vpc.default.id
common_tags = {
project = "blog-terraform-docker"
}
}
# Create ECR repository
resource "aws_ecr_repository" "blog_terraform_docker" {
name = "blog-terraform-docker"
image_scanning_configuration {
scan_on_push = true
}
tags = local.common_tags
}
# Keep only one untagged image that precedes the latest image
resource "aws_ecr_lifecycle_policy" "blog_terraform_docker" {
repository = aws_ecr_repository.blog_terraform_docker.name
policy = <<EOF
{
"rules": [
{
"rulePriority": 1,
"description": "Keep only one untagged image that precedes the latest image",
"selection": {
"tagStatus": "untagged",
"countType": "imageCountMoreThan",
"countNumber": 1
},
"action": {
"type": "expire"
}
}
]
}
EOF
}
# Build Docker image and push to ECR repository
# https://docs.aws.amazon.com/AmazonECR/latest/userguide/docker-push-ecr-image.html
resource "terraform_data" "docker_build" {
depends_on = [aws_ecr_repository.blog_terraform_docker]
# To make sure the local-exec provisioner runs every time
triggers_replace = [timestamp()]
provisioner "local-exec" {
command = <<EOF
aws ecr get-login-password --region ${var.aws_region} | docker login --username AWS --password-stdin ${local.account_id}.dkr.ecr.${var.aws_region}.amazonaws.com
# https://stackoverflow.com/a/68004485
docker build --platform linux/amd64 -t "${aws_ecr_repository.blog_terraform_docker.repository_url}:latest" .
docker push "${aws_ecr_repository.blog_terraform_docker.repository_url}:latest"
EOF
}
}
# IAM Role for the EC2 instance to allow it to pull images from ECR.
resource "aws_iam_role" "blog_terraform_docker_ec2_role" {
name = "blog-terraform-docker-ec2-role"
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = "sts:AssumeRole",
Effect = "Allow",
Principal = {
Service = "ec2.amazonaws.com",
},
},
],
})
tags = local.common_tags
}
# Policy Attachment to allow EC2 instance to communicate with ECR.
resource "aws_iam_role_policy_attachment" "blog_terraform_docker_ec2_ecr_policy" {
role = aws_iam_role.blog_terraform_docker_ec2_role.name
# https://us-east-1.console.aws.amazon.com/iam/home?region=us-east-1#/policies/details/arn%3Aaws%3Aiam%3A%3Aaws%3Apolicy%2FAmazonEC2ContainerRegistryReadOnly?section=permissions
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
}
# Create an EC2 instance profile which will house the role.
resource "aws_iam_instance_profile" "blog_terraform_docker_ec2_instance_profile" {
name = "blog-terraform-docker-ec2-instance-profile"
role = aws_iam_role.blog_terraform_docker_ec2_role.name
tags = local.common_tags
}
# Create a Security Group to allow SSH access to the instance.
module "dev_ssh_sg" {
# https://registry.terraform.io/modules/terraform-aws-modules/security-group/aws/latest
source = "terraform-aws-modules/security-group/aws"
name = "dev_ssh_sg"
description = "Security group for SSH access"
vpc_id = local.default_vpc_id
ingress_cidr_blocks = ["108.53.23.213/32", "38.122.226.210/32"]
ingress_rules = ["ssh-tcp"]
tags = local.common_tags
}
# Create a Security Group to allow HTTP access to the instance.
module "ec2_sg" {
# https://registry.terraform.io/modules/terraform-aws-modules/security-group/aws/latest
source = "terraform-aws-modules/security-group/aws"
name = "ec2_sg"
description = "Security group for ec2_sg"
vpc_id = local.default_vpc_id
ingress_cidr_blocks = ["0.0.0.0/0"]
ingress_rules = ["http-80-tcp"]
egress_rules = ["all-all"]
tags = local.common_tags
}
resource "aws_instance" "blog_terraform_docker_ec2_instance" {
lifecycle {
# https://developer.hashicorp.com/terraform/language/meta-arguments/lifecycle#replace_triggered_by
replace_triggered_by = [terraform_data.docker_build.id]
}
# Amazon Linux 2023 AMI 2023.4.20240416.0 x86_64 HVM kernel-6.1
# https://us-east-1.console.aws.amazon.com/ec2/home?region=us-east-1#ImageDetails:imageId=ami-04e5276ebb8451442
ami = "ami-04e5276ebb8451442"
instance_type = "t3.micro"
key_name = "blog-terraform-docker" # https://us-east-1.console.aws.amazon.com/ec2/home?region=us-east-1#KeyPairs:
vpc_security_group_ids = [
module.ec2_sg.security_group_id,
module.dev_ssh_sg.security_group_id
]
iam_instance_profile = aws_iam_instance_profile.blog_terraform_docker_ec2_instance_profile.name
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-docker.html
user_data = <<-EOF
#!/bin/bash
sudo yum update -y
sudo yum install -y docker
sudo service docker start
sudo usermod -a -G docker $(whoami)
docker ps
# https://github.com/awslabs/amazon-ecr-credential-helper?tab=readme-ov-file#amazon-linux-2023-al2023
sudo dnf install -y amazon-ecr-credential-helper
# https://github.com/awslabs/amazon-ecr-credential-helper?tab=readme-ov-file#docker
# Check if ~/.docker/config.json exists
if [ ! -f ~/.docker/config.json ]; then
# If not, create the file
mkdir -p ~/.docker
touch ~/.docker/config.json
fi
# Update the file with the content { "credsStore": "ecr-login" }
echo '{ "credsStore": "ecr-login" }' > ~/.docker/config.json
# Pull the image from ECR and run a container with it.
docker pull ${aws_ecr_repository.blog_terraform_docker.repository_url}:latest
docker run -d -p 80:3000 ${aws_ecr_repository.blog_terraform_docker.repository_url}:latest
# https://serverfault.com/questions/228481/where-is-log-output-from-cloud-init-stored
EOF
tags = local.common_tags
}
output "account_id" {
value = local.account_id
}
output "public_ip" {
value = aws_instance.blog_terraform_docker_ec2_instance.public_ip
}
output "public_dns" {
value = aws_instance.blog_terraform_docker_ec2_instance.public_dns
}