Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lessons learned updates #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
group ID for scaling
  • Loading branch information
jpb-Cloudy-McCloudFace committed Aug 31, 2023
commit c2b91648339f3f041af468bff4f5717f842bd775
60 changes: 60 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
terraform.tf Bump default subnets high in CIDR range as it can confilct with default VPC ranges.
lambda to SQS Single SQS FIFO group ID prevents ECS scaling as only 10 messages can be pulled at a time per group ID preventing parrallel processing of the queue. In the case of needing to upload
thousands of S3 objects, scalling using parrallel uploads to Akamai over exact order is typically prefered (exact order is not guarenteed from S3 delivery to Lambda anyway). A FIFO
SQS queue will help with exactly once delivery, as required when uploading large files (why do it twice?) and using a group ID to the EPOCH second will alow for multiple ECS tasks
to run in parrallel processing the files, 10 at a time, that arrived in that second.
lambnda logs Logs were not going to CloudWatch logs
12 changes: 4 additions & 8 deletions lambda_function.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json,boto3,os,logging
import json,boto3,os,logging,time
from botocore.exceptions import ClientError

logger = logging.getLogger("AKAM:S3-NS-SYNC")
Expand All @@ -12,6 +12,7 @@ def configure_logging():
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
logger.addHandler(ch)

def addToQueue(record):
"""
Description: add processed event to SQS Queue for processing
Expand All @@ -25,19 +26,17 @@ def addToQueue(record):

try:
response = sqs.send_message(
QueueUrl= os.environ['queueUrl'],
QueueUrl=os.environ['queueUrl'],
DelaySeconds=0,
MessageBody=(json.dumps(record)),
MessageGroupId='S3-NS-SYNC'

MessageGroupId=str(int(time.time()))
)
logger.info ("Record added to queue: {0}".format(record))
except Exception as e:
logger.error ("Error adding record '{0}' to queue: {1}".format(record,e))
return False
return True


def lambda_handler(event, context):
configure_logging()
statusCode = 200
Expand All @@ -64,10 +63,7 @@ def lambda_handler(event, context):
'body': 'Error, {0}/{1} added to queue!'.format(len(record_lst),len(event['Records']))
}



return {
'statusCode': statusCode,
'body': 'Success, {0}/{1} Added to queue!'.format(len(record_lst),len(event['Records']))

}
Binary file modified lambda_function.zip
Binary file not shown.
9 changes: 7 additions & 2 deletions terraform.tf
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ resource "aws_iam_role_policy_attachment" "sqs-full-role-policy-attach" {
policy_arn = "arn:aws:iam::aws:policy/AmazonSQSFullAccess"
}

resource "aws_iam_role_policy_attachment" "lambda-execution-policy-attach" {
role = aws_iam_role.iam_lambda_sqs.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}

resource "aws_iam_role" "iam_lambda_ecs_task" {
path = "/service-role/"
name = "AkamaiNetStorageSync_ecs_task"
Expand Down Expand Up @@ -224,7 +229,7 @@ resource "aws_security_group" "EC2SecurityGroup" {

resource "aws_subnet" "EC2Subnet" {
availability_zone = "${var.region}b"
cidr_block = "172.31.30.0/24"
cidr_block = "172.31.100.0/24"
vpc_id = aws_default_vpc.default.id
map_public_ip_on_launch = false
tags = {
Expand All @@ -234,7 +239,7 @@ resource "aws_subnet" "EC2Subnet" {

resource "aws_subnet" "EC2Subnet2" {
availability_zone = "${var.region}a"
cidr_block = "172.31.50.0/24"
cidr_block = "172.31.101.0/24"
vpc_id = aws_default_vpc.default.id
map_public_ip_on_launch = false
tags = {
Expand Down