def set_state(key, value, namespace=None, table_name=None, environment=None,<EOL>layer=None, stage=None, shard_id=None, consistent=True,<EOL>serializer=json.dumps, wait_exponential_multiplier=<NUM_LIT>,<EOL>wait_exponential_max=<NUM_LIT>, stop_max_delay=<NUM_LIT>, ttl=None):
if table_name is None:<EOL><INDENT>table_name = _state_table_name(environment=environment, layer=layer,<EOL>stage=stage)<EOL><DEDENT>if not table_name:<EOL><INDENT>msg = ("<STR_LIT>"<EOL>"<STR_LIT>".format(key))<EOL>logger.error(msg)<EOL>raise StateTableError(msg)<EOL>return<EOL><DEDENT>dynamodb = boto3.resource("<STR_LIT>")<EOL>table = dynamodb.Table(table_name)<EOL>logger.info("<STR_LIT>".format(key, value,<EOL>table_name))<EOL>if serializer:<EOL><INDENT>try:<EOL><INDENT>value = serializer(value)<EOL><DEDENT>except TypeError:<EOL><INDENT>logger.error(<EOL>"<STR_LIT>".format(<EOL>key))<EOL>raise<EOL><DEDENT><DEDENT>if namespace:<EOL><INDENT>key = "<STR_LIT>".format(namespace, key)<EOL><DEDENT>if shard_id:<EOL><INDENT>key = "<STR_LIT>".format(shard_id, key)<EOL><DEDENT>item = {"<STR_LIT:id>": key, "<STR_LIT:value>": value}<EOL>if ttl:<EOL><INDENT>item["<STR_LIT>"] = {"<STR_LIT:N>": str(int(time.time() + ttl))}<EOL><DEDENT>@retry(retry_on_exception=_is_critical_exception,<EOL>wait_exponential_multiplier=<NUM_LIT>,<EOL>wait_exponential_max=<NUM_LIT>,<EOL>stop_max_delay=<NUM_LIT>)<EOL>def put_item():<EOL><INDENT>try:<EOL><INDENT>return table.put_item(Item=item)<EOL><DEDENT>except Exception as err:<EOL><INDENT>if _is_dynamodb_critical_exception(err):<EOL><INDENT>raise CriticalError(err)<EOL><DEDENT>else:<EOL><INDENT>raise<EOL><DEDENT><DEDENT><DEDENT>resp = put_item()<EOL>logger.info("<STR_LIT>".format(resp))<EOL>return resp<EOL>
Set Lambda state value.