def create_buckets(bk_in, bk_out): s3 = boto3.resource('s3') buckets = [] buckets.append(bk_in) buckets.append(bk_out) for bucket in buckets: response = s3.create_bucket(Bucket=bucket) myutils.myprint('INFO', 'Creating new S3 buckets', response) #print (json.dumps(response, indent=4, sort_keys=True, default=str)) # Turn bucket_in to a website website_payload = { 'ErrorDocument': { 'Key': 'error.html' }, 'IndexDocument': { 'Suffix': 'index.html' } } bucket_website = s3.BucketWebsite(bk_in) # And configure the static website with our desired index.html # and error.html configuration. bucket_website.put(WebsiteConfiguration=website_payload) # Create an S3 client s3 = boto3.client('s3') for bucket in buckets: # Create the bucket policy bucket_policy = { 'Version': '2012-10-17', 'Statement': [{ 'Sid': 'AddPerm', 'Effect': 'Allow', 'Principal': '*', 'Action': ['s3:GetObject'], "Condition": { "IpAddress": { "aws:SourceIp": ip_string } }, 'Resource': "arn:aws:s3:::%s/*" % bucket }] } # Convert the policy to a JSON string bucket_policy = json.dumps(bucket_policy) # Set the new policy on the given bucket response = s3.put_bucket_policy(Bucket=bucket, Policy=bucket_policy) myutils.myprint('INFO', 'Updating bucket policies', response)
role_arn = role_name['Role']['Arn'] # topics = sns.list_topics() # for topic in topics["Topics"]: # if 'new_posts' in str(topic['TopicArn']): # topic_arn = topic['TopicArn'] # print(topic_arn) # open the payload function which is in the zip file myutils.zipit('./convertoaudio.py', './payload2.zip') with open('./payload2.zip', 'rb') as f: zipped_code = f.read() response = lambda_client.create_function( FunctionName=function_name, Runtime='python2.7', Description="This function converts the text from s3 to polly mp3", Role=role_arn, MemorySize=128, Handler='convertoaudio.lambda_handler', Code=dict(ZipFile=zipped_code), Timeout=300, # Maximum allowable timeout Environment={ 'Variables': { "BUCKET_NAME": bucket_out, "DB_TABLE_NAME": table_name } }, ) fname = f'Creating Lambda function {function_name}' myutils.myprint('INFO', fname, response) #print (json.dumps(response, indent=4, sort_keys=True, default=str))
#!/usr/bin/python3 import boto3, json import sys import os sys.path.append('../../lib') import myutils myapi_name = os.environ.get('API_NAME', 'ServerLessPollyAPI') ids = myutils.get_api_ids(myapi_name) api_id = ids['api_id'] root_id = ids['root_id'] resource_id = ids['resource_id'] apigw = boto3.client('apigateway') lam = boto3.client('lambda') response = apigw.get_integration(restApiId=api_id, resourceId=resource_id, httpMethod='OPTIONS') #print (json.dumps(response, indent=4, sort_keys=True, default=str)) myutils.myprint('INFO', 'get_integration output for OPTIONS', response)
#!/usr/bin/python3 import boto3 import json import os import sys sys.path.append('../../lib') import myutils ''' Delete the top level restApi ''' myapi_name = os.environ.get('API_NAME', 'ServerLessPollyAPI') #myapi_name = 'PostReaderAPI' apigw = boto3.client('apigateway') ids = myutils.get_api_ids(myapi_name) api_id = ids['api_id'] response = apigw.delete_rest_api(restApiId=api_id) myutils.myprint('INFO', 'Deleting REST API', response) #print (json.dumps(response, indent=4, sort_keys=True, default=str))
import myutils myapi_name = os.environ.get('API_NAME', 'ServerLessPollyAPI') stage_name = os.environ.get('STAGE_NAME', 'development') apigw = boto3.client('apigateway') ids = myutils.get_api_ids(myapi_name) api_id = ids['api_id'] root_id = ids['root_id'] resource_id = ids['resource_id'] print('api id = ' + api_id + ' root id = ' + root_id + ' resource id = ' + resource_id) # delete the stage first response = apigw.delete_stage(restApiId=api_id, stageName=stage_name) myutils.myprint('INFO', 'Deleting stage', response) # now delete the deployments deployments = apigw.get_deployments(restApiId=api_id) for item in deployments['items']: if stage_name in item['description']: logmsg = "Will delete this deployment id = " + item[ 'id'] + " description = " + item['description'] print(logmsg) response = apigw.delete_deployment(restApiId=api_id, deploymentId=item['id']) myutils.myprint('INFO', 'Deleting deployments', response) print(json.dumps(response, indent=4, sort_keys=True, default=str))
sys.path.append('./lib') import myutils table_name= os.environ.get('TABLE_NAME', 'PollyProject') region=os.environ.get('REGION', 'us-east-1') session = boto3.session.Session(region_name=region) dynamodb = session.resource('dynamodb') table = dynamodb.Table(table_name) new_table = dynamodb.create_table( KeySchema=[ { 'AttributeName' : 'id', 'KeyType' : 'HASH', }, ], TableName=table_name, ProvisionedThroughput={ 'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5 }, AttributeDefinitions= [ { "AttributeName": "id", "AttributeType": "S" } ] ) # Wait until the table exists. table.meta.client.get_waiter('table_exists').wait(TableName=table_name) myutils.myprint ('INFO', 'Creating new dynamodb Table', new_table)
# GET Method query string query_field='postId' query_string='method.request.querystring.' + query_field #Create the GET method in the Resource response = apigw.put_method( restApiId=api_id, resourceId=root_id, httpMethod='GET', authorizationType='NONE', requestParameters={ query_string: False } ) myutils.myprint ('INFO', 'Creating GET Method for REST API', response) #print (json.dumps(response, indent=4, sort_keys=True, default=str)) # method integration # first, get the function arn res = lam.get_function(FunctionName=function_name) function_arn=res['Configuration']['FunctionArn'] uri_string = 'arn:aws:apigateway:' + region + ':lambda:path/2015-03-31/functions/' + function_arn + '/invocations' template="{\r\n \"postId\" : \"$input.params('postId')\"\r\n}" #print ("uri = " + uri_string) response = apigw.put_integration( restApiId=api_id, resourceId=resource_id,
import json import sys sys.path.append('../lib') import myutils table_name = os.environ.get('TABLE_NAME', 'PollyProject') region = os.environ.get('REGION', 'us-east-1') topic_name = os.environ.get('TOPIC_NAME', 'new_polly_events') role_name = os.environ.get('LAMBDA_ROLE', 'sk-lambda-role') policy_name = os.environ.get('LAMBDA_POLICY', 'sk-lambda-policy') # Delete dynamodb table session = boto3.session.Session(region_name=region) dynamodb = session.resource('dynamodb') table = dynamodb.Table(table_name) response = table.delete() myutils.myprint('INFO', 'Deleting dynamodb Table', table_name) # delete s3 buckets s3 = boto3.resource('s3') bk_in = os.environ.get('BUCKET_IN', 'sk-pollybucket-in') bk_out = os.environ.get('BUCKET_OUT', 'sk-pollybucket-out') buckets = [] buckets.append(bk_in) buckets.append(bk_out) for bucket in buckets: s3_bucket = s3.Bucket(bucket) response = s3_bucket.objects.all().delete() myutils.myprint('INFO', 'Deleting bucket objects', response) #print (json.dumps(response, indent=4, sort_keys=True, default=str)) response = s3_bucket.delete() myutils.myprint('INFO', 'Deleting buckets', response)
#!/usr/bin/python3 import boto3 import json import os import sys sys.path.append('../lib') import myutils ''' Create the top level restApi ''' myapi_name = os.environ.get('API_NAME', 'ServerLessPollyAPI') #myapi_name = 'PostReaderAPI' apigw = boto3.client('apigateway') # create the top level API name and ID response = apigw.create_rest_api(name=myapi_name, description="My Lambda Polly API", endpointConfiguration={'types': ['REGIONAL']}) myutils.myprint('INFO', 'Creating new REST API ', response) #print (json.dumps(response, indent=4, sort_keys=True, default=str))
''' myapi_name = os.environ.get('API_NAME', 'ServerLessPollyAPI') region = os.environ.get('REGION', 'us-east-1') #---------------------------DO NOT CHANGE ANYTHING BELOW ------------- lam = boto3.client('lambda') ids = myutils.get_api_ids(myapi_name) api_id = ids['api_id'] account_id = myutils.get_account_id() # lambda functionst that is granting invoke perm to api gateway function_names = [ 'PostReader_GetPosts', 'PostReader_NewPosts', 'PostReader_ConvertToAudio' ] uri = 'arn:aws:execute-api:' + region + ':' + account_id + ':' + api_id + '/*' for fn in function_names: # Give the api deployment permission to trigger the lambda function response = lam.add_permission( FunctionName=fn, StatementId='apigateway-lambda-invoke-perms-' + fn, Action='lambda:InvokeFunction', Principal='apigateway.amazonaws.com', SourceArn=uri) myutils.myprint('INFO', 'Adding Lambda invoke permission for API Gateway', response) #print (json.dumps(response, indent=4, sort_keys=True, default=str))
#!/usr/bin/python3 import boto3 import json import os import sys sys.path.append('./lib') import myutils ''' step 3 create a SNS topic to publish to ''' topic_name = os.environ.get('TOPIC_NAME', 'new_polly_events') sns = boto3.client('sns') response = sns.create_topic(Name=topic_name, Attributes={'DisplayName': 'New Posts'}) myutils.myprint('INFO', 'Creating new SNS topic', response) #print (json.dumps(response, indent=4, sort_keys=True, default=str))
#---------------------------DO NOT CHANGE ANYTHING BELOW ------------- apigw = boto3.client('apigateway') lam = boto3.client('lambda') ids = myutils.get_api_ids(myapi_name) api_id = ids['api_id'] root_id = ids['root_id'] resource_id = ids['resource_id'] # Create an OPTIONS method response = apigw.put_method(restApiId=api_id, resourceId=resource_id, httpMethod='OPTIONS', authorizationType='NONE') myutils.myprint('INFO', 'Creating OPTIONS Method for CORS ', response) #print (json.dumps(response, indent=4, sort_keys=True, default=str)) # # add method response response = apigw.put_method_response( restApiId=api_id, resourceId=resource_id, httpMethod='OPTIONS', statusCode='200', responseParameters={ 'method.response.header.Access-Control-Allow-Headers': False, 'method.response.header.Access-Control-Allow-Origin': False, 'method.response.header.Access-Control-Allow-Methods': False }, responseModels={'application/json': 'Empty'}) myutils.myprint('INFO', 'Creating OPTIONS Method response for CORS ', response) #print (json.dumps(response, indent=4, sort_keys=True, default=str))
api_id = ids['api_id'] root_id = ids['root_id'] resource_id = ids['resource_id'] # GET Method query string # needed to search as http://xxx/?postId=2837481748 query_field = 'postId' query_string = 'method.request.querystring.' + query_field # Create the GET method in the Resource response = apigw.put_method(restApiId=api_id, resourceId=root_id, httpMethod='POST', authorizationType='NONE', requestParameters={}) myutils.myprint('INFO', 'Creating POST method for API ', response) #print (json.dumps(response, indent=4, sort_keys=True, default=str)) # method_response response = apigw.put_method_response( restApiId=api_id, resourceId=resource_id, httpMethod='POST', statusCode='200', responseParameters={ 'method.response.header.Access-Control-Allow-Origin': False }, responseModels={'application/json': 'Empty'}) myutils.myprint('INFO', 'Creating method response for POST method ', response) #print (json.dumps(response, indent=4, sort_keys=True, default=str))
#!/usr/bin/python3 import boto3, json import os import sys sys.path.append('../../lib') import myutils ''' Remove Lambda invocation perms from api gateway ''' myapi_name = os.environ.get('API_NAME', 'ServerLessPollyAPI') lam = boto3.client('lambda') # lambda functionst that is granting invoke perm to api gateway function_names = [ 'PostReader_NewPosts', 'PostReader_GetPosts', 'PostReader_ConvertToAudio' ] for fn in function_names: # Give the api deployment permission to trigger the lambda function response = lam.remove_permission( FunctionName=fn, StatementId='apigateway-lambda-invoke-perms-' + fn) myutils.myprint('INFO', 'Deleting LAMBDA invoke permissions', response) print(json.dumps(response, indent=4, sort_keys=True, default=str))