-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
673 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.terraform* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
resource "aws_cloudfront_distribution" "photogram_CF" { | ||
origin { | ||
domain_name = aws_elb.photogram_ELB.dns_name | ||
origin_id = "photogram_ELB_origin" | ||
} | ||
|
||
default_cache_behavior { | ||
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"] | ||
target_origin_id = "photogram_ELB_origin" | ||
|
||
forwarded_values { | ||
query_string = false | ||
cookies { | ||
forward = "none" | ||
} | ||
} | ||
|
||
viewer_protocol_policy = "redirect-to-https" | ||
cached_methods = ["GET", "HEAD"] | ||
} | ||
|
||
viewer_certificate { | ||
cloudfront_default_certificate = true | ||
} | ||
|
||
restrictions { | ||
geo_restriction { | ||
restriction_type = "none" | ||
} | ||
} | ||
|
||
aliases = ["photogram.0x0.kr"] | ||
|
||
enabled = true | ||
is_ipv6_enabled = true | ||
price_class = "PriceClass_100" | ||
default_root_object = "index.html" | ||
} | ||
|
||
|
||
|
||
|
||
resource "aws_cloudfront_origin_access_identity" "photogram_oai" { | ||
comment = "Allows CloudFront to access the S3 bucket" | ||
} | ||
|
||
resource "aws_cloudfront_distribution" "photogram_image_CF" { | ||
origin { | ||
domain_name = aws_s3_bucket.photogram_image.bucket_regional_domain_name | ||
origin_id = "photogram_image_bucket_origin" | ||
|
||
s3_origin_config { | ||
origin_access_identity = aws_cloudfront_origin_access_identity.photogram_oai.cloudfront_access_identity_path | ||
} | ||
} | ||
|
||
default_cache_behavior { | ||
allowed_methods = ["GET", "HEAD"] | ||
cached_methods = ["GET", "HEAD"] | ||
target_origin_id = "photogram_image_bucket_origin" | ||
|
||
forwarded_values { | ||
query_string = false | ||
cookies { | ||
forward = "none" | ||
} | ||
} | ||
|
||
viewer_protocol_policy = "redirect-to-https" | ||
} | ||
|
||
viewer_certificate { | ||
cloudfront_default_certificate = true | ||
} | ||
|
||
restrictions { | ||
geo_restriction { | ||
restriction_type = "none" | ||
} | ||
} | ||
|
||
aliases = ["photogram-image.0x0.kr"] | ||
|
||
enabled = true | ||
is_ipv6_enabled = true | ||
price_class = "PriceClass_100" | ||
default_root_object = "index.html" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
//webserver ec2 instance | ||
resource "aws_instance" "photogram_webserver" { | ||
ami = "ami-0440d3b780d96b29d" # Replace with your desired AMI ID | ||
instance_type = "t2.micro" # Or any instance type you prefer | ||
|
||
provisioner "remote-exec" { | ||
inline = [ | ||
"sudo yum install -y nodejs npm --enablerepo=epel", | ||
"sudo npm install -g forever", | ||
"mkdir web", | ||
"aws s3 sync --region=ap-northeast-1 s3://photogram.src/web web", | ||
"cd web", | ||
"npm install" | ||
] | ||
} | ||
} | ||
|
||
resource "aws_security_group" "photogram_webserver_sg" { | ||
name = "photogram-webserver-sg" | ||
description = "Security group for Photogram web servers" | ||
vpc_id = aws_vpc.photogram_vpc.id | ||
|
||
ingress { | ||
from_port = 80 | ||
to_port = 80 | ||
protocol = "tcp" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
|
||
egress { | ||
from_port = 0 | ||
to_port = 65535 | ||
protocol = "tcp" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
} | ||
|
||
resource "aws_security_group" "photogram_asg_sg" { | ||
name = "photogram-asg-sg" | ||
description = "Security group for Photogram auto scaling groups" | ||
vpc_id = aws_vpc.photogram_vpc.id | ||
|
||
ingress { | ||
from_port = 22 | ||
to_port = 80 | ||
protocol = "tcp" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
|
||
egress { | ||
from_port = 0 | ||
to_port = 65535 | ||
protocol = "tcp" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
} | ||
|
||
resource "aws_ami_from_instance" "photogram_webserver_ami" { | ||
name = "photogram_webserver_ami" | ||
source_instance_id = aws_instance.photogram_webserver.id | ||
} | ||
|
||
resource "aws_launch_configuration" "photogram_webserver_lc" { | ||
name = "photogram_webserver_lc" | ||
image_id = aws_ami_from_instance.photogram_webserver_ami.id | ||
instance_type = "t2.micro" | ||
security_groups = [aws_security_group.photogram_webserver_sg.id] # Specify your security group ID | ||
key_name = "your_key_pair" # Specify your key pair name | ||
user_data = <<-EOF | ||
#!/bin/bash | ||
cd /home/ec2-user | ||
aws s3 sync --region=ap-northeast-1 \ | ||
s3://examplephoto.src/ExamplePhotoWebServer ExamplePhotoWebServer | ||
cd ExamplePhotoWebServer | ||
npm install | ||
forever start -w app.js | ||
EOF | ||
} | ||
|
||
resource "aws_autoscaling_group" "photogram_webserver_asg" { | ||
name = "photogram_webserver_asg" | ||
launch_configuration = aws_launch_configuration.photogram_webserver_lc.name | ||
min_size = 1 # Minimum number of instances | ||
max_size = 5 # Maximum number of instances | ||
desired_capacity = 2 # Desired number of instances | ||
vpc_zone_identifier = [aws_subnet.photogram_subnet.id] # Specify your subnet IDs # Specify your target group ARN if using ALB/NLB | ||
load_balancers = [aws_elb.photogram_ELB.name] | ||
enabled_metrics = ["GroupMinSize", "GroupMaxSize", "GroupDesiredCapacity", "GroupInServiceInstances", "GroupPendingInstances", "GroupStandbyInstances", "GroupTerminatingInstances", "GroupTotalInstances"] | ||
metrics_granularity = "1Minute" | ||
} | ||
|
||
|
||
resource "aws_autoscaling_policy" "cpu_scaling_policy" { | ||
name = "cpu_scaling_policy" | ||
scaling_adjustment = 1 | ||
adjustment_type = "ChangeInCapacity" | ||
cooldown = 300 | ||
autoscaling_group_name = aws_autoscaling_group.photogram_webserver_asg.name | ||
|
||
target_tracking_configuration { | ||
predefined_metric_specification { | ||
predefined_metric_type = "ASGAverageCPUUtilization" | ||
} | ||
target_value = 80 # Set the desired target value (e.g., 50 for 50% CPU utilization) | ||
} | ||
|
||
|
||
} | ||
|
||
//resizer ec2 instance | ||
resource "aws_instance" "photogram_resizer" { | ||
ami = "ami-0440d3b780d96b29d" # Replace with your desired AMI ID | ||
instance_type = "t2.micro" # Or any instance type you prefer | ||
iam_instance_profile = aws_iam_instance_profile.photogram_resizer_profile.name | ||
|
||
provisioner "remote-exec" { | ||
inline = [ | ||
"sudo yum install -y nodejs npm --enablerepo=epel", | ||
"sudo yum install -y ImageMagick", | ||
"sudo npm install -g forever", | ||
"mkdir resize", | ||
"aws s3 sync --region=ap-northeast-1 s3://photogram.src/resize resize", | ||
"cd resize", | ||
"npm install", | ||
"forever start -w app.js" | ||
] | ||
} | ||
} | ||
resource "aws_iam_instance_profile" "photogram_resizer_profile" { | ||
name = "photogram_resizer_profile" | ||
role = aws_iam_role.photogram_Role.name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
resource "aws_elb" "photogram_ELB" { | ||
name = "photogram-ELB" | ||
availability_zones = ["us-east-1a", "us-east-1b", "us-east-1c"] // Add appropriate availability zones | ||
listener { | ||
instance_port = 80 | ||
instance_protocol = "HTTP" | ||
lb_port = 80 | ||
lb_protocol = "HTTP" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
resource "aws_iam_role" "photogram_Role" { | ||
name = "photogram_Role" | ||
assume_role_policy = jsonencode({ | ||
"Version" : "2012-10-17", | ||
"Statement" : [ | ||
{ | ||
"Effect" : "Allow", | ||
"Principal" : { | ||
"Service" : "ec2.amazonaws.com" | ||
}, | ||
"Action" : "sts:AssumeRole" | ||
} | ||
] | ||
}) | ||
} | ||
|
||
resource "aws_iam_policy_attachment" "s3_full_access" { | ||
name = "photogram_S3_Full_Access" | ||
roles = [aws_iam_role.photogram_Role.name] | ||
policy_arn = "arn:aws:iam::aws:policy/AmazonS3FullAccess" | ||
} | ||
|
||
resource "aws_iam_policy_attachment" "sqs_full_access" { | ||
name = "photogram_SQS_Full_Access" | ||
roles = [aws_iam_role.photogram_Role.name] | ||
policy_arn = "arn:aws:iam::aws:policy/AmazonSQSFullAccess" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
terraform { | ||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
} | ||
} | ||
} | ||
|
||
# Configure the AWS Provider | ||
provider "aws" { | ||
region = "us-east-1" | ||
shared_credentials_files = ["~/.aws/credentials"] | ||
profile = "terra" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
resource "aws_db_instance" "photogram_DB" { | ||
allocated_storage = 10 | ||
db_name = "photogram" | ||
engine = "mysql" | ||
engine_version = "5.7" | ||
instance_class = "db.t3.micro" | ||
username = "admin" | ||
password = "Qwer1234**" | ||
parameter_group_name = "default.mysql5.7" | ||
vpc_security_group_ids = [aws_security_group.allow_mysql.id] | ||
skip_final_snapshot = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
var AWS = require('aws-sdk') | ||
, Sequelize = require('sequelize') | ||
, im = require('imagemagick') | ||
, mime = require('mime') | ||
, s3 = new AWS.S3({ region: 'ap-northeast-1' }) | ||
, sqs = new AWS.SQS({ region: 'ap-northeast-1' }); | ||
|
||
var s3Bucket = 'examplephoto.image'; | ||
var sqsQueueUrl = 'https://sqs.ap-northeast-1.amazonaws.com/232075047203/ExamplePhotoQueue'; | ||
var rdsEndpoint = { | ||
host: 'examplephoto.cnlconsezo7y.ap-northeast-1.rds.amazonaws.com', | ||
port: 3306 | ||
}; | ||
|
||
// MySQL DB 이름, 계정, 암호 | ||
var sequelize = new Sequelize('examplephoto', 'admin', 'adminpassword', { | ||
host: rdsEndpoint.host, | ||
port: rdsEndpoint.port | ||
}); | ||
|
||
// MySQL DB 테이블 정의 | ||
var Photo = sequelize.define('Photo', { | ||
filename: { type: Sequelize.STRING, allowNull: false, unique: true } | ||
}); | ||
|
||
// SQS 메시지 삭제 | ||
function deleteMessage(ReceiptHandle) { | ||
sqs.deleteMessage({ | ||
QueueUrl: sqsQueueUrl, | ||
ReceiptHandle: ReceiptHandle | ||
}, function (err, data) { | ||
if (err) | ||
console.log(err, err.stack); | ||
else | ||
console.log(data); | ||
}); | ||
} | ||
|
||
// MySQL에 데이터 저장 | ||
function insertPhoto(filename) { | ||
sequelize.sync().success(function () { | ||
Photo.create({ | ||
filename: filename | ||
}); | ||
}); | ||
} | ||
|
||
// SQS 메시지 받기 | ||
function receiveMessage() { | ||
sqs.receiveMessage({ | ||
QueueUrl: sqsQueueUrl, | ||
MaxNumberOfMessages: 1, | ||
VisibilityTimeout: 10, | ||
WaitTimeSeconds: 10 | ||
}, function (err, data) { | ||
if (!err && data.Messages && data.Messages.length > 0) | ||
resizeImage(data.Messages[0]); | ||
else if (err) | ||
console.log(err, err.stack); | ||
receiveMessage(); | ||
}); | ||
} | ||
|
||
// 이미지 해상도 변환 | ||
function resizeImage(Message) { | ||
var filename = Message.Body; | ||
s3.getObject({ | ||
Bucket: s3Bucket, | ||
Key: 'original/' + filename | ||
}, function (err, data) { | ||
im.resize({ | ||
srcData: data.Body, | ||
width: 800 | ||
}, function (err, stdout, stderr) { | ||
s3.putObject({ | ||
Bucket: s3Bucket, | ||
Key: 'resized/' + filename, | ||
Body: new Buffer(stdout, 'binary'), | ||
ACL: 'public-read', | ||
ContentType: mime.lookup(filename) | ||
}, function (err, data) { | ||
console.log('Complete resize ' + filename); | ||
deleteMessage(Message.ReceiptHandle); | ||
insertPhoto(filename); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
receiveMessage(); |
Oops, something went wrong.