我遵循了不同的链接,但没有一个对我有用。我最后的方法是将从MQ接收到的消息放入Kinesis数据流的不同帐户。
使用sts_client进行相应编辑的代码:-
import time
import boto3
import stomp
import json
class Listener(stomp.ConnectionListener):
def on_error(self, headers, message):
print('received an error "%s"' % message)
def on_message(self, headers, message):
print('received a message "%s"' % message)
kinesis_client.put_record(
StreamName='',
Data=u'{}\r\n'.format(message).encode('utf-8'),
PartitionKey='0'
)
sts_client = boto3.client('sts')
kinesis_client = boto3.client(region_name="eu-central-1",aws_access_key_id = sts_response['Credentials']['AccessKeyId'],aws_secret_access_key = sts_response['Credentials']['SecretAccessKey'])
def lambda_handler(event, context):
sts_response = client.assume_role(RoleArn='arn:aws:iam::12345678910:role/project-ppl-bookings-kinesis')
conn = stomp.Connection(host_and_ports=[('b-4714-4441-8166-47aae158281a-1.mq.eu-central-1.amazonaws.com', 61614)])
lst = Listener()
conn.set_listener('Listener', lst)
conn.set_ssl(for_hosts=[('b-4714-4441-8166-47aae158281a-1.mq.eu-central-1.amazonaws.com', 61614)])
conn.start()
print('CONNECTION Started')
conn.connect(login='test_mq', passcode='',wait=True)
print('CONNECTION established')
conn.subscribe(destination='/queue/OnePurchasing', id=1, ack='auto')
print('CONNECTION Subscribed')
time.sleep(10)
conn.disconnect()
return
在对如何连接到不同帐户进行了一些基础研究之后,我已经更新了代码:-
Response:
{
"errorMessage": "module initialization error"
}
Request ID:
"9d810868-f165-11e8-b722-a38a441089b2"
Function Logs:
START RequestId: 9d810868-f165-11e8-b722-a38a441089b2 Version: $LATEST
module initialization error: name 'sts_response' is not defined
END RequestId: 9d810868-f165-11e8-b722-a38a441089b2
REPORT RequestId: 9d810868-f165-11e8-b722-a38a441089b2 Duration: 225.12 ms Billed Duration: 300 ms Memory Size: 128 MB Max Memory Used: 30 MB
module initialization error
name 'sts_response' is not defined