-
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.
Merge pull request #64 from threnjen/updates_user_data_cleaner
create user data cleaner lambda
- Loading branch information
Showing
7 changed files
with
85 additions
and
16 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
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
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
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
74 changes: 74 additions & 0 deletions
74
modules/lambda_functions/bgg_user_data_cleaner_fargate_trigger.py
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,74 @@ | ||
import json | ||
import os | ||
|
||
import boto3 | ||
|
||
from config import CONFIGS | ||
|
||
ENVIRONMENT = os.environ.get("ENVIRONMENT", "dev") | ||
S3_SCRAPER_BUCKET = os.environ.get("S3_SCRAPER_BUCKET") | ||
SCRAPER_TASK_DEFINITION = CONFIGS["user_cleaner_task_definition"] | ||
TERRAFORM_STATE_BUCKET = os.environ.get("TF_VAR_BUCKET") | ||
|
||
|
||
def get_terraform_state_file_for_vpc(): | ||
"""Get the terraform state file for the VPC""" | ||
|
||
s3_client = boto3.client("s3") | ||
terraform_state_file = ( | ||
s3_client.get_object(Bucket=TERRAFORM_STATE_BUCKET, Key="vpc.tfstate")["Body"] | ||
.read() | ||
.decode("utf-8") | ||
) | ||
|
||
terraform_state_file = json.loads(terraform_state_file) | ||
|
||
print(terraform_state_file.keys()) | ||
|
||
return terraform_state_file | ||
|
||
|
||
def lambda_handler(event, context): | ||
"""Trigger the Fargate task to process the files in the S3 bucket""" | ||
|
||
print(f"Running User Data Cleaner task") | ||
|
||
terraform_state_file = get_terraform_state_file_for_vpc() | ||
|
||
task_definition = ( | ||
f"dev_{SCRAPER_TASK_DEFINITION}" | ||
if ENVIRONMENT != "prod" | ||
else SCRAPER_TASK_DEFINITION | ||
) | ||
print(task_definition) | ||
|
||
ecs_client = boto3.client("ecs") | ||
|
||
latest_version = ( | ||
ecs_client.describe_task_definition(taskDefinition=task_definition) | ||
.get("taskDefinition") | ||
.get("revision") | ||
) | ||
|
||
response = ecs_client.run_task( | ||
taskDefinition=f"{task_definition}:{latest_version}", | ||
cluster="boardgamegeek", | ||
launchType="FARGATE", | ||
count=1, | ||
platformVersion="LATEST", | ||
enableECSManagedTags=False, | ||
networkConfiguration={ | ||
"awsvpcConfiguration": { | ||
"subnets": terraform_state_file["outputs"]["public_subnets"]["value"], | ||
"securityGroups": [ | ||
terraform_state_file["outputs"]["sg_ec2_ssh_access"]["value"] | ||
], | ||
"assignPublicIp": "ENABLED", | ||
}, | ||
}, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
lambda_handler(None, None) |