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

victorious-helicopter-18904

03/29/2024, 11:33 AM
Hi All, While trying to create an AWS Autoscaling with a Load balancer I am getting an error. Below is my code.
Copy code
import pulumi
import pulumi_aws as aws

# Create a new VPC
vpc = aws.ec2.Vpc("my-vpc", cidr_block="10.0.0.0/16")

# Create a new security group
security_group = aws.ec2.SecurityGroup("my-security-group",
    vpc_id=vpc.id,
    ingress=[{
        "protocol": "tcp",
        "from_port": 80,
        "to_port": 80,
        "cidr_blocks": ["0.0.0.0/0"]
    }]
)

# Create a classic load balancer
load_balancer = aws.elb.LoadBalancer("my-load-balancer",
    availability_zones=["us-west-2a", "us-west-2b", "us-west-2c"],
    listeners=[{
        "instance_port": 80,
        "instance_protocol": "HTTP",
        "lb_port": 80,
        "lb_protocol": "HTTP"
    }],
    security_groups=[security_group.id]
)

# Create an auto-scaling group
auto_scaling_group = aws.autoscaling.Group("my-auto-scaling-group",
    min_size=2,
    max_size=5,
    launch_configuration=aws.ec2.LaunchConfiguration("my-launch-configuration",
        image_id="ami-0c101f26f147fa7fd",
        instance_type="t2.micro",
        security_groups=[security_group.name]
    ),
    availability_zones=["us-east-1a", "us-east-1b", "us-east-1c"],
    load_balancers=[load_balancer.name]
)


# Attach the load balancer to the auto-scaling group
attachment = aws.autoscaling.Attachment("my-attachment",
    autoscaling_group_name=auto_scaling_group.name,
    load_balancers=[load_balancer.name]
)

# Create a CloudWatch metric alarm for scaling up
scale_up_alarm = aws.cloudwatch.MetricAlarm("scale-up-alarm",
    alarm_actions=[auto_scaling_group.arn],
    comparison_operator="GreaterThanOrEqualToThreshold",
    evaluation_periods=1,
    metric_name="CPUUtilization",
    namespace="AWS/EC2",
    period=60,
    statistic="Average",
    threshold=80,
    dimensions={
        "AutoScalingGroupName": auto_scaling_group.name
    }
)

# Create a CloudWatch metric alarm for scaling down
scale_down_alarm = aws.cloudwatch.MetricAlarm("scale-down-alarm",
    alarm_actions=[auto_scaling_group.arn],
    comparison_operator="LessThanOrEqualToThreshold",
    evaluation_periods=1,
    metric_name="CPUUtilization",
    namespace="AWS/EC2",
    period=60,
    statistic="Average",
    threshold=30,
    dimensions={
        "AutoScalingGroupName": auto_scaling_group.name
    }
)

# Export the load balancer URL
pulumi.export("load_balancer_url", load_balancer.dns_name)
And the error I am getting,
Copy code
Diagnostics:
  pulumi:pulumi:Stack (pulumi-demo-dev):
    error: Program failed with an unhandled exception:
    Traceback (most recent call last):
      File "/Users/apple/Documents/Documents - Apple's MacBook Pro/pulumi-demo/./__main__.py", line 45, in <module>
        attachment = aws.autoscaling.Attachment("my-attachment",
      File "/Users/apple/Documents/Documents - Apple's MacBook Pro/pulumi-demo/venv/lib/python3.9/site-packages/pulumi_aws/autoscaling/attachment.py", line 218, in __init__
        __self__._internal_init(resource_name, *args, **kwargs)
    TypeError: _internal_init() got an unexpected keyword argument 'load_balancers'
I tried to get help from chatGPT but that didn't work.
m

mysterious-lamp-54193

03/29/2024, 11:35 AM
It's clear that
Copy code
# Attach the load balancer to the auto-scaling group
attachment = aws.autoscaling.Attachment("my-attachment",
    autoscaling_group_name=auto_scaling_group.name,
    load_balancers=[load_balancer.name]
)
doesn't take
load_balancers
as an argument to it
v

victorious-helicopter-18904

03/30/2024, 4:07 AM
@mysterious-lamp-54193 Now I am getting another error.
Copy code
Diagnostics:
  aws:cloudwatch:MetricAlarm (scale-up-alarm):
    error: 1 error occurred:
        * creating CloudWatch Metric Alarm (scale-up-alarm-c3a2fe3): operation error CloudWatch: PutMetricAlarm, https response error StatusCode: 400, RequestID: 3492e52a-b4bc-40df-81bf-fed1eeb162f9, api error ValidationError: The vendor "autoscaling" cannot be used with relative id autoScalingGroup:eb8875c0-e8d0-41c3-9e54-78db17e8419f:autoScalingGroupName/my-auto-scaling-group-34e8bb1. Allowed relative IDs must be in the formatscalingPolicy:policy-id:autoScalingGroupName/group-friendly-name:policyName/policy-friendly-name

  aws:cloudwatch:MetricAlarm (scale-down-alarm):
    error: 1 error occurred:
        * creating CloudWatch Metric Alarm (scale-down-alarm-ef2c488): operation error CloudWatch: PutMetricAlarm, https response error StatusCode: 400, RequestID: a4b6114c-af26-4bca-b1a2-b4909054bfca, api error ValidationError: The vendor "autoscaling" cannot be used with relative id autoScalingGroup:eb8875c0-e8d0-41c3-9e54-78db17e8419f:autoScalingGroupName/my-auto-scaling-group-34e8bb1. Allowed relative IDs must be in the formatscalingPolicy:policy-id:autoScalingGroupName/group-friendly-name:policyName/policy-friendly-name
3 Views