本文整理汇总了Python中c7n.mu.LambdaManager.publish方法的典型用法代码示例。如果您正苦于以下问题:Python LambdaManager.publish方法的具体用法?Python LambdaManager.publish怎么用?Python LambdaManager.publish使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类c7n.mu.LambdaManager
的用法示例。
在下文中一共展示了LambdaManager.publish方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_cwl_subscriber
# 需要导入模块: from c7n.mu import LambdaManager [as 别名]
# 或者: from c7n.mu.LambdaManager import publish [as 别名]
def test_cwl_subscriber(self):
self.patch(CloudWatchLogSubscription, 'iam_delay', 0.01)
session_factory = self.replay_flight_data('test_cwl_subscriber')
session = session_factory()
client = session.client('logs')
lname = "custodian-test-log-sub"
self.addCleanup(client.delete_log_group, logGroupName=lname)
client.create_log_group(logGroupName=lname)
linfo = client.describe_log_groups(
logGroupNamePrefix=lname)['logGroups'][0]
params = dict(
session_factory=session_factory,
name="c7n-log-sub",
role=self.role,
sns_topic="arn:",
log_groups=[linfo])
func = logsub.get_function(**params)
manager = LambdaManager(session_factory)
finfo = manager.publish(func)
self.addCleanup(manager.remove, func)
results = client.describe_subscription_filters(logGroupName=lname)
self.assertEqual(len(results['subscriptionFilters']), 1)
self.assertEqual(results['subscriptionFilters'][0]['destinationArn'],
finfo['FunctionArn'])
# try and update
params['sns_topic'] = "arn:123"
manager.publish(func)
示例2: provision
# 需要导入模块: from c7n.mu import LambdaManager [as 别名]
# 或者: from c7n.mu.LambdaManager import publish [as 别名]
def provision(config, session_factory):
func_config = dict(
name=config.get('lambda_name', 'cloud-custodian-mailer'),
description=config.get('lambda_description', 'Cloud Custodian Mailer'),
tags=config.get('lambda_tags', {}),
handler='periodic.dispatch',
runtime=config['runtime'],
memory_size=config['memory'],
timeout=config['timeout'],
role=config['role'],
subnets=config['subnets'],
security_groups=config['security_groups'],
dead_letter_config=config.get('dead_letter_config', {}),
events=[
CloudWatchEventSource(
{'type': 'periodic',
'schedule': config.get('lambda_schedule', 'rate(5 minutes)')},
session_factory,
prefix="")
])
archive = get_archive(config)
func = LambdaFunction(func_config, archive)
manager = LambdaManager(session_factory)
manager.publish(func)
示例3: main
# 需要导入模块: from c7n.mu import LambdaManager [as 别名]
# 或者: from c7n.mu.LambdaManager import publish [as 别名]
def main():
parser = setup_parser()
options = parser.parse_args()
logging.basicConfig(level=logging.DEBUG)
logging.getLogger('botocore').setLevel(logging.ERROR)
if not options.group and not options.prefix:
print("Error: Either group or prefix must be specified")
sys.exit(1)
session_factory = SessionFactory(
options.region, options.profile, options.assume)
groups = get_groups(session_factory, options)
func = logsub.get_function(
session_factory,
"cloud-custodian-error-notify",
role=options.role,
sns_topic=options.topic,
subject=options.subject,
log_groups=groups,
pattern=options.pattern)
manager = LambdaManager(session_factory)
try:
manager.publish(func)
except Exception:
import traceback, pdb, sys
traceback.print_exc()
pdb.post_mortem(sys.exc_info()[-1])
示例4: test_cwe_update_config_and_code
# 需要导入模块: from c7n.mu import LambdaManager [as 别名]
# 或者: from c7n.mu.LambdaManager import publish [as 别名]
def test_cwe_update_config_and_code(self):
# Originally this was testing the no update case.. but
# That is tricky to record, any updates to the code end up
# causing issues due to checksum mismatches which imply updating
# the function code / which invalidate the recorded data and
# the focus of the test.
session_factory = self.replay_flight_data(
'test_cwe_update', zdata=True)
p = Policy({
'resource': 's3',
'name': 's3-bucket-policy',
'mode': {
'type': 'cloudtrail',
'events': ["CreateBucket"],
},
'filters': [
{'type': 'missing-policy-statement',
'statement_ids': ['RequireEncryptedPutObject']}],
'actions': ['no-op']
}, Config.empty())
pl = PolicyLambda(p)
mgr = LambdaManager(session_factory)
result = mgr.publish(pl, 'Dev', role=self.role)
self.addCleanup(mgr.remove, pl)
p = Policy({
'resource': 's3',
'name': 's3-bucket-policy',
'mode': {
'type': 'cloudtrail',
'memory': 256,
'events': [
"CreateBucket",
{'event': 'PutBucketPolicy',
'ids': 'requestParameters.bucketName',
'source': 's3.amazonaws.com'}]
},
'filters': [
{'type': 'missing-policy-statement',
'statement_ids': ['RequireEncryptedPutObject']}],
'actions': ['no-op']
}, Config.empty())
output = self.capture_logging('custodian.lambda', level=logging.DEBUG)
result2 = mgr.publish(PolicyLambda(p), 'Dev', role=self.role)
lines = output.getvalue().strip().split('\n')
self.assertTrue(
'Updating function custodian-s3-bucket-policy code' in lines)
self.assertTrue(
'Updating function: custodian-s3-bucket-policy config' in lines)
self.assertEqual(result['FunctionName'], result2['FunctionName'])
# drive by coverage
functions = [i for i in mgr.list_functions()
if i['FunctionName'] == 'custodian-s3-bucket-policy']
self.assertTrue(len(functions), 1)
start = 0
end = time.time() * 1000
self.assertEqual(list(mgr.logs(pl, start, end)), [])
示例5: test_sns_subscriber
# 需要导入模块: from c7n.mu import LambdaManager [as 别名]
# 或者: from c7n.mu.LambdaManager import publish [as 别名]
def test_sns_subscriber(self):
self.patch(SNSSubscription, 'iam_delay', 0.01)
session_factory = self.replay_flight_data('test_sns_subscriber')
session = session_factory()
client = session.client('sns')
# create an sns topic
tname = "custodian-test-sns-sub"
topic_arn = client.create_topic(Name=tname)['TopicArn']
self.addCleanup(client.delete_topic, TopicArn=topic_arn)
# provision a lambda via mu
params = dict(
session_factory=session_factory,
name='c7n-hello-world',
role='arn:aws:iam::644160558196:role/custodian-mu',
events=[SNSSubscription(session_factory, [topic_arn])])
func = helloworld.get_function(**params)
manager = LambdaManager(session_factory)
manager.publish(func)
self.addCleanup(manager.remove, func)
# now publish to the topic and look for lambda log output
client.publish(TopicArn=topic_arn, Message='Greetings, program!')
#time.sleep(15) -- turn this back on when recording flight data
log_events = manager.logs(func, '1970-1-1', '9170-1-1')
messages = [e['message'] for e in log_events
if e['message'].startswith('{"Records')]
self.addCleanup(
session.client('logs').delete_log_group,
logGroupName='/aws/lambda/c7n-hello-world')
self.assertEqual(
json.loads(messages[0])['Records'][0]['Sns']['Message'],
'Greetings, program!')
示例6: process
# 需要导入模块: from c7n.mu import LambdaManager [as 别名]
# 或者: from c7n.mu.LambdaManager import publish [as 别名]
def process(self, buckets):
from c7n.mu import LambdaManager
from c7n.ufuncs.s3crypt import get_function
func = get_function(
None, self.data.get('role', self.manager.config.assume_role))
# Publish function to all of our buckets regions
region_funcs = {}
regions = set([
b.get('LocationConstraint', 'us-east-1') for b in buckets])
for r in regions:
lambda_mgr = LambdaManager(
functools.partial(self.manager.session_factory, region=r))
lambda_mgr.publish(func)
region_funcs[r] = func
with self.executor_factory(max_workers=3) as w:
results = []
futures = []
for b in buckets:
futures.append(
w.submit(
self.process_bucket,
region_funcs[b.get('LocationConstraint', 'us-east-1')],
b))
for f in as_completed(futures):
if f.exception():
log.exception(
"Error attaching lambda-encrypt %s" % (f.exception()))
results.append(f.result())
return filter(None, results)
示例7: test_can_switch_runtimes
# 需要导入模块: from c7n.mu import LambdaManager [as 别名]
# 或者: from c7n.mu.LambdaManager import publish [as 别名]
def test_can_switch_runtimes(self):
session_factory = self.replay_flight_data("test_can_switch_runtimes")
func = self.make_func()
mgr = LambdaManager(session_factory)
self.addCleanup(mgr.remove, func)
result = mgr.publish(func)
self.assertEqual(result["Runtime"], "python2.7")
func.func_data["runtime"] = "python3.6"
result = mgr.publish(func)
self.assertEqual(result["Runtime"], "python3.6")
示例8: create_a_lambda
# 需要导入模块: from c7n.mu import LambdaManager [as 别名]
# 或者: from c7n.mu.LambdaManager import publish [as 别名]
def create_a_lambda(self, flight, **extra):
session_factory = self.replay_flight_data(flight, zdata=True)
mode = {
"type": "config-rule", "role": "arn:aws:iam::644160558196:role/custodian-mu"
}
mode.update(extra)
p = Policy(
{
"resource": "s3",
"name": "hello-world",
"actions": ["no-op"],
"mode": mode,
},
Config.empty(),
)
pl = PolicyLambda(p)
mgr = LambdaManager(session_factory)
def cleanup():
mgr.remove(pl)
if self.recording:
time.sleep(60)
self.addCleanup(cleanup)
return mgr, mgr.publish(pl)
示例9: test_cwl_subscriber
# 需要导入模块: from c7n.mu import LambdaManager [as 别名]
# 或者: from c7n.mu.LambdaManager import publish [as 别名]
def test_cwl_subscriber(self):
self.patch(CloudWatchLogSubscription, "iam_delay", 0.01)
session_factory = self.replay_flight_data("test_cwl_subscriber")
session = session_factory()
client = session.client("logs")
lname = "custodian-test-log-sub"
self.addCleanup(client.delete_log_group, logGroupName=lname)
client.create_log_group(logGroupName=lname)
linfo = client.describe_log_groups(logGroupNamePrefix=lname)["logGroups"][0]
params = dict(
session_factory=session_factory,
name="c7n-log-sub",
role=ROLE,
sns_topic="arn:",
log_groups=[linfo],
)
func = logsub.get_function(**params)
manager = LambdaManager(session_factory)
finfo = manager.publish(func)
self.addCleanup(manager.remove, func)
results = client.describe_subscription_filters(logGroupName=lname)
self.assertEqual(len(results["subscriptionFilters"]), 1)
self.assertEqual(
results["subscriptionFilters"][0]["destinationArn"], finfo["FunctionArn"]
)
示例10: test_cwe_asg_instance
# 需要导入模块: from c7n.mu import LambdaManager [as 别名]
# 或者: from c7n.mu.LambdaManager import publish [as 别名]
def test_cwe_asg_instance(self):
session_factory = self.replay_flight_data('test_cwe_asg', zdata=True)
p = Policy({
'resource': 'asg',
'name': 'asg-spin-detector',
'mode': {
'type': 'asg-instance-state',
'events': ['launch-failure']}
}, Config.empty())
pl = PolicyLambda(p)
mgr = LambdaManager(session_factory)
result = mgr.publish(pl, 'Dev', role=self.role)
self.assert_items(
result,
{'FunctionName': 'maid-asg-spin-detector',
'Handler': 'maid_policy.run',
'MemorySize': 512,
'Runtime': 'python2.7',
'Timeout': 60})
events = session_factory().client('events')
result = events.list_rules(NamePrefix="maid-asg-spin-detector")
self.assert_items(
result['Rules'][0],
{"State": "ENABLED",
"Name": "maid-asg-spin-detector"})
self.assertEqual(
json.loads(result['Rules'][0]['EventPattern']),
{"source": ["aws.autoscaling"],
"detail-type": ["EC2 Instance Launch Unsuccessful"]})
mgr.remove(pl)
示例11: test_cwe_schedule
# 需要导入模块: from c7n.mu import LambdaManager [as 别名]
# 或者: from c7n.mu.LambdaManager import publish [as 别名]
def test_cwe_schedule(self):
session_factory = self.replay_flight_data(
'test_cwe_schedule', zdata=True)
p = Policy({
'resource': 'ec2',
'name': 'periodic-ec2-checker',
'mode': {
'type': 'periodic',
'schedule': 'rate(1 day)'
}
}, Config.empty())
pl = PolicyLambda(p)
mgr = LambdaManager(session_factory)
result = mgr.publish(pl, 'Dev', role=self.role)
self.assert_items(
result,
{'FunctionName': 'maid-periodic-ec2-checker',
'Handler': 'maid_policy.run',
'MemorySize': 512,
'Runtime': 'python2.7',
'Timeout': 60})
events = session_factory().client('events')
result = events.list_rules(NamePrefix="maid-periodic-ec2-checker")
self.assert_items(
result['Rules'][0],
{
"State": "ENABLED",
"ScheduleExpression": "rate(1 day)",
"Name": "maid-periodic-ec2-checker"})
mgr.remove(pl)
示例12: test_publishes_a_lambda
# 需要导入模块: from c7n.mu import LambdaManager [as 别名]
# 或者: from c7n.mu.LambdaManager import publish [as 别名]
def test_publishes_a_lambda(self):
session_factory = self.replay_flight_data("test_publishes_a_lambda")
mgr = LambdaManager(session_factory)
func = self.make_func()
self.addCleanup(mgr.remove, func)
result = mgr.publish(func)
self.assertEqual(result["CodeSize"], 169)
示例13: test_lambda_cross_account
# 需要导入模块: from c7n.mu import LambdaManager [as 别名]
# 或者: from c7n.mu.LambdaManager import publish [as 别名]
def test_lambda_cross_account(self):
self.patch(CrossAccountAccessFilter, "executor_factory", MainThreadExecutor)
session_factory = self.replay_flight_data("test_cross_account_lambda")
client = session_factory().client("lambda")
name = "c7n-cross-check"
tmp_dir = tempfile.mkdtemp()
self.addCleanup(os.rmdir, tmp_dir)
archive = PythonPackageArchive()
archive.add_contents("handler.py", LAMBDA_SRC)
archive.close()
func = LambdaFunction(
{
"runtime": "python2.7",
"name": name,
"description": "",
"handler": "handler.handler",
"memory_size": 128,
"timeout": 5,
"role": self.role,
},
archive,
)
manager = LambdaManager(session_factory)
manager.publish(func)
self.addCleanup(manager.remove, func)
client.add_permission(
FunctionName=name,
StatementId="oops",
Principal="*",
Action="lambda:InvokeFunction",
)
p = self.load_policy(
{
"name": "lambda-cross",
"resource": "lambda",
"filters": ["cross-account"],
},
session_factory=session_factory,
)
resources = p.run()
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0]["FunctionName"], name)
示例14: test_sqs_subscriber
# 需要导入模块: from c7n.mu import LambdaManager [as 别名]
# 或者: from c7n.mu.LambdaManager import publish [as 别名]
def test_sqs_subscriber(self):
session_factory = self.replay_flight_data('test_mu_sqs_subscriber')
func_name = 'c7n-hello-sqs'
queue_name = "my-dev-test-3"
# Setup Queues
session = session_factory()
client = session.client('sqs')
queue_url = client.create_queue(QueueName=queue_name).get('QueueUrl')
queue_arn = client.get_queue_attributes(
QueueUrl=queue_url,
AttributeNames=['QueueArn'])['Attributes']['QueueArn']
self.addCleanup(client.delete_queue, QueueUrl=queue_url)
# Setup Function
params = dict(
session_factory=session_factory,
name=func_name,
role="arn:aws:iam::644160558196:role/custodian-mu",
events=[SQSSubscription(session_factory, [queue_arn])])
func = helloworld.get_function(**params)
manager = LambdaManager(session_factory)
manager.publish(func)
self.addCleanup(manager.remove, func)
# Send and Receive Check
client.send_message(
QueueUrl=queue_url, MessageBody=json.dumps({'jurassic': 'block'}))
if self.recording:
time.sleep(60)
log_events = list(manager.logs(func, "1970-1-1 UTC", "9170-1-1"))
messages = [
e["message"] for e in log_events if e["message"].startswith('{"Records')
]
self.addCleanup(
session.client("logs").delete_log_group,
logGroupName="/aws/lambda/%s" % func_name)
self.assertIn(
'jurassic',
json.loads(messages[0])["Records"][0]["body"])
示例15: test_config_rule_provision
# 需要导入模块: from c7n.mu import LambdaManager [as 别名]
# 或者: from c7n.mu.LambdaManager import publish [as 别名]
def test_config_rule_provision(self):
session_factory = self.replay_flight_data('test_config_rule')
p = Policy({
'resource': 'security-group',
'name': 'sg-modified',
'mode': {'type': 'config-rule'},
}, Config.empty())
pl = PolicyLambda(p)
mgr = LambdaManager(session_factory)
result = mgr.publish(pl, 'Dev', role=self.role)
self.assertEqual(result['FunctionName'], 'custodian-sg-modified')
self.addCleanup(mgr.remove, pl)