-
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.
update terraform script and use ec2 in pipelines
- Loading branch information
Tan 🔥
committed
Jan 6, 2023
1 parent
71583dc
commit e293fbe
Showing
3 changed files
with
61 additions
and
13 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,5 @@ | ||
#!/bin/sh | ||
# go to the spider directory | ||
cd /opt/project/project | ||
# run the spider | ||
scrapy crawl product |
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
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 |
---|---|---|
|
@@ -13,6 +13,7 @@ provider "aws" { | |
resource "aws_instance" "project_ec2" { | ||
ami = "ami-0b93ce03dcbcb10f6" | ||
instance_type = "t2.micro" | ||
iam_instance_profile = aws_iam_instance_profile.scrapy_profile.name | ||
vpc_security_group_ids = ["${aws_security_group.project_sg.id}"] | ||
key_name = "terraform_ec2_key" | ||
|
||
|
@@ -23,19 +24,19 @@ resource "aws_instance" "project_ec2" { | |
apt-get update | ||
apt-get install -y python3-pip git curl | ||
# Replace this with our script and requirements.txt | ||
git clone https://github.com/TanZng/ot6-cloud.git | ||
pip3 install -r requirements.txt | ||
# Create directory | ||
mkdir -p /opt/project | ||
# Copy main script | ||
mkdir -p /opt/app | ||
cp final_project_scraper.py /opt/app/ | ||
# Clone scraper and install requirements.txt | ||
git clone [email protected]:TanZng/ot6-cloud.git /opt/project | ||
cd /opt/project | ||
pip3 install -r requirements.txt | ||
# Create a cron job to run the main.py script every minute | ||
# TODO: change how often run the script | ||
# !!! Create a cron job to run the main.py script every minute | ||
# !!! TODO: change how often run the script | ||
sudo service cron start | ||
echo "* * * * * /usr/bin/python3 /opt/app/final_project_scraper.py >> /scrapy.log" | crontab | ||
echo "* * * * * /opt/project/crawl.sh >> > $HOME/project-`date +\%Y\%m\%d\%H\%M\%S`-cron.log 2>&1" | crontab | ||
EOF | ||
|
||
|
@@ -82,8 +83,6 @@ resource "aws_s3_bucket" "project_s3" { | |
bucket_prefix = "project-s3" | ||
} | ||
|
||
resource "random_uuid" "uuid" {} | ||
|
||
# Create DynamoDB table | ||
resource "aws_dynamodb_table" "project-dynamodb" { | ||
name = "project-dynamodb" | ||
|
@@ -96,4 +95,43 @@ resource "aws_dynamodb_table" "project-dynamodb" { | |
name = "ProductId" | ||
type = "S" | ||
} | ||
} | ||
|
||
# Create an instance profile | ||
resource "aws_iam_instance_profile" "scrapy_profile" { | ||
name = "scrapy_profile" | ||
role = aws_iam_role.ec2_role.name | ||
} | ||
|
||
# Create a role that can be assumed by an EC2 instance | ||
resource "aws_iam_role" "ec2_role" { | ||
name = "ec2_role" | ||
|
||
assume_role_policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Principal": { | ||
"Service": "ec2.amazonaws.com" | ||
}, | ||
"Effect": "Allow", | ||
"Sid": "" | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
|
||
# Attach policies to the role | ||
resource "aws_iam_role_policy_attachment" "role-policy-attachment" { | ||
for_each = toset([ | ||
"arn:aws:iam::aws:policy/AmazonEC2FullAccess", | ||
"arn:aws:iam::aws:policy/AmazonS3FullAccess", | ||
"arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess" | ||
]) | ||
|
||
role = aws_iam_role.ec2_role.name | ||
policy_arn = each.value | ||
} |