https://pulumi.com logo
#general
Title
# general
r

red-football-97286

01/31/2020, 11:55 AM
Wanted to get some feed back on some code I wrote to loop through different conditions on creating an alarm for route53. Could I have done this in a better way?
Copy code
environments = {
    'HealthChecks': [health_check_1.id, health_check_2.id],
    'Names': ['Primary', 'Secondary']
}

# Metric alarm for both primary and secondary sites
for index in range(0, (len(environments))):
    cloudwatch.MetricAlarm(
        f"{environments['Names'][index]}_site_alarm",
        __opts__=ResourceOptions(
            depends_on=[sns_topic, sns_email]),
        alarm_actions=[sns_topic.arn],
        alarm_description=f"{environments['Names'][index]} alarm for website",
        name=f"{environments['Names'][index]}_site",
        comparison_operator="LessThanThreshold",
        statistic="Minimum", evaluation_periods=3,
        metric_name="HealthCheckStatus",
        namespace="AWS/Route53",
        period=60, threshold=1,
        dimensions={
            'HealthCheckId': environments['HealthChecks'][index]
        }
    )
w

white-balloon-205

01/31/2020, 3:02 PM
Looks reasonable to me! You could probably simplify a little by “transposing” your environments variable - make it an array of objects instead of an object of arrays - then doing
for env in environments
.
m

mysterious-egg-7415

02/01/2020, 4:04 PM
You shouldnt use
__opts__
but instead just
opts