├── LICENSE ├── README.md ├── attach_ebsvolume.py ├── create_alb.py ├── create_ami.py ├── create_dns_Arecord.py ├── create_efsvolume.py ├── create_lambdafunction.py ├── create_route53_alias_record.py ├── create_s3_bucket.py ├── create_vpc.py ├── list_ec2info.py ├── modify_s3bucket_aclperms.py ├── new_dynamodb_table.py ├── new_iam_user.py ├── new_rdsmysql.py ├── new_redis_cluster.py ├── start_stop_terminate_ec2instance.py ├── update_route53_record.py └── upload_s3file.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Mike 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AWS_Solutions_Architect_Python 2 | Taking the AWS Solutions Architect certification path is a great way to test your knowledge and skills in cloud computing/cloud engineering. The one thing we don't see a lot of in the certification path is utilizing the API to automate your tasks. 3 | 4 | This project is to solve just that. As we go through the AWS Solutions Architect material, each of the sections have a lab. Each lab that is done manually, I will automate with Python (or as close as I can get). 5 | 6 | Please Note: This is NOT on the exam. This is simply a way to show you what is going on from a code perspective. 7 | -------------------------------------------------------------------------------- /attach_ebsvolume.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import sys 3 | import logging 4 | import time 5 | 6 | def attach_ebsvolume(instanceID): 7 | 8 | try: 9 | new_ebs = boto3.client('ec2') 10 | 11 | create = new_ebs.create_volume( 12 | AvailabilityZone=input('Please enter availability zone: '), 13 | Size=int(input('Pleae enter size: ')), 14 | VolumeType='gp2' 15 | ) 16 | 17 | print('Pausing while the EBS volume creates') 18 | time.sleep(10) 19 | 20 | response = new_ebs.attach_volume( 21 | Device='/dev/sdf', 22 | InstanceId=instanceID, 23 | VolumeId=str(create['VolumeId']), 24 | ) 25 | print(response) 26 | 27 | except Exception as e: 28 | logging.debug('Checking resource') 29 | logging.info('an issue with the EC2 volume occured occured') 30 | logging.warning('EBS volume not created or EC2 instance does not exist') 31 | print(e) 32 | 33 | instanceID = sys.argv[1] 34 | attach_ebsvolume(instanceID) -------------------------------------------------------------------------------- /create_alb.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import sys 3 | import logging 4 | import time 5 | 6 | def create_alb(lbName, Scheme, LBtype): 7 | 8 | subnet1 = input('Please enter first subnet: ') 9 | subnet2 = input('Please enter second subnet: ') 10 | securityGroup = input('Please enter security group: ') 11 | 12 | lb = boto3.client('elbv2') 13 | new_lb = lb.create_load_balancer( 14 | Name=lbName, 15 | Subnets=[subnet1, subnet2], 16 | SecurityGroups=[securityGroup], 17 | Scheme=Scheme, 18 | Type=LBtype, 19 | IpAddressType='ipv4' 20 | 21 | ) 22 | print(new_lb) 23 | 24 | lbName = sys.argv[1] 25 | Scheme = sys.argv[2] 26 | LBtype = sys.argv[3] 27 | 28 | if __name__ == '__main__': 29 | create_alb(lbName, Scheme, LBtype) 30 | -------------------------------------------------------------------------------- /create_ami.py: -------------------------------------------------------------------------------- 1 | import boto3 as amicreation 2 | import logging 3 | import sys 4 | import time 5 | 6 | def create_ami(imageDescription, instanceID, imageName): 7 | 8 | try: 9 | ami = amicreation.client('ec2') 10 | ami.create_image( 11 | Description = imageDescription, 12 | InstanceId=instanceID, 13 | Name=imageName, 14 | NoReboot=True 15 | ) 16 | except: 17 | logging.debug('Checking instance ID and IAM credentials') 18 | logging.info('A valid instance Id or IAM credentials were not used') 19 | logging.warning('PLEASE USE A VALID INSTANCE ID AND IAM CREDENTIALS') 20 | 21 | imageDescription = sys.argv[1] 22 | instanceID = sys.argv[2] 23 | imageName = sys.argv[3] 24 | 25 | if __name__ == '__main__': 26 | print('Creating AMI: ' + imageName) 27 | create_ami(imageDescription, instanceID, imageName) 28 | 29 | else: 30 | print('running as imported module') 31 | time.sleep(5) 32 | create_ami(imageDescription, instanceID, imageName) -------------------------------------------------------------------------------- /create_dns_Arecord.py: -------------------------------------------------------------------------------- 1 | import boto3 as route53 2 | import sys 3 | import logging 4 | import time 5 | 6 | global dnsEntry 7 | 8 | def create_dns_Arecord(hostedZone, name, IPValueAddress): 9 | dnsEntry = route53.client('route53') 10 | print('Please use a hosted zone id for your hosted zone parameter') 11 | print('Please use an FQDN for your DNS host name. For example, foobar.example.com.') 12 | time.sleep(5) 13 | 14 | try: 15 | newEntry = dnsEntry.change_resource_record_sets( 16 | HostedZoneId = hostedZone, 17 | ChangeBatch={ 18 | 'Comment': 'Creating: {}'.format(name), 19 | 'Changes': [ 20 | { 21 | 'Action': 'CREATE', 22 | 'ResourceRecordSet': { 23 | 'Name': name, 24 | 'Type': 'A', 25 | 'TTL': 300, 26 | 'ResourceRecords': [ 27 | { 28 | 'Value': IPValueAddress 29 | } 30 | 31 | ] 32 | } 33 | } 34 | ] 35 | } 36 | 37 | ) 38 | 39 | except Exception as e: 40 | logging.warning('WARNING: An error has occured') 41 | print(e) 42 | 43 | hostedZone = sys.argv[1] 44 | name = sys.argv[2] 45 | IPValueAddress = sys.argv[3] 46 | 47 | if __name__ == '__main__': 48 | create_dns_Arecord(hostedZone, name, IPValueAddress) -------------------------------------------------------------------------------- /create_efsvolume.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import sys 3 | import os 4 | import random 5 | import string 6 | 7 | def generate_token(size=15, chars=string.ascii_uppercase + string.ascii_lowercase + string.digits): 8 | rando = ''.join(random.choice(chars) for _ in range(size)) 9 | return rando 10 | 11 | def check_ec2instance(): 12 | print('Lets check if the EC2 instance exists that you are looking to mount') 13 | describe = input('Please enter instance id: ') 14 | 15 | check_ec2 = boto3.client('ec2') 16 | resource = check_ec2.describe_instances(InstanceIds=[describe]) 17 | 18 | print(resource) 19 | 20 | def create_filesystem(): 21 | print('Creating file system\n') 22 | create_efs = boto3.client('efs') 23 | 24 | create_efs = create_efs.create_file_system( 25 | CreationToken=generate_token(), 26 | PerformanceMode='generalPurpose' 27 | ) 28 | 29 | print(create_efs) 30 | 31 | if __name__ == '__main__': 32 | check_ec2instance() 33 | create_filesystem() 34 | -------------------------------------------------------------------------------- /create_lambdafunction.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import sys 3 | import time 4 | 5 | # This Lambda function was designed to pull some code from a zip file location in a place of your choice. For my test, 6 | # I used the S3 script below: 7 | # import boto3 8 | # def get_s3buckets(): 9 | # s3Buckets = boto3.client('s3') 10 | # resource = s3Buckets.list_buckets() 11 | # for b in resource['Buckets']: 12 | # print(b['Name']) 13 | 14 | # You can, of course, use this to create any Lambda function you would like though. Enjoy! 15 | 16 | 17 | def create_lambdafunction(LambdafunctionName, iamRole, PythonfunctionName): 18 | filePath = input( 19 | 'Please enter the location of the zip file where your Python code is for your Lambda function: ') 20 | lambdaCreation = boto3.client('lambda') 21 | resource = lambdaCreation.list_functions() 22 | for f in resource['Functions']: 23 | if LambdafunctionName in f['FunctionName']: 24 | print('Lambda function already exists') 25 | print('Closing in 5 seconds') 26 | time.sleep(5) 27 | exit() 28 | 29 | else: 30 | newLambda = lambdaCreation.create_function( 31 | FunctionName=LambdafunctionName, 32 | Runtime='python3.7', 33 | # The role is the IAM role you created that has access to kick off the Lambda Function. You need to put in the ARN 34 | Role=iamRole, 35 | Handler='{}.lambda_handler'.format(PythonfunctionName), 36 | # The code below is some sample code. This will pull all of your S3 bucket names 37 | Code={'ZipFile': open(filePath, 'rb').read(), }, 38 | Description='Print out all S3 bucket names' 39 | ) 40 | 41 | print(newLambda) 42 | 43 | 44 | LambdafunctionName = sys.argv[1] 45 | iamRole = sys.argv[2] 46 | PythonfunctionName = sys.argv[3] 47 | 48 | if __name__ == '__main__': 49 | create_lambdafunction(LambdafunctionName, iamRole, PythonfunctionName) 50 | -------------------------------------------------------------------------------- /create_route53_alias_record.py: -------------------------------------------------------------------------------- 1 | import boto3 as route53 2 | import sys 3 | import logging 4 | import time 5 | 6 | global dnsEntry 7 | 8 | def create_route53_alias_record(hostedZone, name, aliasValue): 9 | dnsEntry = route53.client('route53') 10 | print('Please use a hosted zone id for your hosted zone parameter') 11 | print('Please use an FQDN for your DNS host name. For example, foobar.example.com.') 12 | time.sleep(5) 13 | 14 | try: 15 | newEntry = dnsEntry.change_resource_record_sets( 16 | HostedZoneId=hostedZone, 17 | ChangeBatch={ 18 | 'Comment': 'Creating: {}'.format(name), 19 | 'Changes': [ 20 | { 21 | 'Action': 'CREATE', 22 | 'ResourceRecordSet': { 23 | 'Name': name, 24 | 'Type': 'CNAME', 25 | 'TTL': 300, 26 | 27 | 'ResourceRecords': [ 28 | { 29 | 'Value': aliasValue 30 | } 31 | ], 32 | } 33 | 34 | } 35 | ] 36 | } 37 | 38 | ) 39 | print(newEntry) 40 | 41 | except Exception as e: 42 | logging.warning('WARNING: An error has occured') 43 | print(e) 44 | 45 | 46 | hostedZone = sys.argv[1] 47 | name = sys.argv[2] 48 | aliasValue = sys.argv[3] 49 | 50 | if __name__ == '__main__': 51 | create_route53_alias_record(hostedZone, name, aliasValue) 52 | -------------------------------------------------------------------------------- /create_s3_bucket.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import sys 3 | 4 | def create_bucket(region, accessKey, secretKey, bucketName): 5 | 6 | try: 7 | # Connect to the s3 API 8 | s3_bucket = boto3.client('s3', 9 | region_name = region, 10 | aws_access_key_id = accessKey, 11 | aws_secret_access_key = secretKey) 12 | 13 | # Create a new bucket with the ACL as private and your specified bucket name 14 | new_bucket = s3_bucket.create_bucket( 15 | Bucket=bucketName, 16 | ACL = 'private', 17 | ) 18 | 19 | except Exception as e: 20 | print(e) 21 | 22 | region = sys.argv[1] 23 | accessKey = sys.argv[2] 24 | secretKey = sys.argv[3] 25 | bucketName = sys.argv[4] 26 | 27 | if __name__ == '__main__': 28 | create_bucket(region, accessKey, secretKey, bucketName) -------------------------------------------------------------------------------- /create_vpc.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import sys 3 | # To use this script supply cidr block, tenancy, name for vpc, and whether or not the subnet is public or private 4 | # ex: create_vpc.py 10.100.100.0/16 default myName public 5 | 6 | ec2 = boto3.resource('ec2') 7 | 8 | def create_VPC(cidr, tenancy, name, privPub): 9 | #Create vpc 10 | vpc = ec2.create_vpc( 11 | CidrBlock=cidr, 12 | AmazonProvidedIpv6CidrBlock=False, 13 | InstanceTenancy=tenancy 14 | ) 15 | vpc.create_tags( 16 | Tags=[{ 17 | "Key": "Name", 18 | "Value": name 19 | }]) 20 | 21 | #Create subnet 22 | subnet = ec2.create_subnet( 23 | CidrBlock='10.100.1.0/24', 24 | VpcId=vpc.id 25 | ) 26 | subnet.create_tags( 27 | Tags=[{ 28 | "Key": "Name", 29 | "Value": name 30 | }]) 31 | 32 | #Create internet gateway 33 | igw = ec2.create_internet_gateway() 34 | igw.attach_to_vpc( 35 | VpcId=vpc.id 36 | ) 37 | igw.create_tags( 38 | Tags=[{ 39 | "Key": "Name", 40 | "Value": name 41 | }]) 42 | 43 | #Create route-table and route to internet if VPC is public 44 | if privPub != 'public': 45 | pass 46 | else: 47 | rt_table = vpc.create_route_table() 48 | route = rt_table.create_route( 49 | DestinationCidrBlock = '0.0.0.0/0', 50 | GatewayId = igw.id 51 | ) 52 | 53 | return subnet 54 | return vpc 55 | return igw 56 | 57 | cidr = sys.argv[1] 58 | tenancy = sys.argv[2] 59 | name = sys.argv[3] 60 | privPub = sys.argv[4] 61 | 62 | create_VPC(cidr, tenancy, name, privPub) 63 | print('Your VPC is ready for usage') 64 | -------------------------------------------------------------------------------- /list_ec2info.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import logging 3 | import sys 4 | 5 | def get_ec2_info(error_log_path): 6 | 7 | try: 8 | ec2 = boto3.resource('ec2') 9 | print_instances = ec2.instances.all() 10 | 11 | print('Below are stats for each EC2 instance\n') 12 | for ec2instance in print_instances: 13 | print( 14 | "ID: {}\nType: {} \nPublic IPv4: {}\nAMI: {}\nState: {}\nTags: {}\nSecurityGroups: {}\n".format( 15 | ec2instance.id, ec2instance.instance_type, ec2instance.public_ip_address, ec2instance.image.id, ec2instance.state, ec2instance.tags, ec2instance.security_groups 16 | ) 17 | ) 18 | except Exception as e: 19 | logging.basicConfig(filename='{}'.format(error_log_path), level=logging.DEBUG) 20 | logging.debug('Checking resource') 21 | logging.info('an issue with the EC2 resource occured') 22 | logging.warning(e) 23 | print(e) 24 | 25 | error_log_path = sys.argv[1] 26 | 27 | if __name__ == '__main__': 28 | get_ec2_info(error_log_path) -------------------------------------------------------------------------------- /modify_s3bucket_aclperms.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import sys 3 | 4 | def change_s3bucket_aclperms(region, accessKey, secretKey, bucketName, ACLPerms): 5 | # List which ACL options are available 6 | print('Please choose from the following ACL Perms: \t\nprivate \t\npublic-read \t\npublic-read-write \t\nauthenticated-read') 7 | 8 | try: 9 | # If statement that accepts only the following strings 10 | if 'private' or 'public-read' or 'public-read-write' or 'authenticated-read' in bucketName: 11 | # Connect to your specified bucket 12 | s3bucket_perms = boto3.resource('s3', 13 | region = region, 14 | aws_access_key_id= accessKey, 15 | aws_secret_access_key = secretKey) 16 | 17 | bucket_perms = s3bucket_perms.BucketAcl(bucketName) 18 | 19 | # Call the ACL perms that you specified and attach them to your bucket 20 | new_perms = bucket_perms.put( 21 | ACL = ACLPerms 22 | ) 23 | else: 24 | print('Invalid choice. Please choose from the list above.') 25 | 26 | except Exception as e: 27 | print(e) 28 | 29 | region = sys.argv[1] 30 | accessKey = sys.argv[2] 31 | secretKey = sys.argv[3] 32 | bucketName = sys.argv[4] 33 | ACLPerms = sys.argv[5] 34 | 35 | if __name__ == '__main__': 36 | change_s3bucket_aclperms(region, accessKey, secretKey, bucketName, ACLPerms) 37 | -------------------------------------------------------------------------------- /new_dynamodb_table.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import sys 3 | import os 4 | import time 5 | 6 | global tableName 7 | 8 | def new_dynamodb_table(tableName): 9 | 10 | new_table = boto3.client('dynamodb') 11 | get_existing_table = boto3.client('dynamodb') 12 | new_item = boto3.client('dynamodb') 13 | 14 | try: 15 | def get_tables(tableName): 16 | table = get_existing_table.describe_table( 17 | TableName = sys.argv[1] 18 | ) 19 | pass 20 | get_tables(tableName) 21 | except: 22 | print('The table does not exist. Moving on') 23 | 24 | new_table.create_table( 25 | AttributeDefinitions=[ 26 | { 27 | 'AttributeName': sys.argv[1], 28 | 'AttributeType': 'S' 29 | }, 30 | ], 31 | TableName = sys.argv[1], 32 | KeySchema=[ 33 | { 34 | 'AttributeName': sys.argv[1], 35 | 'KeyType': 'HASH', 36 | } 37 | ], 38 | ProvisionedThroughput= { 39 | 'ReadCapacityUnits': 10, 40 | 'WriteCapacityUnits': 10, 41 | } 42 | ) 43 | 44 | print('Creating Table. May take up to 20 seconds') 45 | time.sleep(20) 46 | print('Creation: Complete') 47 | 48 | newItem = input('Would you like to create a new item?: ') 49 | if 'Y' in newItem or 'Yes' in newItem: 50 | value = input('Please enter a new item: ') 51 | new_table_item = new_item.put_item( 52 | TableName = sys.argv[1], 53 | Item={ 54 | sys.argv[1]: { 55 | 'S': value 56 | } 57 | } 58 | ) 59 | 60 | elif 'N' in newItem or 'No' in newItem: 61 | print('No new item. Exiting') 62 | 63 | tableName = sys.argv[1] 64 | 65 | if __name__ == '__main__': 66 | new_dynamodb_table(tableName) -------------------------------------------------------------------------------- /new_iam_user.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import sys 3 | import os 4 | 5 | def new_iam_user(accessKey, secretKey, username): 6 | # Create connection to AWS API 7 | iam_user = boto3.client('iam', 8 | aws_access_key_id=accessKey, 9 | aws_secret_access_key=secretKey) 10 | 11 | try: 12 | # Try to create a new user 13 | new_user_create = iam_user.create_user( 14 | UserName=username 15 | ) 16 | print(new_user_create) 17 | 18 | except: 19 | # Always remember the naming convention needed by IAM and to ensure your accesskey/secretkey is correct 20 | print( 21 | 'No new user was created, please confirm your region, accesskey, and secretkey were correct and try again') 22 | exit() 23 | 24 | try: 25 | # For the user that you just created, this will give them programmatic access for AWS CLI 26 | new_user_access = iam_user.create_access_key( 27 | UserName=username 28 | ) 29 | 30 | print(new_user_access) 31 | 32 | except: 33 | print( 34 | 'Users access key was not created. Please confirm you have permissions to utilize programmatic access in AWS and try again') 35 | 36 | accessKey = sys.argv[1] 37 | secretKey = sys.argv[2] 38 | username = sys.argv[3] 39 | 40 | if __name__ == '__main__': 41 | new_iam_user(accessKey, secretKey, username) 42 | else: 43 | print('please run this directly from the script or take out the __name__ == __main__ if statement if you would prefer to not run this script as main and import as a module') -------------------------------------------------------------------------------- /new_rdsmysql.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import sys 3 | import time 4 | import logging 5 | import getpass 6 | 7 | def new_rdsmysql(dbname, instanceID, storage, dbInstancetype, dbusername): 8 | 9 | masterPass = getpass.getpass('DBMasterPassword: ') 10 | if len(masterPass) < 10: 11 | logging.warning('Password is not at least 10 characters. Please try again') 12 | time.sleep(5) 13 | exit 14 | else: 15 | None 16 | 17 | try: 18 | rds_instance = boto3.client('rds') 19 | create_instance = rds_instance.create_db_instance( 20 | DBName = dbname, 21 | DBInstanceIdentifier = instanceID, 22 | AllocatedStorage = int(storage), 23 | DBInstanceClass = dbInstancetype, 24 | Engine = 'mysql', 25 | MasterUsername = dbusername, 26 | MasterUserPassword = str(masterPass), 27 | MultiAZ = True, 28 | EngineVersion = '5.7.23', 29 | AutoMinorVersionUpgrade = False, 30 | LicenseModel = 'general-public-license', 31 | PubliclyAccessible = False, 32 | 33 | Tags = [ 34 | { 35 | 'Key': 'Name', 36 | 'Value' : dbname 37 | } 38 | ] 39 | ) 40 | 41 | print(create_instance) 42 | except Exception as e: 43 | logging.warning('An error has occured') 44 | print(e) 45 | 46 | dbname = sys.argv[1] 47 | instanceID = sys.argv[2] 48 | storage = sys.argv[3] 49 | dbInstancetype = sys.argv[4] 50 | dbusername = sys.argv[5] 51 | 52 | new_rdsmysql(dbname, instanceID, storage, dbInstancetype, dbusername) -------------------------------------------------------------------------------- /new_redis_cluster.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import sys 3 | 4 | def new_redis_cluster(clusterID, instanceSize, cacheName): 5 | new_cluster = boto3.client('elasticache') 6 | new_cluster.create_cache_cluster( 7 | CacheClusterId = clusterID, 8 | AZMode='single-az', 9 | NumCacheNodes = 1, 10 | CacheNodeType = instanceSize, 11 | Engine='redis', 12 | Tags = [ 13 | { 14 | 'Key': 'Name', 15 | 'Value': cacheName, 16 | }, 17 | ] 18 | ) 19 | print(cacheName + ' cluster: Created') 20 | 21 | clusterID = sys.argv[1] 22 | instanceSize = sys.argv[2] 23 | cacheName = sys.argv[3] 24 | 25 | if __name__ == '__main__': 26 | new_redis_cluster(clusterID, instanceSize, cacheName) -------------------------------------------------------------------------------- /start_stop_terminate_ec2instance.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import sys 3 | import logging 4 | 5 | global action 6 | 7 | def start_stop_terminate_ec2instance(region, accessKey, secretKey, instanceID): 8 | 9 | # This input will allow you to pick your action of what you want to do with your EC2 instance 10 | action = input('Please type: \none for start \ntwo for stop \nthree for terminate \n') 11 | 12 | # Connection to the EC2 API 13 | ec2 = boto3.client('ec2', 14 | region_name = region, 15 | aws_access_key_id = accessKey, 16 | aws_secret_access_key = secretKey) 17 | 18 | # First if statement is to start the instance 19 | if 'one' in action: 20 | ec2.start_instances(InstanceIds=[instanceID]) 21 | print('starting: ' + instanceID) 22 | 23 | # First elif is to stop the instance 24 | elif 'two' in action: 25 | ec2.stop_instances(InstanceIds=[instanceID]) 26 | print('stopping: ' + instanceID) 27 | 28 | # Second elif is to terminate the instance 29 | elif 'three' in action: 30 | ec2.terminate_instances(InstanceIds=[instanceID]) 31 | print('terminating: ' + instanceID) 32 | 33 | # Else is for if start, stop, or terminate was not selected 34 | else: 35 | logging.debug('Checking argument') 36 | logging.info('stop, start or restart were not used') 37 | logging.warning('Please choose \n1: Start\n2: Stop \n3: Terminate') 38 | 39 | 40 | region = sys.argv[1] 41 | accessKey = sys.argv[2] 42 | secretKey = sys.argv[3] 43 | instanceID = sys.argv[4] 44 | 45 | start_stop_terminate_ec2instance(region, accessKey, secretKey, instanceID) -------------------------------------------------------------------------------- /update_route53_record.py: -------------------------------------------------------------------------------- 1 | import boto3 as route53 2 | import sys 3 | import logging 4 | import time 5 | 6 | global dnsEntry 7 | 8 | def update_route53_record(hostedZone, name, type, value): 9 | dnsEntry = route53.client('route53') 10 | print('Please use a hosted zone id for your hosted zone parameter') 11 | print('Please use an FQDN for your DNS host name. For example, foobar.example.com.') 12 | print('For type, please use one of the following: \nSOA \nA \nTXT \nNS \nCNAME \nMX \nNAPTR \nPTR \nSRV \nSPF') 13 | time.sleep(5) 14 | 15 | try: 16 | newEntry = dnsEntry.change_resource_record_sets( 17 | HostedZoneId = hostedZone, 18 | ChangeBatch={ 19 | 'Comment': 'Updating: {}'.format(name), 20 | 'Changes': [ 21 | { 22 | 'Action': 'UPSERT', 23 | 'ResourceRecordSet': { 24 | 'Name': name, 25 | 'Type': type, 26 | 'TTL': 300, 27 | 'ResourceRecords': [ 28 | { 29 | 'Value': value 30 | } 31 | 32 | ] 33 | } 34 | } 35 | ] 36 | } 37 | 38 | ) 39 | 40 | except Exception as e: 41 | logging.warning('WARNING: An error has occured') 42 | print(e) 43 | 44 | hostedZone = sys.argv[1] 45 | name = sys.argv[2] 46 | type = sys.argv[3] 47 | value = sys.argv[4] 48 | 49 | if __name__ == '__main__': 50 | update_route53_record(hostedZone, name, type, value) -------------------------------------------------------------------------------- /upload_s3file.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import sys 3 | import os 4 | 5 | def upload_s3file(accessKey, secretKey, bucketName, filepath, filename): 6 | # Confirm path specified exists 7 | check_path = os.path.exists(filepath) 8 | 9 | try: 10 | # if statement that if the path doesn't exist, prompt for a new one. If it does exist, continue with the upload 11 | if check_path == False: 12 | confirm_path = input("Path does not exist. Please enter a proper filepath: ") 13 | 14 | s3_upload = boto3.resource('s3', 15 | aws_access_key_id=accessKey, 16 | aws_secret_access_key=secretKey) 17 | 18 | s3_upload.meta.client.upload_file(confirm_path, bucketName, filename) 19 | 20 | elif check_path == True: 21 | # filepath is where the file currently exists on your machine/server. Filename is what you want it to be called when it is uploaded 22 | s3_upload = boto3.resource('s3', 23 | aws_access_key_id=accessKey, 24 | aws_secret_access_key=secretKey) 25 | 26 | s3_upload.meta.client.upload_file(filepath, bucketName, filename) 27 | 28 | except Exception as e: 29 | print(e) 30 | 31 | accessKey = sys.argv[1] 32 | secretKey = sys.argv[2] 33 | bucketName = sys.argv[3] 34 | filepath = sys.argv[4] 35 | filename = sys.argv[5] 36 | 37 | if __name__ == '__main__': 38 | upload_s3file(accessKey, secretKey, bucketName, filepath, filename) 39 | --------------------------------------------------------------------------------