EC2 (Elastic Compute Cloud)
EC2 is a web service that provides resizable compute capacity in the cloud. Here is an example of how to launch an EC2 instance using the AWS SDK for Python (boto3):
python
import boto3
ec2 = boto3.resource('ec2')
image_id = 'ami-0c55b159cbfafe1f0'
instance_type = 't2.micro'
key_name = 'my-key-pair'
security_group_ids = ['sg-0123456789abcdef']
subnet_id = 'subnet-0123456789abcdef'
instance = ec2.create_instances(
ImageId=image_id,
InstanceType=instance_type,
KeyName=key_name,
SecurityGroupIds=security_group_ids,
SubnetId=subnet_id,
MinCount=1,
MaxCount=1
)[0]
print(f"EC2 instance {instance.instance_id} launched")
This code launches an EC2 instance with the specified AMI ID, instance type, key pair name, security group IDs, and subnet ID.
No comments:
Post a Comment