You can do this by setting the post_parent
with the $attach_id
.
$my_post = array(
'ID' => $attach_id,
'post_parent' => $my_post_id //retrieved after you inserted your post
);
wp_update_post( $my_post );
Just run this after you have both the ID`s of the Post and the attachment, and you have set the parent of the attachment to be the post.