def add_topic_and_lambda(self, topic_name, lambda_meta_data): """Adds a SNS topic and SAM Function to the CloudFormation template :param topic_name: (str) the SNS topic name :param lambda_meta_data: (LambdaMetaData) an object specifying info about the lambda to create :return: a sns.Topic object """ topic = sns.Topic( "{}Topic".format(topic_name), TopicName=topic_name, ) self.add_resource(topic) self.add_resource( serverless.Function( "{}Lambda".format(lambda_meta_data.name), Description=lambda_meta_data.description, MemorySize=128, FunctionName=lambda_meta_data.name, Runtime=LAMBDA_RUNTIME, Handler='index.handler', CodeUri='lambda-src/', Timeout=10, Environment=awslambda.Environment( Variables={ 'WEBHOOK_URL': lambda_meta_data.webhook_url, 'MESSAGE_PREFIX': lambda_meta_data.message_prefix, }), Events={ 'SNS': serverless.SNSEvent('sns', Topic=Ref(topic)), }, )) return topic
def cf_resources(): from troposphere import serverless f = serverless.Function( function_name, FunctionName=function_name, Handler=handler_str, Runtime='python3.7', MemorySize=128, Timeout=60, CodeUri='./source/', Policies=[{ 'DynamoDBCrudPolicy': { 'TableName': MachineModel.get_table_name() } }, { 'LambdaInvokePolicy': { 'FunctionName': '*' } }], Events={ event_name: serverless.ApiEvent(event_name, Path=func_args.get('Path'), Method=func_args.get('Method', 'post')) }) return [f]
def cf_resources(**resource_args): """CloudFormation resources""" from troposphere import serverless f = serverless.Function(function_name, FunctionName=function_name, Handler=handler_str, Runtime='python3.7', MemorySize=128, Timeout=60, CodeUri='./source/', Events={ event_name: serverless.ApiEvent( event_name, Path=func_args.get('Path'), Method='post') }) return [f]
] }, Path="/", ManagedPolicyArns=[ "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole", "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole", "arn:aws:iam::aws:policy/AmazonS3FullAccess", ] )) custom_resource_empty_datalake_bucket_on_delete = template.add_resource( serverless.Function( "CustomResourceEmptyDatalakeBucketOnDelete", CodeUri="./clean_bucket", Description="Custom Resource Empty DatalakeBucket OnDelete", FunctionName="empty-example-s3-bucket-stepfunctions-glue-troposphere", Handler="clean_bucket.lambda_handler", Role=custom_resource_empty_on_delete_execution_role.get_att('Arn'), Runtime="python3.8", Timeout=900 )) class CleanupBucket(AWSCustomObject): resource_type = "Custom::cleanupbucket" props = {'ServiceToken': (str, True)} cleanup_datalake_bucket_on_delete = template.add_resource(CleanupBucket( "CleanupDatalakeBucketOnDelete", ServiceToken=custom_resource_empty_datalake_bucket_on_delete.get_att('Arn'), BucketName=internal_s3_bucket.ref(),
"MAGIC_PATH": magic_path, "CONFIG_BUCKET": Ref(config_bucket), }), 'Role': GetAtt(lambda_role, 'Arn'), } template.add_resource( serverless.Function( "Index", **common_lambda_options, Handler='index.handler', Events={ 'Index': serverless.ApiEvent( 'unused', Path='/', Method='GET', ), }, )) template.add_resource( serverless.Function( "DomainList", **common_lambda_options, Handler='domain_list.handler', Events={ 'DomainList': serverless.ApiEvent( 'unused',
def createSQSConsumer(name, timeout=5, snsTopic=None): res = {} # create queue res['QueueName'] = name + 'Queue' queue = t.add_resource( sqs.Queue(res['QueueName'], VisibilityTimeout=timeout * 2)) queueArn = GetAtt(res['QueueName'], 'Arn') # create subscription if (snsTopic): res['SubscriptionName'] = name + 'Subscription' subscription = t.add_resource( sns.SubscriptionResource( res['SubscriptionName'], TopicArn=snsTopic, Endpoint=queueArn, Protocol='sqs', RawMessageDelivery='true', )) t.add_resource( sqs.QueuePolicy(name + 'AllowSNS2SQSPolicy', Queues=[queue.Ref()], PolicyDocument={ "Version": "2008-10-17", "Id": "PublicationPolicy", "Statement": [{ "Sid": "Allow-SNS-SendMessage", "Effect": "Allow", "Principal": { "AWS": "*" }, "Action": ["sqs:SendMessage"], "Resource": queue.GetAtt("Arn"), "Condition": { "ArnEquals": { "aws:SourceArn": snsTopic } } }] })) # create consumer function res['FunctionName'] = name + 'Consumer' consumer = t.add_resource( serverless.Function( res['FunctionName'], Runtime=nodeRuntime, CodeUri=lambdaSrcPath, Handler=lambdaHandlerPath + name + 'Consumer.handler', Timeout=timeout, Events={ 'SQSTrigger': { 'Type': 'SQS', 'Properties': { 'Queue': queueArn, 'BatchSize': 10 } } }, Policies=[ 'arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole', { 'Version': '2012-10-17', 'Statement': [{ 'Effect': 'Allow', 'Action': [ 'sqs:ReceiveMessage', 'sqs:ChangeMessageVisibility', 'sqs:DeleteMessage' ], 'Resource': GetAtt(res['QueueName'], 'Arn') }], } ], VpcConfig=lambdaVpcConfig, Environment=awslambda.Environment(None, Variables=lambdaEnvVars), )) return res
'DATABASE_USER': Ref('MySQLUser'), 'DATABASE_PASSWORD': Ref('MySQLPass'), 'DATABASE_POOL_MIN': 1, 'DATABASE_POOL_MAX': 2, 'REDIS_URL': Join('', ['redis://', importRedisAddress, ':', importRedisPort]), 'SCORE_PERIOD_CONFIG': '[{"id":"day","duration":86400000},{"id":"week","duration":604800000}]', 'SCORE_UPDATES_QUEUE_URL': Ref('ScoreUpdatesQueue'), 'NODE_ENV': Ref('NodeEnv'), 'MOCK_EXCHANGE_SERVICE': Ref('MockExchangeService'), } # Setup Resources graphQL = serverless.Function('GraphQL') graphQL.Runtime = nodeRuntime graphQL.CodeUri = lambdaSrcPath graphQL.Handler = lambdaHandlerPath + 'GraphQL.handler' graphQL.Events = { 'API': { 'Type': 'Api', 'Properties': { 'Path': '/graphql', 'Method': 'post' } } } graphQL.Policies = [ 'arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole' ]
'DATABASE_POOL_MIN': 1, 'DATABASE_POOL_MAX': 2, 'NODE_ENV': Ref('NodeEnv'), 'KEYCLOAK_SERVER_URL': Ref('KeycloakServerURL'), 'KEYCLOAK_REALM': Ref('KeycloakRealm'), 'KEYCLOAK_CLIENT_ID': Ref('KeycloakClientID'), 'KEYCLOAK_CLIENT_SECRET': Ref('KeycloakClientSecret'), 'S3_BUCKET': accountMediaBucket.Ref(), 'AWS_KMS_CMK': Ref('AwsKmsCmk'), 'ADDED_KEYS_SNS_TOPIC_ARN': ImportValue(Sub('${CoreStack}-NewTraderExchangeTopicArn')), 'DELETED_KEYS_SNS_TOPIC_ARN': ImportValue(Sub('${CoreStack}-RemoveTraderExchangeTopicArn')), } # Setup Resources graphQL = serverless.Function('GraphQL') graphQL.Runtime = nodeRuntime graphQL.CodeUri = lambdaSrcPath graphQL.Handler = lambdaHandlerPath + 'GraphQL.handler' graphQL.Events = { 'API': { 'Type': 'Api', 'Properties': { 'Path': '/graphql', 'Method': 'post' } } } graphQL.Policies = [ 'arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole', {