以下是一个使用AWS自动伸缩和竞价实例的代码示例:
import boto3
# 创建Auto Scaling client
autoscaling_client = boto3.client('autoscaling')
# 创建自动伸缩组
response = autoscaling_client.create_auto_scaling_group(
AutoScalingGroupName='my-auto-scaling-group',
LaunchConfigurationName='my-launch-configuration',
MinSize=2,
MaxSize=10,
DesiredCapacity=2,
AvailabilityZones=['us-west-2a'],
DefaultCooldown=300,
HealthCheckType='EC2',
HealthCheckGracePeriod=300
)
import boto3
# 创建EC2 client
ec2_client = boto3.client('ec2')
# 创建启动配置
response = ec2_client.create_launch_configuration(
LaunchConfigurationName='my-launch-configuration',
ImageId='ami-12345678',
InstanceType='t2.micro',
KeyName='my-key-pair',
SecurityGroups=['my-security-group'],
UserData='#!/bin/bash\n echo "Hello, World!" > /tmp/hello.txt'
)
import boto3
# 创建EC2 client
ec2_client = boto3.client('ec2')
# 创建竞价实例请求
response = ec2_client.request_spot_instances(
SpotPrice='0.01',
InstanceCount=1,
LaunchSpecification={
'ImageId': 'ami-12345678',
'InstanceType': 't2.micro',
'KeyName': 'my-key-pair',
'SecurityGroups': ['my-security-group'],
'UserData': '#!/bin/bash\n echo "Hello, World!" > /tmp/hello.txt'
}
)
以上代码示例展示了如何使用AWS SDK创建自动伸缩组、启动配置和竞价实例请求。你可以根据自己的需求进行修改和扩展。请确保你已经安装了AWS SDK并配置了正确的访问密钥和区域。