def _create_kinesis_stream_trigger_from_meta(lambda_name, lambda_arn, role_name, trigger_meta): required_parameters = ['target_stream', 'batch_size', 'starting_position'] validate_params(lambda_name, trigger_meta, required_parameters) stream_name = trigger_meta['target_stream'] stream = CONN.kinesis().get_stream(stream_name) stream_arn = stream['StreamDescription']['StreamARN'] stream_status = stream['StreamDescription']['StreamStatus'] # additional waiting for stream if stream_status != 'ACTIVE': _LOG.debug('Kinesis stream %s is not in active state,' ' waiting for activation...', stream_name) time.sleep(120) # TODO policy should be moved to meta policy_name = '{0}KinesisTo{1}Lambda'.format(stream_name, lambda_name) policy_document = { "Statement": [ { "Effect": "Allow", "Action": [ "lambda:InvokeFunction" ], "Resource": [ lambda_arn ] }, { "Action": [ "kinesis:DescribeStreams", "kinesis:DescribeStream", "kinesis:ListStreams", "kinesis:GetShardIterator", "Kinesis:GetRecords" ], "Effect": "Allow", "Resource": stream_arn } ], "Version": "2012-10-17" } CONN.iam().attach_inline_policy(role_name=role_name, policy_name=policy_name, policy_document=policy_document) _LOG.debug('Inline policy %s is attached to role %s', policy_name, role_name) _LOG.debug('Waiting for activation policy %s...', policy_name) time.sleep(10) _add_kinesis_event_source(lambda_arn, stream_arn, trigger_meta) _LOG.info('Lambda %s subscribed to kinesis stream %s', lambda_name, stream_name)
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ import time from syndicate.commons.log_helper import get_logger from syndicate.core import CONN, ClientError from syndicate.core.helper import create_pool, unpack_kwargs from syndicate.core.resources.helper import build_description_obj _LOG = get_logger('syndicate.core.resources.kinesis_resource') _KINESIS_CONN = CONN.kinesis() def create_kinesis_stream(args): return create_pool(_create_kinesis_stream_from_meta, args) def remove_kinesis_streams(args): create_pool(_remove_kinesis_stream, args) @unpack_kwargs def _remove_kinesis_stream(arn, config): stream_name = config['resource_name'] try: _KINESIS_CONN.remove_stream(stream_name=stream_name)