Skip to content

Commit

Permalink
Refactor and clean up Pulumi stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ru Chern Chong committed May 23, 2024
1 parent 99a1ca1 commit 54f3b37
Showing 1 changed file with 64 additions and 102 deletions.
166 changes: 64 additions & 102 deletions __main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@
import pulumi
import pulumi_aws as aws

project_name = f"{pulumi.get_stack()}-lta-datasets-updater"
MEMORY_SIZE = 1024
TIMEOUT = 15
RUNTIME = aws.lambda_.Runtime.PYTHON3D12
TIMEZONE = "Asia/Singapore"

PROJECT_NAME = f"{pulumi.get_stack()}-{pulumi.get_project()}"

config = pulumi.Config()
MONGODB_URI = config.require_secret("MONGODB_URI")
MONGODB_DB_NAME = config.require("MONGODB_DB_NAME")

role = aws.iam.Role(
f"{project_name}-role",
f"{PROJECT_NAME}-role",
assume_role_policy=json.dumps(
{
"Version": "2012-10-17",
Expand All @@ -22,30 +31,36 @@
)

aws.iam.RolePolicyAttachment(
f"{project_name}-policy",
f"{PROJECT_NAME}-policy",
role=role.name,
policy_arn=aws.iam.ManagedPolicy.AWS_LAMBDA_BASIC_EXECUTION_ROLE,
)

# lta_datasets_updater_bucket = aws.s3.Bucket(f"{project_name}-bucket")

# packages = aws.s3.BucketObjectv2(
# f"{project_name}-packages",
# bucket=lta_datasets_updater_bucket.id,
# source=pulumi.FileAsset("package.zip"),
# )

packages_layer = aws.lambda_.LayerVersion(
f"{project_name}-layer",
layer_name=f"{project_name}-layer",
package_layer = aws.lambda_.LayerVersion(
f"{PROJECT_NAME}-layer",
layer_name=f"{PROJECT_NAME}-layer",
code=pulumi.FileArchive("package.zip"),
# s3_bucket=lta_datasets_updater_bucket.id,
# s3_key=packages.key,
compatible_runtimes=[aws.lambda_.Runtime.PYTHON3D12],
compatible_runtimes=[RUNTIME],
)

update_cars_function = aws.lambda_.Function(
f"{project_name}-cars-function",

def create_lambda_function(name, handler, code):
return aws.lambda_.Function(f"{PROJECT_NAME}-{name}-function",
code=code,
role=role.arn,
environment=aws.lambda_.FunctionEnvironmentArgs(
variables={
"TZ": "Asia/Singapore",
"MONGODB_URI": pulumi.Config().require_secret("MONGODB_URI"),
"MONGODB_DB_NAME": pulumi.Config().require("MONGODB_DB_NAME"),
}
), handler=handler, runtime=RUNTIME, memory_size=MEMORY_SIZE, timeout=TIMEOUT,
layers=[package_layer.arn])


update_cars_function = create_lambda_function(
"cars",
"update_cars.handler",
code=pulumi.AssetArchive(
{
"utils": pulumi.FileArchive("utils"),
Expand All @@ -54,23 +69,12 @@
"updater.py": pulumi.FileAsset("updater.py"),
}
),
role=role.arn,
environment=aws.lambda_.FunctionEnvironmentArgs(
variables={
"TZ": "Asia/Singapore",
"MONGODB_URI": pulumi.Config().require_secret("MONGODB_URI"),
"MONGODB_DB_NAME": pulumi.Config().require("MONGODB_DB_NAME"),
}
),
handler="update_cars.handler",
runtime=aws.lambda_.Runtime.PYTHON3D12,
memory_size=1024,
timeout=15,
layers=[packages_layer.arn],

)

update_coe_function = aws.lambda_.Function(
f"{project_name}-coe-function",
update_coe_function = create_lambda_function(
"coe",
"update_coe.handler",
code=pulumi.AssetArchive(
{
"utils": pulumi.FileArchive("utils"),
Expand All @@ -79,81 +83,39 @@
"updater.py": pulumi.FileAsset("updater.py"),
}
),
role=role.arn,
environment=aws.lambda_.FunctionEnvironmentArgs(
variables={
"TZ": "Asia/Singapore",
"MONGODB_URI": pulumi.Config().require_secret("MONGODB_URI"),
"MONGODB_DB_NAME": pulumi.Config().require("MONGODB_DB_NAME"),
}
),
handler="update_coe.handler",
runtime=aws.lambda_.Runtime.PYTHON3D12,
memory_size=1024,
timeout=15,
layers=[packages_layer.arn],
)

update_cars_rule = aws.cloudwatch.EventRule(
f"{project_name}-cars-rule",
schedule_expression="cron(0/60 0-10 ? * MON-FRI *)",
)

update_coe_rule = aws.cloudwatch.EventRule(
f"{project_name}-coe-rule",
schedule_expression="cron(0/60 0-10 ? * MON-FRI *)",
)

update_coe_first_bidding_rule = aws.cloudwatch.EventRule(
f"{project_name}-coe-first-bidding-rule",
schedule_expression="cron(0/10 8-10 ? * 4#1 *)",
)
def create_event_rule(name, scheduled_expression, target_lambda_function):
rule = aws.cloudwatch.EventRule(f"{PROJECT_NAME}-{name}-rule", schedule_expression=scheduled_expression)

update_coe_second_bidding_rule = aws.cloudwatch.EventRule(
f"{project_name}-coe-second-bidding-rule",
schedule_expression="cron(0/10 8-10 ? * 4#3 *)",
)
aws.cloudwatch.EventTarget(
f"{PROJECT_NAME}-{name}-target",
rule=rule.name,
arn=target_lambda_function.arn,
)

aws.lambda_.Permission(
f"{project_name}-permission",
action="lambda:InvokeFunction",
function=update_cars_function.name,
principal="events.amazonaws.com",
source_arn=update_cars_rule.arn,
)
aws.lambda_.Permission(
f"{PROJECT_NAME}-{name}-permission",
action="lambda:InvokeFunction",
function=target_lambda_function.name,
principal="events.amazonaws.com",
source_arn=rule.arn,
)

aws.lambda_.Permission(
f"{project_name}-coe-permission",
action="lambda:InvokeFunction",
function=update_coe_function.name,
principal="events.amazonaws.com",
source_arn=update_coe_rule.arn,
)
return rule

update_cars_target = aws.cloudwatch.EventTarget(
f"{project_name}-cars-target",
rule=update_cars_rule.name,
arn=update_cars_function.arn,
)

pulumi.export("update_cars_function", update_cars_function.name)
pulumi.export("update_cars_rule", update_cars_rule.schedule_expression)
update_coe_target = aws.cloudwatch.EventTarget(
f"{project_name}-coe-target",
rule=update_coe_rule.name,
arn=update_coe_function.arn,
)

update_coe_first_bidding_target = aws.cloudwatch.EventTarget(
f"{project_name}-coe-first-bidding-target",
rule=update_coe_first_bidding_rule.name,
arn=update_coe_function.arn,
)
cron_schedulers = {
"cars": {"cron": "cron(0/60 0-10 ? * MON-FRI *)", "function": update_cars_function},
"coe": {"cron": "cron(0/60 0-10 ? * MON-FRI *)", "function": update_coe_function},
"coe-first-bidding": {"cron": "cron(0/10 8-10 ? * 4#1 *)", "function": update_coe_function},
"coe-second-bidding": {"cron": "cron(0/10 8-10 ? * 4#3 *)", "function": update_coe_function},
}

update_coe_second_bidding_target = aws.cloudwatch.EventTarget(
f"{project_name}-coe-second-bidding-target",
rule=update_coe_second_bidding_rule.name,
arn=update_coe_function.arn,
)
for name, scheduler in cron_schedulers.items():
create_event_rule(name, scheduler['cron'], scheduler['function'])

pulumi.export("packages_layer", packages_layer.source_code_size)
pulumi.export("update_cars_function", update_cars_function.name)
pulumi.export("update_coe_function", update_coe_function.name)
pulumi.export("packages_layer", package_layer.source_code_size)

0 comments on commit 54f3b37

Please sign in to comment.