├── requirements.txt ├── LICENSE ├── scripts ├── ec2_stop.py ├── ec2_start.py ├── ec2_ip_route53.py └── ec2_start_and_route53.py └── README.md /requirements.txt: -------------------------------------------------------------------------------- 1 | boto3>=1.7.28 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Dmitri Fedotov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /scripts/ec2_stop.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | ''' 3 | This script stops all instances with a specific tag. 4 | ''' 5 | 6 | import boto3 7 | 8 | def instances_find(name, value): 9 | ''' 10 | Finds instance id's based on tags. 11 | Returns a list of instances found. 12 | ''' 13 | list_instances = [] 14 | # filter based on tags 15 | filters =[ 16 | { 17 | 'Name': name, 18 | 'Values': [ 19 | value, 20 | ] 21 | }, 22 | ] 23 | instances = ec2_resource.instances.filter(Filters=filters) 24 | for instance in instances: 25 | # for each instance, append to list 26 | list_instances.append(instance.id) 27 | return list_instances 28 | 29 | def instances_stop(list): 30 | ''' 31 | Stops instances defined in the list. 32 | ''' 33 | ec2_client.stop_instances(InstanceIds=list) 34 | 35 | # enter tag name and value 36 | tag_name = 'tag:environment' 37 | tag_value = 'dev' 38 | 39 | ec2_resource = boto3.resource('ec2') 40 | ec2_client = boto3.client('ec2') 41 | 42 | # find instances 43 | ec2_list = instances_find(tag_name, tag_value) 44 | # stop instances 45 | ec2_stop = instances_stop(ec2_list) 46 | print('stopped instances: ' + str(ec2_list)) 47 | -------------------------------------------------------------------------------- /scripts/ec2_start.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | ''' 3 | This script starts all instances with a specific tag. 4 | ''' 5 | 6 | import boto3 7 | 8 | def instances_find(name, value): 9 | ''' 10 | Finds instance id's based on tags. 11 | Returns a list of instances found. 12 | ''' 13 | list_instances = [] 14 | # filter based on tags 15 | filters =[ 16 | { 17 | 'Name': name, 18 | 'Values': [ 19 | value, 20 | ] 21 | }, 22 | ] 23 | instances = ec2_resource.instances.filter(Filters=filters) 24 | for instance in instances: 25 | # for each instance, append to list 26 | list_instances.append(instance.id) 27 | return list_instances 28 | 29 | def instances_start(list): 30 | ''' 31 | Starts instances defined in the list. 32 | ''' 33 | ec2_client.start_instances(InstanceIds=list) 34 | 35 | # enter tag name and value 36 | tag_name = 'tag:environment' 37 | tag_value = 'dev' 38 | 39 | ec2_resource = boto3.resource('ec2') 40 | ec2_client = boto3.client('ec2') 41 | 42 | # find instances 43 | ec2_list = instances_find(tag_name, tag_value) 44 | # start instances 45 | ec2_stop = instances_start(ec2_list) 46 | print('started instances: ' + str(ec2_list)) 47 | -------------------------------------------------------------------------------- /scripts/ec2_ip_route53.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | ''' 3 | This script finds an instance by its ID. 4 | Then, it finds out its public IP address and changes Route53 'A' record. 5 | ''' 6 | 7 | import boto3 8 | 9 | def find_ip(id): 10 | ''' 11 | Finds a public IP address based on instance ID. 12 | Returns a public IP address. 13 | ''' 14 | ec2 = boto3.resource('ec2') 15 | instance = ec2.Instance(id) 16 | ip = instance.public_ip_address 17 | return ip 18 | 19 | def change_route53_record(zone_id, domain, ip): 20 | ''' 21 | Changes Route53 type A record. 22 | ''' 23 | r53 = boto3.client('route53') 24 | r53.change_resource_record_sets( 25 | HostedZoneId=zone_id, 26 | ChangeBatch={ 27 | 'Comment': 'test', 28 | 'Changes': [ 29 | { 30 | 'Action': 'UPSERT', 31 | 'ResourceRecordSet': { 32 | 'Name': domain, 33 | 'ResourceRecords': [ 34 | { 35 | 'Value': ip 36 | } 37 | ], 38 | 'Type': 'A', 39 | 'TTL': 300 40 | } 41 | }, 42 | ] 43 | } 44 | ) 45 | 46 | # set instance ID 47 | instance_id = '' # Instance ID, e.g. 'i-0111112233' 48 | # set Hosted Zone ID 49 | zone_id = '' # Hosted Zone ID, e.g. 'ZBDAAABBBCCC' 50 | # domain 51 | domain = '' # Domain, e.g. technoff.eu 52 | 53 | # find the public IP address 54 | ip_address = find_ip(instance_id) 55 | # change 'A' record 56 | change_a_record = change_route53_record(zone_id, domain, ip_address) 57 | print(domain + ' record was changed to: ' + ip_address) 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # aws-boto3-scripts 2 | Python scripts for AWS using boto3 SDK 3 | 4 | | Script | Description | 5 | | ---------------------------| ----------------------------------------------------| 6 | | [ec2_start.py](scripts/ec2_start.py) | Start all instances with a specific tag | 7 | | [ec2_stop.py](scripts/ec2_stop.py) | Stop all instances with a specific tag | 8 | | [ec2_ip_route53.py](scripts/ec2_ip_route53.py) | Get EC2 public IP and change Route53 'A' record | 9 | | [ec2_start_and_route53.py](scripts/ec2_start_and_route53.py) | Start EC2, get public IP and change Route53 'A' record | 10 | 11 | ## Installation 12 | 13 | Requires Python and [boto3](https://github.com/boto/boto3). 14 | 15 | ``` 16 | git clone https://github.com/dmitrijsf/aws-boto3-scripts 17 | cd aws-boto3-scripts 18 | pip install -r requirements.txt 19 | ``` 20 | ## Usage 21 | 22 | **Set desired AWS credentials** 23 | 24 | In this example I am using [**aws-vault**](https://github.com/99designs/aws-vault) to work with desired profile. 25 | 26 | ``` 27 | ❯ aws-vault add home 28 | Enter Access Key ID: your-aws-access-key-id 29 | Enter Secret Access Key: your-aws-access-key 30 | Added credentials to profile "home" in vault 31 | 32 | # launches subshell with desired AWS environment variables 33 | ❯ aws-vault exec -- home 34 | ``` 35 | **Launching Scripts** 36 | 37 | ``` 38 | ❯ cd scripts 39 | # modify the script according to your needs 40 | # ec2_ip_route53.py 41 | 42 | # set instance ID 43 | instance_id = 'i-0111112233' # Instance ID, e.g. 'i-0111112233' 44 | # set Hosted Zone ID 45 | zone_id = 'ZBDAAABBBCCC' # Hosted Zone ID, e.g. 'ZBDAAABBBCCC' 46 | # domain 47 | domain = 'technoff.eu' # Domain, e.g. technoff.eu 48 | 49 | ❯ ./ec2_ip_route53.py 50 | technoff.eu record was changed to: 52.51.120.202 51 | ``` -------------------------------------------------------------------------------- /scripts/ec2_start_and_route53.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | ''' 3 | This script finds an instance by its ID and starts it. 4 | Then, it finds out its public IP address and changes Route53 'A' record. 5 | ''' 6 | 7 | import boto3 8 | 9 | def instance_start(id): 10 | ''' 11 | Starts an EC2 instance and waits until it's state is running. 12 | ''' 13 | ec2 = boto3.resource('ec2') 14 | instance = ec2.Instance(id) 15 | # start instance 16 | instance.start() 17 | print('Starting instance ' + id) 18 | # wait until 'running' state 19 | instance.wait_until_running() 20 | 21 | def find_ip(id): 22 | ''' 23 | Finds a public IP address based on instance ID. 24 | Returns a public IP address. 25 | ''' 26 | ec2 = boto3.resource('ec2') 27 | instance = ec2.Instance(id) 28 | ip = instance.public_ip_address 29 | return ip 30 | 31 | def change_route53_record(zone_id, domain, ip): 32 | ''' 33 | Changes Route53 type A record. 34 | ''' 35 | r53 = boto3.client('route53') 36 | r53.change_resource_record_sets( 37 | HostedZoneId=zone_id, 38 | ChangeBatch={ 39 | 'Comment': 'test', 40 | 'Changes': [ 41 | { 42 | 'Action': 'UPSERT', 43 | 'ResourceRecordSet': { 44 | 'Name': domain, 45 | 'ResourceRecords': [ 46 | { 47 | 'Value': ip 48 | } 49 | ], 50 | 'Type': 'A', 51 | 'TTL': 300 52 | } 53 | }, 54 | ] 55 | } 56 | ) 57 | 58 | # set instance ID 59 | instance_id = '' # Instance ID, e.g. 'i-0111112233' 60 | # set Hosted Zone ID 61 | zone_id = '' # Hosted Zone ID, e.g. 'ZBDAAABBBCCC' 62 | # domain 63 | domain = '' # Domain, e.g. technoff.eu 64 | 65 | # start instance 66 | instance_start(instance_id) 67 | # find the public IP address 68 | ip_address = find_ip(instance_id) 69 | # change 'A' record 70 | change_a_record = change_route53_record(zone_id, domain, ip_address) 71 | print(domain + ' record was changed to: ' + ip_address) 72 | --------------------------------------------------------------------------------