Outil boto

boto 2 - python interface to amazon web services

boto3 - Documentation

pip install boto3

import boto3

// check in ~/.aws/credentials for aws_access_key_id and aws_secret_access_key
// check in ~/.aws/config for region

s3 = boto3.resource('s3')
s3.buckets.all() // all bucket (bucket.name, ...)

// https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html?highlight=create_instance#client
ec2 = boto3.client('ec2')
ec2.describe_instances()
instance_id = '...'
ec2.start_instances(InstanceIds=[instance_id], DryRun=True)
ec2.stop_instances(InstanceIds=[instance_id], DryRun=True)
ec2.reboot_instances(InstanceIds=[instance_id], DryRun=True)

// https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html?highlight=create_instances#EC2.ServiceResource.create_instances
// https://boto3.amazonaws.com/v1/documentation/api/latest/guide/migrationec2.html#launching-new-instances
ec2.create_instances(ImageId='ami-image-id', MinCount=1, MaxCount=5)


// security group
response = ec2.describe_security_groups(GroupIds=['SG-ID-1'])
vpc_id = ec2.describe_vpcs().get('Vpcs', [{}])[0].get('VpcId', '')
response = ec2.create_security_group(GroupName='SG-ID-1', Description='Description', VpcId=vpc_id)
security_group_id = response['GroupId']
data = ec2.authorize_security_group_ingress(
  GroupId=security_group_id,
  IpPermission=[
   {'IpProtocol': 'tcp', 'FromPort': 80, 'ToPort': 80, 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]}
   {'IpProtocol': 'tcp', 'FromPort': 22, 'ToPort': 22, 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]}
  ]
)
ec2.delete_security_group(GroupId='SG-ID-1')

CloufFormation

aws cloudformation validate-template --template-body file.json